PC Review


Reply
Thread Tools Rate Thread

Dispose then set to nothing

 
 
=?Utf-8?B?SGFvIEppYW5n?=
Guest
Posts: n/a
 
      26th Feb 2007
Hi,

I see in many MS example code that looks like this

Public Sub Foo()
dim myCustomer as New Customer
...
myCustomer.Dispose()
myCustomer = Nothing
End Sub

I'm wondering if setting myCustomer = Nothing is necessary in this case?
Since myCustomer is a private variable, once it goes out of scope at the next
line, the reference to the object should be released.

Thanks.

Hao
 
Reply With Quote
 
 
 
 
=?UTF-8?B?R8O2cmFuIEFuZGVyc3Nvbg==?=
Guest
Posts: n/a
 
      27th Feb 2007
Hao Jiang wrote:
> Hi,
>
> I see in many MS example code that looks like this
>
> Public Sub Foo()
> dim myCustomer as New Customer
> ...
> myCustomer.Dispose()
> myCustomer = Nothing
> End Sub
>
> I'm wondering if setting myCustomer = Nothing is necessary in this case?
> Since myCustomer is a private variable, once it goes out of scope at the next
> line, the reference to the object should be released.
>
> Thanks.
>
> Hao


That is correct. Removing the reference is pointless in this case.

--
Göran Andersson
_____
http://www.guffa.com
 
Reply With Quote
 
Herfried K. Wagner [MVP]
Guest
Posts: n/a
 
      27th Feb 2007
"Hao Jiang" <Hao (E-Mail Removed)> schrieb:
> Public Sub Foo()
> dim myCustomer as New Customer
> ...
> myCustomer.Dispose()
> myCustomer = Nothing
> End Sub
>
> I'm wondering if setting myCustomer = Nothing is necessary in this case?


No, it actually isn't necessary for the reason you mentioned.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
 
Reply With Quote
 
Jay B. Harlow [MVP - Outlook]
Guest
Posts: n/a
 
      27th Feb 2007
Hao Jiang,
As Göran & Herfried suggests setting myCustomer = Nothing is not needed for
the reason you give.

Instead of calling Dispose outright I would recommend using the new Using
statement in .NET 2.0 (VS 2005). The Using statement ensures that Dispose is
called even if one of the statements contained by the Using throws an
exception.

> Public Sub Foo()

Using myCustomer as New Customer
> ...

End Using
> End Sub


Which is basically the following in .NET 1.x:

Dim myCustomer As New Customer
Try
DoSomething()
Finally
If myCustomer IsNot Nothing Then
myCustomer.Dispose()
End If
End Try

