Re: destructors
- From: "Kevin Spencer" <kevin@xxxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Wed, 2 Nov 2005 10:14:40 -0500
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;
>> }
>> }
>
.
- References:
- Re: destructors
- From: Lloyd Dupont
- Re: destructors
- Prev by Date: Re: Large image load error that do work in C++
- Next by Date: Re: destructors
- Previous by thread: Re: destructors
- Next by thread: howto copy data from one Access database to another
- Index(es):
Relevant Pages
|