LionAM wrote:
In my case, I need the local object because it is a lock (which has to be destroyed when leaving scope).
Makes sense. Just for reference, there's other ways to get the destructor called:
(from Destructors and Finalizers in Visual C++[^]):
Code authored in Visual C++ and compiled with /clr will run a type's destructor for the following reasons:
\* If an object created using stack semantics goes out of scope.
\* If an exception is thrown during the object's construction.
\* If the object is a member in an object whose destructor is running.
\* If you call the delete Operator (C++) on a handle (^ (Handle to Object on Managed Heap)).
\* If you explicitly call the destructor.
If your type is being consumed by a client authored in another language, the destructor will be called when:
\* A call to Dispose.
\* Calling Dispose(void) on the type.
\* If the type goes out of scope in a C# using statement.
Regardless, having a second reference outside the scope is a bad idea and shouldn't be necessary :) Cheers, Mark
Mark Salsbery Microsoft MVP - Visual C++ :java: