• Advertisement
Sign in to follow this  

Managed C++ Wrapper Class Destructor Not Getting Called

This topic is 4123 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
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
Sign in to follow this  

  • Advertisement