Click here to Skip to main content
13,352,280 members (68,686 online)
Click here to Skip to main content
Articles » Languages » C++ / CLI » C++/CLI » Revisions
Add your own
alternative version


35 bookmarked
Posted 20 Jan 2006

CAutoNativePtr - A managed smart pointer for using native objects in managed code

, 20 Jan 2006
Rate this:
Please Sign up or sign in to vote.
CAutoNativePtr is a managed template class that acts as a smart pointer, and is handy for using native objects in managed code.
This is an old version of the currently published article.


<CODE lang=mc++>CAutoNativePtr is a managed template class that acts as a smart pointer, and is handy for using native objects in managed code.

<pre lang=mc++>template < typename T > ref class CAutoNativePtr

Here, <CODE lang=mc++>T is the native type that's wrapped. 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 <CODE lang=mc++>CAutoNativePtr can own a specific native object at any time (unless you write buggy code that directly overrides this rule).

Using the class

Here's some sample code that shows how the class can be used.

<pre lang=mc++>class Native { public: void F() { } }; class Derived : public Native{}; void SomeFunc(Native){} //Function takes a Native object void SomeOtherFunc(Native*){} //Function takes a Native* ref class Ref { CAutoNativePtr<Native> m_native; //Declare the smart pointer object public: Ref() { } Ref(Native* pN) : m_native(pN) //Constructor that takes a T* { } //Copy constructor comes into play here Ref(CAutoNativePtr<Native> pN) : m_native(pN) { } //Assigning from a T* void Change(Native* pNew) { m_native = pNew; } //Assigning from another smart pointer void Change(CAutoNativePtr<Native> pNew) { m_native = pNew; } void DoStuff() { if(!m_native) // Logical NOT applied via T* cast { } else { m_native->F(); //-> operator at work here SomeFunc(*m_native); //The T* cast at work SomeOtherFunc(m_native); //The T* cast at work } } bool DoComparisons(CAutoNativePtr<Native> a1, CAutoNativePtr<Native> a2, CAutoNativePtr<Native> a3) { //Operators == and != applied via T* cast return (a1 == a2) && (a1 != a3); } void Close() { m_native.Destroy(); //Free native resource } }; int main() { CAutoNativePtr<Derived> d1(new Derived); CAutoNativePtr<Derived> d2(new Derived); //Specialized constructor for derived types is called here CAutoNativePtr<Native> n1(d1); //Specialized assignment operator for derived types is called here n1 = d2; return 0; }

Class Reference


<CODE lang=mc++>CAutoNativePtr - The constructor

There are four overloads that you can use.

  • <CODE lang=mc++>CAutoNativePtr()
  • <CODE lang=mc++>CAutoNativePtr(T* t)
  • <CODE lang=mc++>CAutoNativePtr(CAutoNativePtr<T>% an)
  • <CODE lang=mc++>template<typename TDERIVED> CAutoNativePtr(CAutoNativePtr<TDERIVED>% an)

The parameter-less constructor creates a <CODE lang=mc++>CAutoNativePtr that wraps a <CODE lang=mc++>nullptr object of type <CODE lang=mc++>T. The overload that takes a <CODE lang=mc++>T* can be used to wrap an existing pointer. Then, there are two copy constructor overloads, where one of them is to copy construct from a <CODE lang=mc++>CAutoNativePtr<T> (same type) and the other is to copy construct from a <CODE lang=mc++>CAutoNativePtr<TDERIVED> (which wraps a derived type of <CODE lang=mc++>T). When you copy construct a <CODE lang=mc++>CAutoNativePtr object, the source object's <CODE lang=mc++>T* is detached, because only one <CODE lang=mc++>CAutoNativePtr should own a <CODE lang=mc++>T* at any given time, else we end up with double deletion.

<CODE lang=mc++>~CAutoNativePtr/<CODE lang=mc++>!CAutoNativePtr - Destructor and Finalizer

  • <CODE lang=mc++>!CAutoNativePtr()
  • <CODE lang=mc++>~CAutoNativePtr()

The allocated object (if any) is freed. By having the destructor invoke the finalizer, both stack semantics and non-deterministic garbage collection are supported.

<CODE lang=mc++>Attach - To take over an existing <CODE lang=mc++>T*

  • <CODE lang=mc++>void Attach(T* t)

The <CODE lang=mc++>CAutoNativePtr will take ownership of the <CODE lang=mc++>T*, and if there's an existing <CODE lang=mc++>T*, it will be deleted.

