I have some questions regarding use of gcnew and explicitly calling delete on the created object.
It appears that I have a leak in my code.
Consider the following example.
I have an unmanaged class:
I have a managed class which wraps the unmanaged class:
public class FooBar
I have a method which returns a handle to the managed class to a .Net C# client:
ref class Foo
private FooBar* pFooBar;
pFooBar = new FooBar();
pFooBar = nullptr;
public Foo^ GetMeAFoo()
Foo^ foo = gcnew Foo();
It's my understanding that since Foo is created on the managed heap that I do not have to explicitly
delete it. Garbage Collection (GC) on the managed side should take care of it.
The problem in my case is that the destructor of my managed class never seems to get called.
The results in the interior pointer to my unmanaged class getting (memory) leaked.
What is the correct way to handle this situation so that the underlying unmanaged class (FooBar) gets
correctly freed (when the managed wrapper class goes out of scope)?