Sign in to follow this  
Raeldor

Managed C++ Wrapper Class Destructor Not Getting Called

Recommended Posts

Hi All, I have a wrapper class like...
	MTerrain::MTerrain(String^ in_name, int in_maxSimultaneousTextures)
	{
		NW_STRING name;
		MarshalString(in_name, name);
		m_terrain=new NW::Terrain(name, in_maxSimultaneousTextures);
	}

	MTerrain::~MTerrain()
	{
		delete m_terrain;
	}

But when I use this in C# like...
MTerrain test=new Terrain("test", 8);
test=null;

The destructor is not getting called. Unless I specifically do...
test.Dispose();
test=null;

Why does the destructor not get called immediately on the test=null? Is this a function of the garbage collector? Thanks Rael

Share this post


Link to post
Share on other sites
Assuming you mean C++/CLI, "destructor" syntax actually causes you to implement IDisposable, and that becomes your Dispose method.

If you want to do something after all references are gone and before you're collected, you'll need to write a finalizer, which uses !Class() instead of ~Class().

Share this post


Link to post
Share on other sites
Quote:
Original post by Promit
Assuming you mean C++/CLI, "destructor" syntax actually causes you to implement IDisposable, and that becomes your Dispose method.

If you want to do something after all references are gone and before you're collected, you'll need to write a finalizer, which uses !Class() instead of ~Class().

Thanks for the reply. When you say 'do something after all references are gone and before you're collected', does this mean !Class() gets called before ~Class()? Does ~Class() only get called when you either specifically call Dispose(), or when the garbage collector feels like it? Seems like it never gets called even after the program shuts down, which is a bit strange to me.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this