I have a third-party unmanaged C++ library which has two classes, let's call them
ClassA has a method, let's call it
getTheB(), which returns an instance of
ClassB - it does not return a pointer to the instance, but the instance itself.
I now wrote a managed wrapper for
ClassA which in turn has a method
getTheB() which returns a managed wrapper wrapping the
ClassB object from the third-party library has to be handed over to my managed wrapper via its pointer, like:
ThirdParty::ClassB db = delegateForClassA -> getTheB(); ManagedClassB^ mb = gcnew ManagedClassB(&db);
However, when my wrapped
getTheB() of my
ClassA wrapper finishes and the scope ends, the
ManagedClassB instance contains a dangling reference to the third-party
ClassB and the destructor of the latter one is called, leading to funny results when accessing methods of
In my other question, I was told to somehow store the original
ClassB object, but I don't know how.
So, how do I keep the third-party
ClassB instance alive?