# Managed C++ Wrapper Class Destructor Not Getting Called

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

## 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 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 on other sites
Quote:
 Original post by PromitAssuming 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.

1. 1
2. 2
3. 3
4. 4
Rutin
17
5. 5

• 11
• 31
• 12
• 12
• 11
• ### Forum Statistics

• Total Topics
631409
• Total Posts
2999927
×