about dispose

T

Tony Johansson

When I want to dispose the Image(theImage) which of these two methods namned
1 and 2 below is best practice to use ?
Both will dispose the Image but they use somewhat different ways.

public partial class Form1 : Form
{
private Image theImage;
public Form1()
{
InitializeComponent();
SetStyle(ControlStyles.Opaque, true);
theImage = new Bitmap("logo.gif");
1 theImage.Dispose();
}
}


protected override void Dispose(bool disposing)
{
2 if (disposing)
2 {
2 theImage.Dispose();
2 }


if (disposing && (components != null))
{
components.Dispose();
}

base.Dispose(disposing);
}

//Tony
 
Z

Zach

Tony Johansson said:
When I want to dispose the Image(theImage) which of these two methods
namned 1 and 2 below is best practice to use ?
Both will dispose the Image but they use somewhat different ways.
Why not set it to null, if you must.
 
W

Willem van Rumpt

public partial class Form1 : Form
{
private Image theImage;
public Form1()
{
InitializeComponent();
SetStyle(ControlStyles.Opaque, true);
theImage = new Bitmap("logo.gif");
1 theImage.Dispose();
}
}

This doesn't make any sense. You're creating and releasing the image in
the constructor without ever using it.
protected override void Dispose(bool disposing)
{
2 if (disposing)
2 {
2 theImage.Dispose();
2 }


if (disposing&& (components != null))
{
components.Dispose();
}

base.Dispose(disposing);
}

This is more like it, although you might want include a null check for
"theImage".

In general, it's good practise to dispose as early as possible.

Given the limited amount of code you've shown, and assuming that
"theImage" has to hang around for the lifetime of the form, the second
method is OK (with the possible addition of a null check for "theImage").
 
W

Willem van Rumpt

The code as written cannot find the variable set to null. Of course, if
there is other code that does set the variable to null, then checking
for null would be useful. But otherwise, a check for null is just
wasted, misleading code (misleading, because its mere presence implies
that the variable _could_ be null).

I didn't see the constructor and the overriden Dispose as being
connected. But if they are, then yes, it can't be null, and I agree
completely.
And if the object doesn't have to have a lifetime equal to that of the
form, the question remains: why would it be an instance field in the
first place?

It might be initialized outside the constructor, defaulting to null if
never used. Plenty of possible scenarios.
 
Z

Zach

Peter Duniho said:
Setting the reference to null doesn't dispose it. One should definitely
_not_ set the variable to null until one has called Dispose().
<snipped>

Pete, would you oppose this?

InfraStructure.Routines.currentForm is a static that keeps
track of the currently open form, belonging to a set of forms that
are made use of in no particular order.
if (InfraStructure.Routines.currentForm != null)

{

InfraStructure.Routines.currentForm.Close();

InfraStructure.Routines.currentForm = null;

}

Zach.
 
T

Tony Johansson

Peter Duniho said:
[...]
And if the object doesn't have to have a lifetime equal to that of the
form, the question remains: why would it be an instance field in the
first place?

It might be initialized outside the constructor, defaulting to null if
never used. Plenty of possible scenarios.

I took the two snippets of code as connected to each other. My rhetorical
question should be understood in that view. I agree that if we take the
Dispose(bool) method as completely independent of the constructor (i.e.
they are from two completely different classes), then one could have a
null reference.

I mean that this theImage.Dispose();
should be in OnPaint instead and not in the C-tor it was a typo

public partial class Form1 : Form
{
private Image theImage;
public Form1()
{
InitializeComponent();
SetStyle(ControlStyles.Opaque, true);
theImage = new Bitmap("logo.gif");
1 theImage.Dispose();
}
}
//Tony
 
Z

Zach

Peter Duniho said:
That depends on what the Close() method does.

If it follows the standard .NET convention and is identical to calling
Dispose(), then the above seems fine to me, at least out of context.

(There are of course still the question as to when this code is executed,
whether it really needs to set the variable to null at that point, and why
you've got a static reference to a form instance in the first place. But
taking as granted all of those questions have good answers supporting the
existence of the code, the code on its own seems fine).

This is the context:

I use a backdrop upon which, depending on the function being
used, different forms are placed, so the form actually in use
will be likely to differ at the point in time when the backdrop
is minimized.

#region Minimalizing routine.
void MainForm_SizeChanged(object sender, EventArgs e)
{
if (WindowState == FormWindowState.Minimized)
{
if (InfraStructure.Routines.currentForm != null)
{
InfraStructure.Routines.currentForm.Close();
InfraStructure.Routines.currentForm = null;
}
}
}
#endregion
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top