I'm currently wrapping a C++ class with C++/CLI for .NET interoperability following the standard process of holding a native pointer in a managed class. In one instance, I have a native class that has a function like:
std::shared_ptr<BaseChannel> channelData(const int RunNumber);
I have already begun creating a wrapper class for BaseChannel
. However, if I pass the raw pointer to the constructor of the managed class, there are no guarantees on the lifetime of the object being pointed to by the managed class. I.e. the shared_ptr could go out of scope and the object will get deleted and the managed class will be left holding a dangling pointer.
What is the common solution for this situation?
UPDATE
@Ben: So I wrap the class that holds the method in the above question like so (let's say it is in a native class called Node
and it is being wrapped in a managed class called NodeRef:
ChannelUser^ NodeRef::ChannelData(int runNumber)
{
// mpNode is native class pointer of type Node held in managed class
// wrapper called NodeRef
std::shared_ptr<BaseChannel> spBaseChannel = mpNode->channelData(runNumber);
// ChannelUser is using clr_scoped_ptr to hold the shared_ptr
ChannelUser^ channelUser = gcnew ChannelUser(spBaseChannel);
return channelUser;
}
Because the shared_ptr does not have its reference count increased as it is passed to the managed class by reference, does that mean
that as long as this shared_ptr is in scope, the object it points to will still exist, because its reference count will be at least 1
? (ref C++ - passing references to std::shared_ptr or boost::shared_ptr)
spBaseChannel
's constructor, once bynew shared_ptr
in the ChannelUser ctor-initializer)... but this stuff isn't going to be thread safe whenstd::shared_ptr
isn't. – Paregmenon