Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > .NET General

Vista - When does a class isntance get destroyed?

Reply
 
Old 07-17-2009   #1 (permalink)
John Peterson


 
 

When does a class isntance get destroyed?

Question about when an class instance get destroyed. I have a class
called ArgList that I use to pass arguments to another thread.
See code below. Sub A creates the new instance of the class
and sets the values. The class is then passed to the new
thread called GetWeight. GetWeight then uses the the values
passed to it. My question is the following. After the thread
is created in Sub A, Sub A will exit. What happens to the class
instance when Sub A exits? If the class object will be deleted
then is it possible that Sub GetWeight will not be able to
reference the values passed to it? Thanks.

John

Public Class ArgList
Public iPort As Integer
Public bWeightIntFlg As Boolean
End Class

Sub A()
Dim TArgs As New ArgList
TArgs.iPort = 1
TArgs.bWeightIntFlg = True
Dim ThreadT As New Thread(AddressOf GetWeight)
ThreadT.Start(TArgs)
End Sub

Sub GetWeight(obj As Object)
Dim Args As ArgList = DirectCast(obj, ArgList)
Dim iPort as Integer, Weight as Single
IF Args.iPort = 1 Then
<Do some work>
'now use the flag to see if weight is
'integer or floating point
If Args.bWeightIntFlg then
Weight = CInt(WeightStr)
Else
Weight = Csng(WeightStr)
End If
End If
End Sub


My System SpecsSystem Spec
Old 07-17-2009   #2 (permalink)
SyntaxError


 
 

Re: When does a class isntance get destroyed?

The class will stay instantiated as long as there is a reference to it.
Since a reference is created in GetWeight, the class will remain until
GetWeight is completed.

"John Peterson" <nospam@xxxxxx> wrote in message
news:MPG.24c9948464f695e0989685@xxxxxx
Quote:

> Question about when an class instance get destroyed. I have a class
> called ArgList that I use to pass arguments to another thread.
> See code below. Sub A creates the new instance of the class
> and sets the values. The class is then passed to the new
> thread called GetWeight. GetWeight then uses the the values
> passed to it. My question is the following. After the thread
> is created in Sub A, Sub A will exit. What happens to the class
> instance when Sub A exits? If the class object will be deleted
> then is it possible that Sub GetWeight will not be able to
> reference the values passed to it? Thanks.
>
> John
>
> Public Class ArgList
> Public iPort As Integer
> Public bWeightIntFlg As Boolean
> End Class
>
> Sub A()
> Dim TArgs As New ArgList
> TArgs.iPort = 1
> TArgs.bWeightIntFlg = True
> Dim ThreadT As New Thread(AddressOf GetWeight)
> ThreadT.Start(TArgs)
> End Sub
>
> Sub GetWeight(obj As Object)
> Dim Args As ArgList = DirectCast(obj, ArgList)
> Dim iPort as Integer, Weight as Single
> IF Args.iPort = 1 Then
> <Do some work>
> 'now use the flag to see if weight is
> 'integer or floating point
> If Args.bWeightIntFlg then
> Weight = CInt(WeightStr)
> Else
> Weight = Csng(WeightStr)
> End If
> End If
> End Sub
>
My System SpecsSystem Spec
Old 07-18-2009   #3 (permalink)
Scott M.


 
 

Re: When does a class isntance get destroyed?

Just an extra FYI. Technically, the instance will stay in memory until it
is Garbage Collected, which may or may not happen in a timely manner. It
will no longer be available for use when GetWeight completes.


"SyntaxError" <visit@xxxxxx> wrote in message
news:9C53A468-B234-4BD8-B824-00D44F4F5906@xxxxxx
Quote:

> The class will stay instantiated as long as there is a reference to it.
> Since a reference is created in GetWeight, the class will remain until
> GetWeight is completed.
>
> "John Peterson" <nospam@xxxxxx> wrote in message
> news:MPG.24c9948464f695e0989685@xxxxxx
Quote:

