Managed C++ Wrapper Class Destructor Not Getting Called

Started by
1 comment, last by Raeldor 17 years, 5 months ago
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
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().
SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.
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.

This topic is closed to new replies.

Advertisement