Dispose Called for Temporary Objects? 
Author Message
 Dispose Called for Temporary Objects?

Am I correct in assuming that Dispose will not be called on disposable objects that are created as parameters (at least not until finalisation)? Here's an example of what I mean:

// Cached bitmap.
Stream stream = SomeClass.GetStream();
Bitmap bmp = new Bitmap(stream);
CustomImage custom = AnotherClass.ConvertImage(bmp);
bmp.Dispose();
stream.Close();

// Uncached bitmap.
Stream stream = SomeClass.GetStream();
CustomImage custom = AnotherClass.ConvertImage(new Bitmap(stream));
stream.Close();

Looking at the IL for both cases suggests that there is no implicit Dispose call on the bitmap in the second case, which means that this type of call should generally be avoided. Is that right?

--
Kevin Westhead



Mon, 23 May 2005 23:53:57 GMT  
 Dispose Called for Temporary Objects?
Kevin,

    Yes, you are right, you should avoid this when dealing with classes
implementing IDispose.

    Hope this helps.

--
               - Nicholas Paldino [.NET/C# MVP]


Am I correct in assuming that Dispose will not be called on disposable
objects that are created as parameters (at least not until finalisation)?
Here's an example of what I mean:

// Cached bitmap.
Stream stream = SomeClass.GetStream();
Bitmap bmp = new Bitmap(stream);
CustomImage custom = AnotherClass.ConvertImage(bmp);
bmp.Dispose();
stream.Close();

// Uncached bitmap.
Stream stream = SomeClass.GetStream();
CustomImage custom = AnotherClass.ConvertImage(new Bitmap(stream));
stream.Close();

Looking at the IL for both cases suggests that there is no implicit Dispose
call on the bitmap in the second case, which means that this type of call
should generally be avoided. Is that right?

--
Kevin Westhead



Tue, 24 May 2005 00:03:22 GMT  
 Dispose Called for Temporary Objects?

It would be better to do:

using (Stream stream = SomeClass.GetStream()) {
   using (Bitmap bmp = new Bitmap(stream)) {
      CustomImage custom = AnotherClass.ConvertImage(bmp);
   }

Quote:
}

"using" disposes for you.

-- Alan


  Am I correct in assuming that Dispose will not be called on disposable objects that are created as parameters (at least not until finalisation)? Here's an example of what I mean:

  // Cached bitmap.
  Stream stream = SomeClass.GetStream();
  Bitmap bmp = new Bitmap(stream);
  CustomImage custom = AnotherClass.ConvertImage(bmp);
  bmp.Dispose();
  stream.Close();

  // Uncached bitmap.
  Stream stream = SomeClass.GetStream();
  CustomImage custom = AnotherClass.ConvertImage(new Bitmap(stream));
  stream.Close();

  Looking at the IL for both cases suggests that there is no implicit Dispose call on the bitmap in the second case, which means that this type of call should generally be avoided. Is that right?

  --
  Kevin Westhead



Tue, 24 May 2005 00:16:02 GMT  
 Dispose Called for Temporary Objects?

Hi, Kevin
   The compiler would never generate Dispose function call for you, unless you write either a using statement or call it explicitly. The IDisposable interface is only a programming contract rather than some CLR mechanism: The one who implements the class should implement this interface. While the one who uses the class object should call the method defined by the interface. For CLR, there isn't anything special for IDisposable, it's simply a normal interface.

   Hope this helps.
   Ming Chen [.NET MVP]

  Am I correct in assuming that Dispose will not be called on disposable objects that are created as parameters (at least not until finalisation)? Here's an example of what I mean:

  // Cached bitmap.
  Stream stream = SomeClass.GetStream();
  Bitmap bmp = new Bitmap(stream);
  CustomImage custom = AnotherClass.ConvertImage(bmp);
  bmp.Dispose();
  stream.Close();

  // Uncached bitmap.
  Stream stream = SomeClass.GetStream();
  CustomImage custom = AnotherClass.ConvertImage(new Bitmap(stream));
  stream.Close();

  Looking at the IL for both cases suggests that there is no implicit Dispose call on the bitmap in the second case, which means that this type of call should generally be avoided. Is that right?

  --
  Kevin Westhead



Tue, 24 May 2005 00:27:53 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. disposing of all objects in object pool

2. destructor is called before copy-constructor on temporary class (allocated on stack during function call)

3. To Dispose() or not to Dispose()

4. Managed Class Containing Unmanaged Objects Does Not Dispose

5. Need to unsubscribe from event on disposing object?

6. Wishlist: Checking for missing dispose calls.

7. Cannot dispose wrapped Excel Library object - Please Help

8. How to check the object is disposed

9. compiler bug: destructor for temporary in constructor initializer never called

10. Temporary objects

11. Temporary Created Objects

12. VC++6.0 bug: Failure to destroy temporary objects

 

 
Powered by phpBB® Forum Software