I have a third-party unmanaged C++ library which has two classes, let's call them ClassA and ClassB. 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.
The original 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 ClassB.
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?
                        
You can either change getTheB to return a heap-allocated ClassB, or have ManagedClassB make his own copy of the db object.
Update for the copy:
I assume ManagedClassB's constructor looks something like
You should simply change it to
or