Re: destructors

Tech-Archive recommends: Fix windows errors by optimizing your registry



Correction: IDisposable!

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
A watched clock never boils.

"Brian Gideon" <briangideon@xxxxxxxxx> wrote in message
news:1130893092.811601.228620@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
> You are correct. The execution of finalizers has a loose guarentee at
> best. .NET 2.0 narrows the circumstances in which the finalizer will
> not run with the addition of constrained execution regions (CERs).
> Take a look at CriticalFinalizerObject and SafeHandle for more
> information.
>
> Brian
>
> Lloyd Dupont wrote:
>> > They are the same thing as the Finalize() function. IOW, they are
>> > called
>> > by the Garbage Collector. And yes, they are guaranteed to be
>> > called...eventually.
>> I'm not sure they are guaranteed to be called, particularly when you exit
>> your program.
>> Anyway if you want to be sure sure your unsused object are called
>> collected
>> and finalized you can call:
>> GC.Collect();
>> GC.WaitForPendingFinalizers();
>>
>> >
>> > This is why the IDispose Interface was created. If you don't want to
>> > wait
>> > around for the GC, you just call Dispose().
>> >
>> Dispose() and Finalize() are 2 different methods called at different
>> time!
>> it's advised to write your code like that:
>>
>> class MyHeavyObject : IDisposable
>> {
>> IntPtr aNativeResource;
>> IDisposable aManagedResource;
>>
>> public MyHeavyObject() { .... }
>>
>> ~MyHeavyObject()
>> {
>> Dispose(false);
>> }
>> public void Dispose()
>> {
>> Dispose(true);
>> }
>> protected virtual bool Dispose(bool disposing)
>> {
>> if( disposing )
>> {
>> aManagedResource.Dispose();
>> }
>>
>> if(aNativeResource != IntPtr.Zero)
>> Free( aNativeResource);
>> aNativeResource = IntPtr.Zero;
>> }
>> }
>


.



Relevant Pages

  • Re: Memory Leak Experts!!!!
    ... you also implement the Finalize method which simply calles the Dispose ... >interface implementation has interfered with the normal function of the GC. ... >Deterministic disposing or implementing the IDispose pattern is only a ...
    (microsoft.public.dotnet.framework.performance)
  • Re: A re-announce on GCs defects
    ... Finalize() as soon as possible if it's been overridden, ... IDispose, or the Disposemethod. ... finalizers on finalizable objects (objects overriding the Finalize() ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Delegates and Events confusion
    ... finalize is unneccessary, and not even fruitful. ... dispose and bypass an expensive finalization. ... >>If you subscribe to an event and do not unsubscribe, the subscriber will ... >>subscriptions that C makes and have C implement IDispose. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Delegates and Events confusion
    ... > When you implement the IDisposable interface, ... > requires you to implement Finalize as well. ... it makes sense to implement IDispose in ... order to dispose of the internal object - but it doesn't make sense to ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: destructors
    ... > by the Garbage Collector. ... > This is why the IDispose Interface was created. ... Dispose() and Finalize() are 2 different methods called at different time! ...
    (microsoft.public.dotnet.languages.csharp)