PC Review


Reply
Thread Tools Rate Thread

about dispose

 
 
Tony Johansson
Guest
Posts: n/a
 
      29th Mar 2011
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


 
Reply With Quote
 
 
 
 
Zach
Guest
Posts: n/a
 
      29th Mar 2011
"Tony Johansson" <(E-Mail Removed)> wrote in message
news:imsa1p$sol$(E-Mail Removed)...
> 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.

 
Reply With Quote
 
Willem van Rumpt
Guest
Posts: n/a
 
      29th Mar 2011
On 29-Mar-11 11:50, Tony Johansson wrote:

>
> 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").

--
Willem van Rumpt
 
Reply With Quote
 
Willem van Rumpt
Guest
Posts: n/a
 
      29th Mar 2011
On 29-Mar-11 18:12, Peter Duniho wrote:
> On 3/29/11 8:39 AM, Willem van Rumpt wrote:
>
>>> 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".

>
> 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.

--
Willem van Rumpt
 
Reply With Quote
 
Zach
Guest
Posts: n/a
 
      29th Mar 2011

"Peter Duniho" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> On 3/29/11 4:31 AM, Zach wrote:
>> "Tony Johansson" <(E-Mail Removed)> wrote in message
>> news:imsa1p$sol$(E-Mail Removed)...
>>> 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.

>
> 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.

 
Reply With Quote
 
Tony Johansson
Guest
Posts: n/a
 
      29th Mar 2011

"Peter Duniho" <(E-Mail Removed)> skrev i meddelandet
news:Vo-(E-Mail Removed)...
> On 3/29/11 10:03 AM, Willem van Rumpt wrote:
>> [...]
>>> 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


 
Reply With Quote
 
Zach
Guest
Posts: n/a
 
      30th Mar 2011

"Peter Duniho" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> On 3/29/11 1:21 PM, Zach wrote:
>> 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;
>>
>> }

>
> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Does DataGridView.Dispose Call DataGridViewRow.Dispose? eBob.com Microsoft VB .NET 13 31st Jul 2009 02:08 PM
Dispose Pattern -- why void Dispose() is nonvirtual? gz Microsoft Dot NET 2 24th Jan 2008 08:18 PM
Dispose, Dispose(true), Finalize, IDisposable tascien@gmail.com Microsoft Dot NET 1 27th Jul 2007 01:01 AM
Form.Dispose does not invoke InputPanel.Dispose tomj@softhome.net Microsoft Dot NET Compact Framework 3 24th Jan 2006 03:28 PM
the difference betweenSqlConnection.IDisposable.Dispose() andSqlConnection.Dispose(). tangyong Microsoft Dot NET Framework 1 20th Jan 2006 04:53 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 05:56 PM.