![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | 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 Specs![]() |
| | #2 (permalink) |
| | 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 Specs![]() |
| | #3 (permalink) |
| | 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 Specs![]() |
| | #4 (permalink) |
| | 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. > 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 Specs![]() |
| | #5 (permalink) |
| | 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. 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 Specs![]() |
| | #6 (permalink) |
| | 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. 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 Specs![]() |
![]() |
| 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 | |||