Click here to Skip to main content
16,005,037 members
Home / Discussions / Managed C++/CLI
   

Managed C++/CLI

 
GeneralRe: What makes a class IDisposable? [modified] Pin
mid=574112-Aug-07 9:00
mid=574112-Aug-07 9:00 
GeneralRe: What makes a class IDisposable? Pin
iddqd51513-Aug-07 6:06
iddqd51513-Aug-07 6:06 
GeneralRe: What makes a class IDisposable? Pin
mid=574113-Aug-07 6:15
mid=574113-Aug-07 6:15 
GeneralRe: What makes a class IDisposable? Pin
led mike13-Aug-07 6:28
led mike13-Aug-07 6:28 
GeneralRe: What makes a class IDisposable? Pin
mid=574113-Aug-07 6:46
mid=574113-Aug-07 6:46 
GeneralRe: What makes a class IDisposable? Pin
iddqd51513-Aug-07 6:29
iddqd51513-Aug-07 6:29 
GeneralRe: What makes a class IDisposable? Pin
mid=574113-Aug-07 6:45
mid=574113-Aug-07 6:45 
GeneralRe: What makes a class IDisposable? Pin
iddqd51513-Aug-07 7:32
iddqd51513-Aug-07 7:32 
That's just it though, it is syntactic sugar. When you write ~Class1() in C# you've got a finalizer. It's not doing something then calling Finalize() I don't believe. Internally the compiler synthesizes that to a call to Dispose(false) which is invoked by the garbage collector right before the memory is reclaimed nondeterministically.
By contrast, a destructor in C++/CLI is synthesized to
Dispose()<br />
{<br />
   Dispose(true);<br />
   GC::SuppressFinalize();<br />
}

While a call to !Class1() in C++/CLI (the finalizer) results in a call to Dispose(false).

The ~Class1() is a finalizer, not a destructor. To call it a destructor is a complete misnomer and doesn't convey when or how its called. A destructor would be called automatically when the object goes out of scope (via a using block in C# or via stack semantics in C++/CLI) or when invoked specifically by Dispose(). You must not reference other finalizable objects in that finalizer and other similar rules for finalizers that aren't present in a destructor. In C++/CLI a class destructor is created by the compiler automatically and will clean up managed resources for you. A finalizer (~Class1() in C#) is not generated by the compiler. It only exists if you explicitly write one. Most important is the fact that the ~Class1() in C# is nondeterminitic which by its nature makes it not a destructor (since they're deterministic) and the performance penalty for creating a finalizer for a class (In C++/CLI you would incur this cost with !Class1() but ~Class1() would enable actual RAII destructor semantics like native C++).

In C++/CLI you would write ~Class1() to properly handle the closing of resources in a way that can be done deterministically (when the object goes out of scope or a call to delete is made). This is a destructor. In C# when you write ~Class1() you're providing a way for the garbage collector to handle resource cleanup or unmanaged cleanup at the last second before memory reclamation in the case that the user failed to manually release it with Dispose or a using block. This is a finalizer. There is no destructor. You can't call delete. You have to write a Dispose method and/or use using blocks.

The C++/CLI dtor/ctor behavior is semantically the same as native C++. Although the destructor in C++/CLI doesn't cause the memory to actually be reclaimed (as that's always the garbage collectors job for managed objects) it is invoked deterministically (unlike the C# ~Class1() finalizer) which ensures resources will be closed immediately (unlike the C# finalizer).

Hopefully that wasn't too rambling...
GeneralRe: What makes a class IDisposable? Pin
mid=574113-Aug-07 7:53
mid=574113-Aug-07 7:53 
GeneralRe: What makes a class IDisposable? Pin
iddqd51513-Aug-07 8:09
iddqd51513-Aug-07 8:09 
GeneralRe: What makes a class IDisposable? Pin
Mark Salsbery13-Aug-07 8:44
Mark Salsbery13-Aug-07 8:44 
GeneralRe: What makes a class IDisposable? Pin
iddqd51513-Aug-07 8:55
iddqd51513-Aug-07 8:55 
GeneralRe: What makes a class IDisposable? Pin
Mark Salsbery13-Aug-07 9:35
Mark Salsbery13-Aug-07 9:35 
GeneralRe: What makes a class IDisposable? Pin
mid=574113-Aug-07 8:48
mid=574113-Aug-07 8:48 
GeneralRe: What makes a class IDisposable? Pin
iddqd51513-Aug-07 9:03
iddqd51513-Aug-07 9:03 
GeneralRe: What makes a class IDisposable? Pin
mid=574113-Aug-07 9:15
mid=574113-Aug-07 9:15 
GeneralRe: What makes a class IDisposable? [modified] Pin
iddqd51513-Aug-07 9:19
iddqd51513-Aug-07 9:19 
GeneralRe: What makes a class IDisposable? Pin
mid=574113-Aug-07 9:30
mid=574113-Aug-07 9:30 
GeneralRe: What makes a class IDisposable? Pin
iddqd51513-Aug-07 9:36
iddqd51513-Aug-07 9:36 
GeneralRe: What makes a class IDisposable? Pin
Mark Salsbery13-Aug-07 9:38
Mark Salsbery13-Aug-07 9:38 
GeneralRe: What makes a class IDisposable? [modified] Pin
iddqd51513-Aug-07 9:43
iddqd51513-Aug-07 9:43 
GeneralRe: What makes a class IDisposable? Pin
Mark Salsbery13-Aug-07 9:51
Mark Salsbery13-Aug-07 9:51 
GeneralRe: What makes a class IDisposable? Pin
iddqd51513-Aug-07 9:55
iddqd51513-Aug-07 9:55 
GeneralRe: What makes a class IDisposable? Pin
Luc Pattyn13-Aug-07 7:00
sitebuilderLuc Pattyn13-Aug-07 7:00 
GeneralRe: What makes a class IDisposable? Pin
Mark Salsbery12-Aug-07 10:06
Mark Salsbery12-Aug-07 10:06 

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.