Click here to Skip to main content
15,897,371 members
Home / Discussions / C#
   

C#

 
GeneralUDP broadcast problem Pin
smartnose19-Apr-03 15:34
smartnose19-Apr-03 15:34 
GeneralDynamically move ScrollBar to bottom in a TextBox or a RichTextBox Pin
krisp19-Apr-03 14:00
krisp19-Apr-03 14:00 
GeneralRe: Dynamically move ScrollBar to bottom in a TextBox or a RichTextBox Pin
J. Dunlap19-Apr-03 14:12
J. Dunlap19-Apr-03 14:12 
GeneralRe: Dynamically move ScrollBar to bottom in a TextBox or a RichTextBox Pin
krisp19-Apr-03 16:11
krisp19-Apr-03 16:11 
GeneralRe: Dynamically move ScrollBar to bottom in a TextBox or a RichTextBox Pin
J. Dunlap19-Apr-03 17:03
J. Dunlap19-Apr-03 17:03 
GeneralGC - again Pin
peterchen19-Apr-03 12:58
peterchen19-Apr-03 12:58 
GeneralRe: GC - again Pin
Paul Ingles19-Apr-03 14:00
Paul Ingles19-Apr-03 14:00 
GeneralRe: GC - again Pin
James T. Johnson19-Apr-03 14:02
James T. Johnson19-Apr-03 14:02 
peterchen wrote:
However, I'm still mixed up about Dispose, Finalize, and the recommended Dispose(bool disposing).

Finalize is the closest equivalent to C++ destructors that .NET has. The difference is that in .NET you can only clean up ValueType-based variables, in other words, IntPtr's that represent handles for P/Invoke use. This is because there is no guarantee on the order in which reference based (ie non-value types) will be Finalized.

That isn't very helpful in the long run, so the IDisposable interface came about so you could free up your managed resources. For example, flush a FileStream then close it. Because the FileStream uses a managed byte array to buffer the data you cannot use that byte array within the Finalize method. Sticky sticky.

Now to get to your question Wink | ;)

When Finalize is called we know we can only deal with unmanaged resources, but when Dispose is called we can deal with both. Rather than have two places to dispose of unmanaged resources MS came up with another pattern to use, this time it is a protected method that takes a bool.

protected void Dispose(bool disposing)

Both Finalize and the public Dispose methods should call this method to do the dirty work work. The bool tells if you can dispose of managed resources (ie Dispose was called). If it is false then you should only get rid of unmanaged resources.

Now to tie it all together:

class MyClass : IDisposable
{ 
  Brush myBrush;
  IntPtr myHandle;
 
  ...
 
  ~MyClass 
  {
    Dispose(false);
  }
  
  public void Dispose()
  {
    Dispose(true);
    GC.SuppressFinalize(this); // No need to call finalize now
  } 
 
  protected void Dispose(bool disposing)
  {
    if( disposing )
    {
      if( myBrush != null )
        myBrush.Dispose();

      myBrush = null;
    }
 
    // Dispose of unmanaged resources here... like
    FreeSuppaHandle(myHandle);
    // Fake function call of course :)
 
    // Last but not least propgate the Dispose call if needed
    // Dispose(disposing)
  }
}
HTH,

James

"It is self repeating, of unknown pattern"
Data - Star Trek: The Next Generation

GeneralMaking class properties editable Pin
peterchen19-Apr-03 12:19
peterchen19-Apr-03 12:19 
GeneralRe: Making class properties editable Pin
James T. Johnson19-Apr-03 12:56
James T. Johnson19-Apr-03 12:56 
GeneralRe: Making class properties editable Pin
peterchen19-Apr-03 13:10
peterchen19-Apr-03 13:10 
GeneralPrefixing Classes and Interfaces Pin
J. Dunlap19-Apr-03 11:47
J. Dunlap19-Apr-03 11:47 
GeneralRe: Prefixing Classes and Interfaces Pin
dog_spawn19-Apr-03 12:11
dog_spawn19-Apr-03 12:11 
GeneralRe: Prefixing Classes and Interfaces Pin
J. Dunlap19-Apr-03 12:32
J. Dunlap19-Apr-03 12:32 
GeneralRe: Prefixing Classes and Interfaces Pin
James T. Johnson19-Apr-03 13:00
James T. Johnson19-Apr-03 13:00 
GeneralRe: Prefixing Classes and Interfaces Pin
J. Dunlap19-Apr-03 14:04
J. Dunlap19-Apr-03 14:04 
GeneralRe: Prefixing Classes and Interfaces Pin
Arun Bhalla19-Apr-03 13:32
Arun Bhalla19-Apr-03 13:32 
GeneralRe: Prefixing Classes and Interfaces Pin
J. Dunlap19-Apr-03 14:02
J. Dunlap19-Apr-03 14:02 
GeneralWrappers for P/Invoke Pin
Alex Korchemniy19-Apr-03 8:27
Alex Korchemniy19-Apr-03 8:27 
GeneralRe: Wrappers for P/Invoke Pin
Stephane Rodriguez.19-Apr-03 9:13
Stephane Rodriguez.19-Apr-03 9:13 
GeneralRe: Wrappers for P/Invoke Pin
Alex Korchemniy19-Apr-03 15:34
Alex Korchemniy19-Apr-03 15:34 
GeneralRe: Wrappers for P/Invoke Pin
Stephane Rodriguez.19-Apr-03 20:58
Stephane Rodriguez.19-Apr-03 20:58 
QuestionHwo to use SecondaryBuffer to play sound through Buffer? Pin
JmmJ19-Apr-03 1:02
JmmJ19-Apr-03 1:02 
AnswerRe: Hwo to use SecondaryBuffer to play sound through Buffer? Pin
leppie19-Apr-03 5:22
leppie19-Apr-03 5:22 
GeneralRe: Hwo to use SecondaryBuffer to play sound through Buffer? Pin
Stephane Rodriguez.19-Apr-03 9:06
Stephane Rodriguez.19-Apr-03 9: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.