You're right...silly mistake on my part.

BTW, I know that closing the stream in the finalizer is not a good
practice.  I put the close in the finalizer to see if I was leaking
handles (and that was the reason for the missing finalizer calling.)
Of course I would never do this in production code. : )


Thanks,

Shawn Wildermuth
[EMAIL PROTECTED]
http://adoguy.com
http://adoguy.com/book
http://shawnwildermuth.com


> -----Original Message-----
> From: Moderated discussion of advanced .NET topics.
> [mailto:[EMAIL PROTECTED]] On Behalf Of
> Jeroen Frijters
> Sent: Thursday, September 19, 2002 3:48 AM
> To: [EMAIL PROTECTED]
> Subject: Re: [ADVANCED-DOTNET] Missing Finalizers
>
>
> The finalizers start running before you've finished the look,
> and they decrement Class1.iterations which is used in the
> loop, thus you're allocating less than 1000 objects.
>
> Also, it is *very* bad practice to close the filestream in
> the finalizer. You only ever should release *unmanaged*
> resources from your finalizer. The finalizer of filestream
> will take care of closing the file (and it may in fact have
> already done that before your finalizer runs).
>
> Regards,
> Jeroen
>
> > -----Original Message-----
> > From: Moderated discussion of advanced .NET topics.
> > [mailto:[EMAIL PROTECTED]] On Behalf Of Shawn
> > Wildermuth
> > Sent: Thursday, September 19, 2002 03:53
> > To: [EMAIL PROTECTED]
> > Subject: [ADVANCED-DOTNET] Missing Finalizers
> >
> >
> > Hopefully I am missing something.  I have this small example of
> > finalizers not being called as a process is being torn
> down.[1]  Does
> > anyone know if this is expected behavior?
> >
> > Thanks,
> >
> > Shawn Wildermuth
> > [EMAIL PROTECTED]
> > http://adoguy.com
> > http://adoguy.com/book
> > http://shawnwildermuth.com
> >
> > [1]:
> >
> > // test.cs
> >
> > // Definition to add or remove the
> > // stream reference (which causes
> > // some of the finalizers to
> > // be lost)
> > #define ADDSTREAM
> >
> > using System;
> > using System.IO;
> >
> > namespace TestMissingFinalizers
> > {
> >   class Class1
> >   {
> >     static public int iterations = 1000;
> >
> >     [STAThread]
> >     static void Main(string[] args)
> >     {
> >       for (int x = 0; x < iterations; ++x)
> >       {
> >         // Create a new Instance
> >         // to be collected as soon
> >         // as possible
> >         new Foo(x);
> >       }
> >     }
> >   }
> >
> >   public class Foo
> >   {
> >     public Foo(int number)
> >     {
> > #if ADDSTREAM
> >       // Open a Text Stream
> >       // This will cause theFinalizer to not be
> >       // called on every instance
> >       string fileName = string.Format("foo{0}.txt", number);
> >       strm = new FileStream(fileName, FileMode.Create); #endif
> >     }
> >
> > #if ADDSTREAM
> >     private FileStream strm;
> > #endif
> >
> >     ~Foo()
> >     {
> > #if ADDSTREAM
> >       // If we close the stream
> >       strm.Close();
> > #endif
> >       // Write out to see our results
> >       // Write out the number of collections
> >       // that happen
> >       Console.WriteLine("Number Uncollected: {0}",
> >                         --Class1.iterations);
> >     }
> >   }
> > }
> >
> > You can read messages from the Advanced DOTNET archive, unsubscribe
> > from Advanced DOTNET, or subscribe to other DevelopMentor lists at
> > http://discuss.develop.com.
> >
>
> You can read messages from the Advanced DOTNET archive,
> unsubscribe from Advanced DOTNET, or subscribe to other
> DevelopMentor lists at http://discuss.develop.com.
>

You can read messages from the Advanced DOTNET archive, unsubscribe from Advanced 
DOTNET, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.

Reply via email to