Confused about __crt_dll_terminate

S

Steve Baer

I'm wrapping unmanaged C++ classes using the standard managed C++ approach.
i.e. a __gc class that contains an internal pointer to an unmanaged class.
The purpose is to generate a .NET class library from our unmanaged class
library.

There are static unmanaged classes in this library, so I'm following the
MSFT approach of creating static functions that call __crt_dll_initialize at
startup and __crt_dll_terminate at the end of the calling application. My
problem occurs when I call the __crt_dll_terminate function at the end of my
test application. Immediately after this function gets called, the garbage
collector cleans up wrapper classes.

WrapperClass::~WrapperClass(){
if(m_ptr) delete m_ptr; //delete internal unmanaged class
m_ptr=NULL;
}

The wrapper class finalizer throws a NullPointer exception when I attempt to
delete the internal pointer. If I remove the call to __crt_dll_terminate,
everything works fine. I guess I'm confused as to what to do about this.

Thanks for any help in advance,
-steve
 
S

S. Han

If you write your own destructor you must suppress the GC finalizer.
So In your Wrapper class, override Dispose method:
public void Dispose()
{ // ... do your clean up
GC.SuppressFinalize(this);
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top