Why I have this error when I try to dispose a resource in the finally statment of a try block?

I have some problem disposing a resource.

I heve this code:

    class ChartHelper
    {

        //public static System.Drawing.Image GetPdfChart(int percentage)
        public static System.Drawing.Bitmap GetPdfChart(int percentage)
        {
            if (percentage == 0)
            {
                return null;
            }

            int WIDTH = 130;
            int HEIGHT = 10;


            //using (Bitmap bitmap = new Bitmap(WIDTH, HEIGHT))
            //{
            Bitmap bitmap;
            try {
                bitmap = new Bitmap(WIDTH, HEIGHT);
                using (Graphics graphics = Graphics.FromImage(bitmap))
                {
                    using (LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, WIDTH, HEIGHT), Color.LightGreen, Color.Red, LinearGradientMode.Horizontal))
                    {
                        graphics.FillRectangle(brush, new Rectangle(0, 0, WIDTH, HEIGHT));

                        using (Bitmap target = new Bitmap(WIDTH * percentage / 100, HEIGHT))
                        {
                            Rectangle cropped = new Rectangle(0, 0, WIDTH, HEIGHT);

                            using (Graphics g = Graphics.FromImage(target))
                            {
                                g.DrawImage(bitmap, new Rectangle(0, 0, cropped.Width, cropped.Height), cropped, GraphicsUnit.Pixel);
                                //g.Save();
                                //String filename = Path.GetTempFileName() + ".png";
                                //target.Save(filename);
                                //return filename;

                                return bitmap;
                            }
                        }
                    }
                }
            }
            finally
            {
               bitmap.Dispose(); 
            }
        }

    }

As you can see at the beginning I create a BitMap object, in this way:

bitmap = new Bitmap(WIDTH, HEIGHT);

into a try block.

At the end of the block I have the finaly in wich I try to dispose the resource:

finally
{
    bitmap.Dispose(); 
}

But here it give me the following error message:

Error 2 Use of unassigned local variable 'bitmap'

Why? What can I do to solve it? (I don't want to use the using statment)

Tnx

Jon Skeet
people
quotationmark

Why?

Because bitmap isn't definitely assigned, according to the rules of the language specification. Imagine if the Bitmap constructor threw an exception. What would you dispose?

What can I do to solve it? (I don't want to use the using statment)

Well you can use:

Bitmap bitmap = null;
try
{
    bitmap = ...;
    // Code using bitmap
}
finally
{
    if (bitmap != null)
    {
        bitmap.Dispose();
    }
}

However, I would strongly advise you to use a using statement for this. It's what it's designed for, and it's the idiomatic approach.

As a slightly separate matter, you're returning the bitmap from the middle of the block - but you're disposing it. Do you really want to return a reference to a disposed bitmap? What do you expect the caller to be able to do with it? You might want to consider only disposing the bitmap on failure. e.g.

Bitmap bitmap = null;
bool success = false;
try
{
    bitmap = ...;
    // Code using bitmap
    ...
    success = true;
    return bitmap;
}
finally
{
    if (bitmap != null && !success)
    {
        bitmap.Dispose();
    }
}

At this point of course you do have a good reason not to use a using statement...

people

See more on this question at Stackoverflow