tags:

views:

40

answers:

3

Should I dispose GDI+ object before its creation?

Is recommended to always Dispose GDI+ object after using it.

by e.g.

Pen p = new Pen(Color.Green);
// use 'p'
p.Dispose();

now, if I have this situation:

Pen p = new Pen(Color.Green);
// use green 'p'

p = new Pen(Color.Red); // Should I Dispose my 'p' first?
// use red 'p'

p.Dispose();

EDIT A:

Using 'USING' is not possible every time.

private Pen p;

public RefreshPen(style)
{
    // p.Dispose(); +-
    p = new Pen(style.Color);
    // etc.
}

EDIT B:

Will this be OK?

using (Pen p = new Pen(Color.Green))
{
    // use green 'p'

    p = new Pen(Color.Red); // Should I Dispose my 'p' first?
    // use red 'p'

    p = new Pen(Color.Blue); // Should I Dispose my 'p' first?
    // use blue 'p'
}
+3  A: 

Yes, you should. (Also note that the using statement is a tremendous help here. I very rarely call .Dispose explicitly. using takes care of that.)

danbystrom
maybe, but see my edit. by the way, you would say that 'using' will help disposing even in multiple consecutive object creations like in (green/red) example?
serhio
+1. I wouldn't have a "RefreshPen" method like that because you it makes it hard to use a "using" statement.
David
@David: What if you have a custom control and you draw(Paint) it with a pen, that should conditionally change a style?
serhio
@serhio: you're edited code is waaay worng, I'm afraid. You should split that into three separate using-statements.
danbystrom
@danbystrom: so what difference between `using(p=new)` and `p=new/p.Dispose`?
serhio
@seriho: the difference is "just" more readable and robust code. But returning to your original question: YES, before setting "p" to a new pen, you *should* .Dispose() the old Pen first.
danbystrom
+1  A: 

Yes. You are creating a new object and assigning it to p, which means you should dispose the old object.

I might code it like so:

using(Pen p = new Pen(Color.Green))
{
    //do some stuff
}

using(Pen q = new Pen(Color.Red))
{
    //do some other stuff
}

This prevents you from forgetting to dispose the pen, or using a disposed object.

dsolimano
maybe, but see my edit.
serhio
@serhio - Not maybe... definitely yes. Even though you are assigning it to the same variable named 'p'... it is an entirely *new object*. The old one must be Disposed properly before doing the new Pen statement, because after you do that, you have lost the reference to the old Pen, and won't ever have the ability to call Dispose on it again.
Nick
@Nick - "maybe" was for using 'using', that can't be combined with a private variable.
serhio
@serhio - Actually, you can use `using` with a `private` variable. It will properly dispose of the `Pen` after the `using` block completes. However, if you attempt to make use of the `Pen` without reinitializing it somewhere else, it will throw an `ObjectDisposedException`.
Nick
+1  A: 

Can't you just change the properties of the object?

Pen p = new Pen(Color.Green);
//use it here.
p.Color = Color.Red;
//use it some more.
p.Dispose();
Jacob G
yes. you have reason, finally, I adopted this "strategy". Just as I formulated the question this is not an answer :)
serhio