When dealing with disposable objects, C# offers elegant and simple syntax using the
using statement which disposes managed resources and simplifies the code required to perform cleanup. In other words, the necessary
try-finally block can be rewritten into one simple statement. However, when using disposable managed objects in the C++/CLI code, there is no equivalent for the
using block. Programmer must write the entire try-finally block, introduce new local variable and needlessly complicate the code which would otherwise be relatively clean.
Either declare disposable objects on the stack or use the helper wrapper class at the end of this blog post. Example:
// on the stack, automatic destructor call Managed m; // long syntax, unique_ptr like syntax using_ptr<Managed^> m2(gcnew M()); // shorter syntax, same wrapper as the above auto m3 = make_using_ptr(gcnew M());