Click here to Skip to main content
15,889,931 members
Home / Discussions / Managed C++/CLI
   

Managed C++/CLI

 
GeneralRe: What makes a class IDisposable? Pin
iddqd51513-Aug-07 7:32
iddqd51513-Aug-07 7:32 
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 
From: http://en.wikipedia.org/wiki/Destructor_%28computer_science%29

In object-oriented programming, a destructor (sometimes shortened to dtor) is a method which is automatically invoked when the object is destroyed. Its main purpose is to clean up and to free the resources which were acquired by the object along its life cycle and unlink it from other objects or resources invalidating any references in the process. The use of destructors is key to the concept of RAII.

How in C# do you declare such a thing? A finalizer is not automatically invoked when the object is destroyed deterministically. It is nondeterministically called by the GC at some point in the future. Most important, in no way does a finalizer enable the RAII pattern.

Take a FileStream class which encapsulates a resource in C#. Declare a finalizer for it. When an instance of a FileStream class goes out of the scope the finalizer is not called then. You do not know when it is called. Furthermore, even if the GC does a collect and there are no roots in the code to the object, the simple existence of the finalizer counts as a root and can cause the FileStream object to be promoted into the next generation and not collected by the GC until the next collect cycle (who knows when that will be). So while your FileStream object may have gone out of scope ages ago, by not calling Dispose and assuming the finalizer will close the FileStream, you have potentially caused a resource leak. The next function call that assumes the file you previously had used with the FileStream object is closed may throw an exception because you were relying on the garbage collector to nondeterministically release a critical resource.

On the other hand, a destructor in C++/CLI will be called the moment the FileStream goes out of scope (assuming you declared the object with stack semantics and not gcnew). Then in the destructor you make sure there is a call to Dispose and you ensure that the resource is released immediately. You don't need a try/finally and you don't need to explicitly call Dispose and nor does anyone who uses your class in the future. The destructor incurs no performance penalty during allocation or deallocation like a finalizer does either. There's no way to declare such a function in C#.

To say 'I'm not repudiating the mechanics' therefore C# has destructors is completely wrong. The mechanics are exactly what separate a finalizer and a destructor. You can't just say 'oh it has a ~Class() declaration therefore its a destructor even if it works completely differently.' They work in fundamentally different ways. Start declaring finalizers for all your C# classes as if it was a C++ destructor and watch the programs performance and reliability plummet.
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 
GeneralRe: What makes a class IDisposable? Pin
George L. Jackson14-Aug-07 3:48
George L. Jackson14-Aug-07 3:48 
GeneralRe: What makes a class IDisposable? Pin
Mark Salsbery14-Aug-07 5:10
Mark Salsbery14-Aug-07 5:10 
GeneralRe: What makes a class IDisposable? Pin
Mark Salsbery13-Aug-07 4:53
Mark Salsbery13-Aug-07 4:53 
QuestionControl within a control help please [modified] Pin
Xpnctoc11-Aug-07 6:54
Xpnctoc11-Aug-07 6:54 
Questionassignment vs default ctor Pin
swjam11-Aug-07 5:19
swjam11-Aug-07 5:19 
AnswerRe: assignment vs default ctor Pin
Mark Salsbery11-Aug-07 6:55
Mark Salsbery11-Aug-07 6:55 
Questionoperator++(int) Pin
swjam11-Aug-07 4:58
swjam11-Aug-07 4:58 

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.