>> Question about when an class instance get destroyed. I have a class
>> called ArgList that I use to pass arguments to another thread.
>> See code below. Sub A creates the new instance of the class
>> and sets the values. The class is then passed to the new
>> thread called GetWeight. GetWeight then uses the the values
>> passed to it. My question is the following. After the thread
>> is created in Sub A, Sub A will exit. What happens to the class
>> instance when Sub A exits? If the class object will be deleted
>> then is it possible that Sub GetWeight will not be able to
>> reference the values passed to it? Thanks.
>>
>> John
>>
>> Public Class ArgList
>> Public iPort As Integer
>> Public bWeightIntFlg As Boolean
>> End Class
>>
>> Sub A()
>> Dim TArgs As New ArgList
>> TArgs.iPort = 1
>> TArgs.bWeightIntFlg = True
>> Dim ThreadT As New Thread(AddressOf GetWeight)
>> ThreadT.Start(TArgs)
>> End Sub
>>
>> Sub GetWeight(obj As Object)
>> Dim Args As ArgList = DirectCast(obj, ArgList)
>> Dim iPort as Integer, Weight as Single
>> IF Args.iPort = 1 Then
>> <Do some work>
>> 'now use the flag to see if weight is
>> 'integer or floating point
>> If Args.bWeightIntFlg then
>> Weight = CInt(WeightStr)
>> Else
>> Weight = Csng(WeightStr)
>> End If
>> End If
>> End Sub
>>
>

My System SpecsSystem Spec
Old 07-19-2009   #4 (permalink)
Gregory A. Beamer


 
 

Re: When does a class isntance get destroyed?

John Peterson <nospam@xxxxxx> wrote in news:MPG.24c9948464f695e0989685
@news.giganews.com:
Quote:

> Question about when an class instance get destroyed. I have a class
> called ArgList that I use to pass arguments to another thread.
> See code below. Sub A creates the new instance of the class
> and sets the values. The class is then passed to the new
> thread called GetWeight. GetWeight then uses the the values
> passed to it. My question is the following. After the thread
> is created in Sub A, Sub A will exit. What happens to the class
> instance when Sub A exits? If the class object will be deleted
> then is it possible that Sub GetWeight will not be able to
> reference the values passed to it? Thanks.
>
From a practical standpoint, when code execution falls out of the routine
that instantiated the object the object is no longer available for your
use. The actual memory will not be cleared until later, but without jumping
through hoops (if it is even possible to still reference the object with
some seriously complex code), you cannot access them.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

Twitter: @gbworld
Blog: http://gregorybeamer.spaces.live.com

******************************************
| Think outside the box! |
******************************************
My System SpecsSystem Spec
Old 07-19-2009   #5 (permalink)
Scott M.


 
 

Re: When does a class isntance get destroyed?


"Gregory A. Beamer" <NoSpamMgbworld@xxxxxx> wrote in message
news:Xns9C4D5AA8BECD3gbworldcomcastnet@xxxxxx
Quote:

> John Peterson <nospam@xxxxxx> wrote in news:MPG.24c9948464f695e0989685
> @news.giganews.com:
Quote:

> From a practical standpoint, when code execution falls out of the routine
> that instantiated the object the object is no longer available for your
> use.
Well, not necessarially. If a reference to the object is passed to a
different routine, the first routine could, in fact complete without the
object becomming unavailable, as is the case here. Objects become elligible
for collection (and thus unavailable to code) when they have no more
reference paths from the application.

-Scott


My System SpecsSystem Spec
Old 07-20-2009   #6 (permalink)
Gregory A. Beamer


 
 

Re: When does a class isntance get destroyed?

"Scott M." <s-mar@xxxxxx> wrote in
news:Owe8ZkICKHA.1380@xxxxxx:
Quote:

>
> "Gregory A. Beamer" <NoSpamMgbworld@xxxxxx> wrote in
> message news:Xns9C4D5AA8BECD3gbworldcomcastnet@xxxxxx
Quote:

>> John Peterson <nospam@xxxxxx> wrote in
>> news:MPG.24c9948464f695e0989685 @news.giganews.com:
>
Quote:

>> From a practical standpoint, when code execution falls out of the
>> routine that instantiated the object the object is no longer
>> available for your use.
>
> Well, not necessarially. If a reference to the object is passed to a
> different routine, the first routine could, in fact complete without
> the object becomming unavailable, as is the case here. Objects become
> elligible for collection (and thus unavailable to code) when they have
> no more reference paths from the application.
I probably should have qualified that more, as I made an assumption that
we were talking something like this:

public void Routine1()
{
MyClass c = new MyClass();
}


In this, the class is effectively MIA when you exit the routine. if you
code something globally, or pass to a routine, all bets are off. ;-)

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

Twitter: @gbworld
Blog: http://gregorybeamer.spaces.live.com

*******************************************
| Think outside the box! |
*******************************************
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
When a class is both an inherited class of another, and alsoimplements an interface method .NET General
Destroyed Files.. General Discussion
OS destroyed? Vista security
Codec Destroyed itself? Sound & Audio
Hibernation is destroyed Vista General


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46