Maxwell wrote:[color=blue]
> Hello,
>
> Newbie question here for disposing of unmanaged resources in MC++.NET.
> I have a managed VS.NET 2003 MC++ wrapper class that wraps a unmanaged
> C++ dll. What I am trying to figure out is what is the "best practice"
> for disposing of pointers to unmanaged classes that you have newed in
> your constructor in MC++
>
> For a better description of what is the standard affair I have tried
> looking online at:
>
http://msdn.microsoft.com/library/de...estructors.asp
>
> but I would have to say its very lacking in detail or explanation.
> Does
> anyone know of a better reference or can point me to for this?
>
> My constructor looks something like this:
>
> MClass::MClass()
> {
> pUnmanagedClass_ = new UnmanagedLib::UClass(); //unmanaged heap
> pBridgeClass_ = new _UClassBridge(); //unmanaged heap
>
>
> pUnmanagedClass_->RegisterUnmanagedCallBack(&(pBridgeClass_->UnmanagedBridgeCallback));
> }
>
> pUnanagedClass_ is a pointer to class in a unmanaged C++ DLL
> pBridgeClass_ is a pointer to a __nogc class I created in MC++
>
> Here is my destructor for this class:
> MClass::~MClass()
> {
> pUnmanagedClass_->UnRegisterCallbacks();
> pUnmanagedClass_->~UClass(); ///do I need this line
>
> delete pUnmanagedClass_; //because it was newed
> delete pBridgeClass_; //because it was newed
> }
>
> Since I newed the pointers I would guess that I would have to
> explicitly call delete on them...is this the case? I also need to
> ensure that destrutor gets called in the C++ DLL immediately
> (pUnmanagedClass) so I added a line for this in the managed class
> destructor...is this necessary or is it safe to assume it will get
> called because the delete is called[/color]
A couple points:
1. You don't need to explicitly call the destructor - the delete expression
will take care of that (in fact, you need to NOT call it, since otherwise
it'll be called twice and classes are not generally expected to handle
having their destructor run twice).
2. If the unmanaged classes are holding important resources (other than just
unmanaged heap), you should consider making your managed class IDisposable
and disposing of the unmanaged class in your IDisposable::Dispose. The
~MClass "destructor" is the finalizer under MC++, and there's no guarantee
that a finalizer will ever run (since there's no guarantee that the GC will
ever run).
3. As another poster pointed out, you're doing multiple allocations in your
constructor with no try/catch protection. You have potential resource leaks
if an exception is thrown in your constructor. If you need to make it
bullet proof, I'd suggest picking up a copy of Herb Sutter's excellent book
"Exceptional C++" - about 1/2 of the book is about building exception
safety.
-cd