ArrayView in DEBUG build gives (probably) erroneous RCP message
Created by: ambrad
Consider the following short program:
#include "Tpetra_Map.hpp"
#include "Teuchos_ArrayView.hpp"
#include "Tpetra_DefaultPlatform.hpp"
#include "Teuchos_GlobalMPISession.hpp"
typedef double ST;
typedef int GO;
typedef int LO;
typedef Kokkos::Compat::KokkosDeviceWrapperNode<Kokkos::Serial> KokkosNode;
typedef Tpetra::Map<LO, GO, KokkosNode> Tpetra_Map;
int main (int argc, char** argv) {
Teuchos::GlobalMPISession mpiSession(&argc, &argv);
auto comm = Tpetra::DefaultPlatform::getDefaultPlatform().getComm();
static const int N = 10;
#if 1
// In a Trilinos DEBUG build, fails with RCP message.
GO* ids = new GO[N];
for (int i = 0; i < N; ++i) ids[i] = i;
Teuchos::ArrayView<const GO> ids_av = Teuchos::arrayView(ids, N);
Tpetra_Map m(N, ids_av, 0, comm);
delete[] ids;
#else
// OK (and better, anyway).
Teuchos::Array<GO> ids(N);
for (int i = 0; i < N; ++i) ids[i] = i;
Tpetra_Map m(N, ids, 0, comm);
#endif
}
In the #if 1
branch, the program fails with:
terminate called after throwing an instance of 'Teuchos::DanglingReferenceError'
what(): /home/ambradl/bigcode/tril/Trilinos/install-shared-dbg/include/Teuchos_RCPNode.hpp:605:
Throw number = 1
Throw test that evaluated to true: true
Error, an attempt has been made to dereference the underlying object
from a weak smart pointer object where the underling object has already
been deleted since the strong count has already gone to zero.
The #if 0 branch is fine. This appears to be a bug in ArrayView in a DEBUG build.