-4

What happens to the image in "rendered" once using block disposes "b"?

Bitmap rendered;

using(Bitmap b = new Bitmap(calcHeight, calcWidth))
{
   using (Graphics g = Graphics.FromImage(b))
   {
        RenderMyBitMap(ref b);
        rendered = b;
   }
}
//Outside Using block
DoSomeThingElseWith(rendered);
  • 3
    Why don't you give it a try and see for yourself? – Broots Waymb May 01 '17 at 20:51
  • Because there is still a reference to `b` it will not be garage collected. When `rendered` is set to null, the next cycle of garage collection will take it away. – Black Frog May 01 '17 at 20:54
  • I did not see any error. Could not find if I am filling up memory (an OOM exception waiting to happen) or it is safe. Black Frog answer seems to be justified. But cannot check it. – Venkata Goli May 01 '17 at 20:58

1 Answers1

0

The variable rendered points to the same instance as b. If you attempt to access it after the using block, you'll get an ObjectDisposedException, which is

The exception that is thrown when an operation is performed on a disposed object.

John Wu
  • 50,556
  • 8
  • 44
  • 80