CAutoNativePtr is a managed template class that I wrote, which acts as a smart pointer for using native objects in managed code, and it’s been posted on Code Project.
The class manages a smart pointer, which will automatically free the native resource when it falls out of scope or the containing managed object is finalized during garbage collection. The copy constructors and assignment operators transfer ownership, which means that only one
CAutoNativePtr can own a specific native object at any time (unless you write buggy code that directly overrides this rule). Take a look at the article for more details and to download the class.