Geeks With Blogs
Blog Moved to http://podwysocki.codebetter.com/ Blog Moved to http://podwysocki.codebetter.com/
Nishant Sivakumar recently wrote in his blog about NULL pointers and C++/CLI in his post "You *can* delete a NULL pointer!".   What I did know is that delete works fine on unmanaged code, but I wasn't 100% sure about managed, and now this confirms that it works exactly as I thought it would.
 
Take for example this code:
 
public ref class MyClass
{
     public:
          MyClass();
          ~MyClass();
};
 
And this calling code:
 
MyClass myClass = nullptr;
delete myClass;
 
In this above case, the destructor would not be called and no adverse affects would happen.  The if check is not necessary in this case.  The if check is still necessary for all other method operations such as this:
 
class MyUnmanagedClass : public IUnknown
{
     // Add IUnknown Methods Here
};  
 
MyClass* myClass = NULL;
hr = CoCreateInstance(__uuidof(MyUnmanagedClass), NULL,
                        CLSCTX_LOCAL_SERVER,
                        __uuidof(MyUnmanagedClass),
                        (void**) &myClass);
 
if(myClass)
     myClass->Release();
 
Posted on Tuesday, June 20, 2006 2:36 PM Microsoft , .NET | Back to top


Comments on this post: C++ - Dipose with Null Pointers

No comments posted yet.
Your comment:
 (will show your gravatar)


Copyright © Matthew Podwysocki | Powered by: GeeksWithBlogs.net