<CODE lang=mc++>Detach - Release the <CODE lang=mc++>T*

  • <CODE lang=mc++>T* Detach()

The underlying <CODE lang=mc++>T* is released, and it's up to the caller to free the object now.

<CODE lang=mc++>Destroy - Delete the underlying <CODE lang=mc++>T*

  • <CODE lang=mc++>void Destroy()

The underlying <CODE lang=mc++>T* is deleted. Once you make this call, the <CODE lang=mc++>CAutoNativePtr does not own any object any more.


<CODE lang=mc++>operator-> - Pointer to member operator

  • <CODE lang=mc++>static T* operator->(CAutoNativePtr<T>% an)

This returns the underlying <CODE lang=mc++>T* object and allows the user to access <CODE lang=mc++>T methods and fields by using the <CODE lang=mc++>-> operator.

<CODE lang=mc++>operator T* - Cast to <CODE lang=mc++>T*

  • <CODE lang=mc++>static operator T*(CAutoNativePtr<T>% an)

This is a cast to the underlying <CODE lang=mc++>T*. This means you can pass a <CODE lang=mc++>CAutoNativePtr object where a <CODE lang=mc++>T* is expected, which is pretty convenient.

<CODE lang=mc++>operator= - Assignment operator

There are three overloads for the assignment operator.

  • <CODE lang=mc++>CAutoNativePtr<T>% operator=(T* t)
  • <CODE lang=mc++>CAutoNativePtr<T>% operator=(CAutoNativePtr<T>% an)
  • <CODE lang=mc++>template<typename TDERIVED> CAutoNativePtr<T>% operator=(CAutoNativePtr<TDERIVED>% an)

The first one takes a <CODE lang=mc++>T*. If the <CODE lang=mc++>CAutoNativePtr currently owns a <CODE lang=mc++>T*, that's released before ownership of the new <CODE lang=mc++>T* is taken. The other two overloads are for assignment from <CODE lang=mc++>CAutoNativePtr objects, where one of them is specialized to handle a <CODE lang=mc++>CAutoNativePtr object that owns a <CODE lang=mc++>T derived object. When ownership is taken, it's transferred, which means the source objects loses ownership of the <CODE lang=mc++>T*, and this is done to avoid double-deletion.


  • January 19th, 2006 : Article and code first published


This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)


About the Author

Nish Nishant
United States United States
Nish Nishant is the Principal Software Architect/Consultant for Ganymede Software Solutions LLC, and is based out of Columbus, Ohio. He has over 17 years of software industry experience in various roles including Lead Software Architect, Principal Software Engineer, and Product Manager. Nish was a Microsoft Visual C++ MVP between 2002 and 2015.

Nish is an industry acknowledged expert in the Microsoft technology stack. He authored C++/CLI in Action for Manning Publications in 2005, and had previously co-authored Extending MFC Applications with the .NET Framework for Addison Wesley in 2003. In addition, he has over 140 published technology articles on and another 250+ blog articles on his WordPress blog. Nish is vastly experienced in team management, mentoring teams, and directing all stages of software development.

Contact Nish : If you are interested in hiring Nish as a consultant, you can reach him via his google email id voidnish.

Company Website :

You may also be interested in...


Comments and Discussions

Discussions on this specific version of this article. Add your comments on how to improve this article here. These comments will not be visible on the final published version of this article.
QuestionCatch with delete Pin
guy_montag16-Nov-17 0:42
memberguy_montag16-Nov-17 0:42 
Generalmissing namespace Pin
hofingerandi13-May-11 1:13
memberhofingerandi13-May-11 1:13 
GeneralExport CAutoNativePtr from one C++/CLI DLL to another Pin
Andyb197922-May-09 6:56
memberAndyb197922-May-09 6:56 
GeneralRef class usage Pin
Robin Imrie22-Feb-06 7:05
memberRobin Imrie22-Feb-06 7:05 
Newsmsclr::auto_ptr (STL-like solution) Pin
DS2-Feb-06 2:03
memberDS2-Feb-06 2:03 
GeneralRe: msclr::auto_ptr (STL-like solution) Pin
Nishant Sivakumar2-Feb-06 2:42
staffNishant Sivakumar2-Feb-06 2:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.180111.1 | Last Updated 20 Jan 2006
Article Copyright 2006 by Nish Nishant
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid