Click here to Skip to main content
15,894,405 members
Home / Discussions / C#
   

C#

 
GeneralRe: A Comprehensive Logging Package for .NET Pin
Heath Stewart28-Jul-04 6:39
protectorHeath Stewart28-Jul-04 6:39 
GeneralRe: A Comprehensive Logging Package for .NET Pin
pat27088128-Jul-04 6:53
pat27088128-Jul-04 6:53 
GeneralRe: A Comprehensive Logging Package for .NET Pin
Heath Stewart28-Jul-04 6:59
protectorHeath Stewart28-Jul-04 6:59 
GeneralRe: A Comprehensive Logging Package for .NET Pin
Giles28-Jul-04 21:00
Giles28-Jul-04 21:00 
GeneralRe: A Comprehensive Logging Package for .NET Pin
pat27088128-Jul-04 22:26
pat27088128-Jul-04 22:26 
GeneralMemory leaks Pin
blankg28-Jul-04 5:37
blankg28-Jul-04 5:37 
GeneralRe: Memory leaks Pin
Nick Parker28-Jul-04 6:27
protectorNick Parker28-Jul-04 6:27 
GeneralRe: Memory leaks Pin
Heath Stewart28-Jul-04 6:37
protectorHeath Stewart28-Jul-04 6:37 
For any object that implements IDisposable, you should always dispose of the object when finished. This includes (and definitely is not limited to) Bitmap and Graphics. In C#, you can use the using statement to make sure that objects are disposed even in case an exception is thrown:
using (Bitmap bmp = new Bitmap(32, 32))
{
  using (Graphics g = Graphics.FromImage(bmp))
  {
    // Draw on the bitmap
  } // g.Dispose() gets called no matter what
  bmp.Save(...); // Save the bitmap
} // bmp.Dispose() gets called no matter what
This ensures that unmanaged resource (resources that aren't managed by the CLR, like native handles) are freed and that managed objects are disposed of immediately instead of the next time the GC (garbage collector) is run by the CLR.

On a note, if you call Environment.Exit finalizers (like destructors in C++...sort of) are executed but IDisposable.Dispose is not. This really isn't too important since Environment.Exit will unload the CLR and exit the host process - the OS will free all handles in use at this time. If any exceptions are thrown or you return execution to the caller, using the using statement will still dispose the object. The following is equivalent:
using (Bitmap bmp = new Bitmap(32, 32))
{
  // Use 'bmp'
}
// EQUALS
Bitmap bmp = new Bitmap(32, 32);
try
{
  // Use 'bmp'
}
finally
{
  bmp.Dispose();
}


 

Microsoft MVP, Visual C#
My Articles
GeneralIL code generation on the fly Pin
SOCM_FP_CPP28-Jul-04 2:15
SOCM_FP_CPP28-Jul-04 2:15 
GeneralRe: IL code generation on the fly Pin
Nick Parker28-Jul-04 3:12
protectorNick Parker28-Jul-04 3:12 
GeneralToolbox Visibility Question - Custom Controls Pin
ddelapasse28-Jul-04 1:39
ddelapasse28-Jul-04 1:39 
GeneralRe: Toolbox Visibility Question - Custom Controls Pin
leppie28-Jul-04 3:13
leppie28-Jul-04 3:13 
GeneralRe: Toolbox Visibility Question - Custom Controls Pin
Heath Stewart28-Jul-04 4:54
protectorHeath Stewart28-Jul-04 4:54 
GeneralRe: Toolbox Visibility Question - Custom Controls Pin
Nick Parker28-Jul-04 4:53
protectorNick Parker28-Jul-04 4:53 
Questionhowto generalise compareoperations? Pin
Stephan Wright28-Jul-04 1:34
Stephan Wright28-Jul-04 1:34 
AnswerRe: howto generalise compareoperations? Pin
Daniel Turini28-Jul-04 1:53
Daniel Turini28-Jul-04 1:53 
GeneralRe: howto generalise compareoperations? Pin
Stephan Wright28-Jul-04 2:16
Stephan Wright28-Jul-04 2:16 
QuestionDo MS Collection Class Enumerators use strong reference to keep collection alive? Pin
Paul Evans28-Jul-04 1:29
Paul Evans28-Jul-04 1:29 
AnswerRe: Do MS Collection Class Enumerators use strong reference to keep collection alive? Pin
Daniel Turini28-Jul-04 1:55
Daniel Turini28-Jul-04 1:55 
GeneralLicense Key Create / Validate for windows Application Pin
wcoods28-Jul-04 0:42
wcoods28-Jul-04 0:42 
GeneralRe: License Key Create / Validate for windows Application Pin
Heath Stewart28-Jul-04 4:39
protectorHeath Stewart28-Jul-04 4:39 
GeneralFinding File names from an Executable Pin
Lee Majors27-Jul-04 23:19
sussLee Majors27-Jul-04 23:19 
GeneralRe: Finding File names from an Executable Pin
Colin Angus Mackay28-Jul-04 0:17
Colin Angus Mackay28-Jul-04 0:17 
GeneralRe: Finding File names from an Executable Pin
Lee C Baker28-Jul-04 0:56
Lee C Baker28-Jul-04 0:56 
GeneralRe: Finding File names from an Executable Pin
Heath Stewart28-Jul-04 4:19
protectorHeath Stewart28-Jul-04 4:19 

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.