--
Hope this helps
Jay B. Harlow [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


"Hao Jiang" <Hao (E-Mail Removed)> wrote in message
news:4551F173-DAB8-4F74-AE61-(E-Mail Removed)...
> Hi,
>
> I see in many MS example code that looks like this
>
> Public Sub Foo()
> dim myCustomer as New Customer
> ...
> myCustomer.Dispose()
> myCustomer = Nothing
> End Sub
>
> I'm wondering if setting myCustomer = Nothing is necessary in this case?
> Since myCustomer is a private variable, once it goes out of scope at the
> next
> line, the reference to the object should be released.
>
> Thanks.
>
> Hao


 
Reply With Quote
 
Cor Ligthert [MVP]
Guest
Posts: n/a
 
      27th Feb 2007
Hao Jiang,

Do you mean with MS samples, samples made by Micrsoft, if so can you than
show us some of those?
(URL's)

Because it is not correct, it is as
dim a as integer = 1 + 1
if a <> 2 then messagebox.show "there was a calculation error"

Thanks in advance,

Cor

"Jay B. Harlow [MVP - Outlook]" <(E-Mail Removed)> schreef in
bericht news:(E-Mail Removed)...
> Hao Jiang,
> As Göran & Herfried suggests setting myCustomer = Nothing is not needed
> for the reason you give.
>
> Instead of calling Dispose outright I would recommend using the new Using
> statement in .NET 2.0 (VS 2005). The Using statement ensures that Dispose
> is called even if one of the statements contained by the Using throws an
> exception.
>
>> Public Sub Foo()

> Using myCustomer as New Customer
>> ...

> End Using
>> End Sub

>
> Which is basically the following in .NET 1.x:
>
> Dim myCustomer As New Customer
> Try
> DoSomething()
> Finally
> If myCustomer IsNot Nothing Then
> myCustomer.Dispose()
> End If
> End Try
>
> --
> Hope this helps
> Jay B. Harlow [MVP - Outlook]
> .NET Application Architect, Enthusiast, & Evangelist
> T.S. Bradley - http://www.tsbradley.net
>
>
> "Hao Jiang" <Hao (E-Mail Removed)> wrote in message
> news:4551F173-DAB8-4F74-AE61-(E-Mail Removed)...
>> Hi,
>>
>> I see in many MS example code that looks like this
>>
>> Public Sub Foo()
>> dim myCustomer as New Customer
>> ...
>> myCustomer.Dispose()
>> myCustomer = Nothing
>> End Sub
>>
>> I'm wondering if setting myCustomer = Nothing is necessary in this case?
>> Since myCustomer is a private variable, once it goes out of scope at the
>> next
>> line, the reference to the object should be released.
>>
>> Thanks.
>>
>> Hao

>



 
Reply With Quote
 
aaron.kempf@gmail.com
Guest
Posts: n/a
 
      27th Feb 2007
yeah; in vb6 when a variable went out of scope; it automagically
cleaned up after itself


except for DAO-- (which MS just ressurrected)









On Feb 26, 3:30 pm, Hao Jiang <Hao J...@discussions.microsoft.com>
wrote:
> Hi,
>
> I see in many MS example code that looks like this
>
> Public Sub Foo()
> dim myCustomer as New Customer
> ...
> myCustomer.Dispose()
> myCustomer = Nothing
> End Sub
>
> I'm wondering if setting myCustomer = Nothing is necessary in this case?
> Since myCustomer is a private variable, once it goes out of scope at the next
> line, the reference to the object should be released.
>
> Thanks.
>
> Hao



 
Reply With Quote
 
=?Utf-8?B?SGFvIEppYW5n?=
Guest
Posts: n/a
 
      27th Feb 2007
Thanks Goran, Hefried and Jay! That's what I thought.

Cor, please check out the 2nd and 3rd code section on this MS page:
http://support.microsoft.com/kb/888168

Hao

"Cor Ligthert [MVP]" wrote:

> Hao Jiang,
>
> Do you mean with MS samples, samples made by Micrsoft, if so can you than
> show us some of those?
> (URL's)
>
> Because it is not correct, it is as
> dim a as integer = 1 + 1
> if a <> 2 then messagebox.show "there was a calculation error"
>
> Thanks in advance,
>
> Cor
>
> "Jay B. Harlow [MVP - Outlook]" <(E-Mail Removed)> schreef in
> bericht news:(E-Mail Removed)...
> > Hao Jiang,
> > As Göran & Herfried suggests setting myCustomer = Nothing is not needed
> > for the reason you give.
> >
> > Instead of calling Dispose outright I would recommend using the new Using
> > statement in .NET 2.0 (VS 2005). The Using statement ensures that Dispose
> > is called even if one of the statements contained by the Using throws an
> > exception.
> >
> >> Public Sub Foo()

> > Using myCustomer as New Customer
> >> ...

> > End Using
> >> End Sub

> >
> > Which is basically the following in .NET 1.x:
> >
> > Dim myCustomer As New Customer
> > Try
> > DoSomething()
> > Finally
> > If myCustomer IsNot Nothing Then
> > myCustomer.Dispose()
> > End If
> > End Try
> >
> > --
> > Hope this helps
> > Jay B. Harlow [MVP - Outlook]
> > .NET Application Architect, Enthusiast, & Evangelist
> > T.S. Bradley - http://www.tsbradley.net
> >
> >
> > "Hao Jiang" <Hao (E-Mail Removed)> wrote in message
> > news:4551F173-DAB8-4F74-AE61-(E-Mail Removed)...
> >> Hi,
> >>
> >> I see in many MS example code that looks like this
> >>
> >> Public Sub Foo()
> >> dim myCustomer as New Customer
> >> ...
> >> myCustomer.Dispose()
> >> myCustomer = Nothing
> >> End Sub
> >>
> >> I'm wondering if setting myCustomer = Nothing is necessary in this case?
> >> Since myCustomer is a private variable, once it goes out of scope at the
> >> next
> >> line, the reference to the object should be released.
> >>
> >> Thanks.
> >>
> >> Hao

> >

>
>
>

 
Reply With Quote
 
Brian Gideon
Guest
Posts: n/a
 
      27th Feb 2007
On Feb 26, 5:30 pm, Hao Jiang <Hao J...@discussions.microsoft.com>
wrote:
> Hi,
>
> I see in many MS example code that looks like this
>
> Public Sub Foo()
> dim myCustomer as New Customer
> ...
> myCustomer.Dispose()
> myCustomer = Nothing
> End Sub
>
> I'm wondering if setting myCustomer = Nothing is necessary in this case?
> Since myCustomer is a private variable, once it goes out of scope at the next
> line, the reference to the object should be released.
>
> Thanks.
>
> Hao


Hao,

To be absolutely precise the object is eligible for garbage collection
even before it goes out of scope as long as it's not used anymore. I
don't know how aggressive the GC rules are specifically, but it is
possible that the GC would consider the line myCustomer = Nothing as
having no side effects on the object and would collect the object even
before the line executed. I suppose it's reasonable to theorize that
the JIT compiler could optimize the line away as well.

Brian

Brian


 
Reply With Quote
 
Brian Gideon
Guest
Posts: n/a
 
      27th Feb 2007
On Feb 27, 11:25 am, Hao Jiang <HaoJi...@discussions.microsoft.com>
wrote:
> Thanks Goran, Hefried and Jay! That's what I thought.
>
> Cor, please check out the 2nd and 3rd code section on this MS page:http://support.microsoft.com/kb/888168
>
> Hao


Hmm...yeah, those are poorly written examples.

 
Reply With Quote
 
Cor Ligthert [MVP]
Guest
Posts: n/a
 
      27th Feb 2007
Hao,

Thanks, I have sent it further

Cor

"Hao Jiang" <(E-Mail Removed)> schreef in bericht
news:FAA14219-133E-44F6-A858-(E-Mail Removed)...
> Thanks Goran, Hefried and Jay! That's what I thought.
>
> Cor, please check out the 2nd and 3rd code section on this MS page:
> http://support.microsoft.com/kb/888168
>
> Hao
>
> "Cor Ligthert [MVP]" wrote:
>
>> Hao Jiang,
>>
>> Do you mean with MS samples, samples made by Micrsoft, if so can you than
>> show us some of those?
>> (URL's)
>>
>> Because it is not correct, it is as
>> dim a as integer = 1 + 1
>> if a <> 2 then messagebox.show "there was a calculation error"
>>
>> Thanks in advance,
>>
>> Cor
>>
>> "Jay B. Harlow [MVP - Outlook]" <(E-Mail Removed)> schreef in
>> bericht news:(E-Mail Removed)...
>> > Hao Jiang,
>> > As Göran & Herfried suggests setting myCustomer = Nothing is not needed
>> > for the reason you give.
>> >
>> > Instead of calling Dispose outright I would recommend using the new
>> > Using
>> > statement in .NET 2.0 (VS 2005). The Using statement ensures that
>> > Dispose
>> > is called even if one of the statements contained by the Using throws
>> > an
>> > exception.
>> >
>> >> Public Sub Foo()
>> > Using myCustomer as New Customer
>> >> ...
>> > End Using
>> >> End Sub
>> >
>> > Which is basically the following in .NET 1.x:
>> >
>> > Dim myCustomer As New Customer
>> > Try
>> > DoSomething()
>> > Finally
>> > If myCustomer IsNot Nothing Then
>> > myCustomer.Dispose()
>> > End If
>> > End Try
>> >
>> > --
>> > Hope this helps
>> > Jay B. Harlow [MVP - Outlook]
>> > .NET Application Architect, Enthusiast, & Evangelist
>> > T.S. Bradley - http://www.tsbradley.net
>> >
>> >
>> > "Hao Jiang" <Hao (E-Mail Removed)> wrote in message
>> > news:4551F173-DAB8-4F74-AE61-(E-Mail Removed)...
>> >> Hi,
>> >>
>> >> I see in many MS example code that looks like this
>> >>
>> >> Public Sub Foo()
>> >> dim myCustomer as New Customer
>> >> ...
>> >> myCustomer.Dispose()
>> >> myCustomer = Nothing
>> >> End Sub
>> >>
>> >> I'm wondering if setting myCustomer = Nothing is necessary in this
>> >> case?
>> >> Since myCustomer is a private variable, once it goes out of scope at
>> >> the
>> >> next
>> >> line, the reference to the object should be released.
>> >>
>> >> Thanks.
>> >>
>> >> Hao
>> >

>>
>>
>>



 
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.