Prev: ReDim'ed Array size
Next: Borderless Borderless Form
From: Simon Woods on 13 Oct 2009 06:23 Hi If I have a factory method like this in a module (or class) Public Function CreateMyObject() as MyObject dim myLocalObject as MyObject Set myLocalObject = new MyObject Set CreateMyObject = myLocalObject End Function and in another class I instantiate it Private Sub Test Dim myRemoteObject as MyObject Set myRemoteObject = Factory.CreateMyObject ... End Sub Will myLocalObject get destroyed when Sub Test finishes? Thx S
From: Ralph on 13 Oct 2009 06:44 "Simon Woods" <simonjwoods(a)hotmail.com> wrote in message news:uA9g58%23SKHA.220(a)TK2MSFTNGP02.phx.gbl... > Hi > > If I have a factory method like this in a module (or class) > > Public Function CreateMyObject() as MyObject > > dim myLocalObject as MyObject > > Set myLocalObject = new MyObject > Set CreateMyObject = myLocalObject > End Function > > and in another class I instantiate it > > Private Sub Test > > Dim myRemoteObject as MyObject > > Set myRemoteObject = Factory.CreateMyObject > ... > > End Sub > > Will myLocalObject get destroyed when Sub Test finishes? > Yes But this is better stated as "the object that is associated with the myRemoteObject reference variable is destroyed in Sub Test". -ralph
From: Dee Earley on 13 Oct 2009 07:34 On 13/10/2009 11:23, Simon Woods wrote: > Hi > > If I have a factory method like this in a module (or class) > > Public Function CreateMyObject() as MyObject > > dim myLocalObject as MyObject > > Set myLocalObject = new MyObject > Set CreateMyObject = myLocalObject > End Function > > and in another class I instantiate it > > Private Sub Test > > Dim myRemoteObject as MyObject > > Set myRemoteObject = Factory.CreateMyObject > ... > > End Sub > > Will myLocalObject get destroyed when Sub Test finishes? Yes as myRemoteObject goes out of scope, the last reference will be released causing it to be destroyed. -- Dee Earley (dee.earley(a)icode.co.uk) i-Catcher Development Team iCode Systems
From: Ralph on 13 Oct 2009 07:45 Perhaps this will make it clearer: VB uses reference counting to manage its form of "garbage collection". When you declare an Object like this ... Dim myLocalObject As MyObject What you are doing is declaring an Object Reference variable of the type default public interface of MyObject. It is not the MyObject it is a reference variable that can hold a reference to any object that satitisfies the type description - ie, any object that exposes a "MyObject Interface". But it doesn't 'point to', 'is associated with', or reference any concrete object. To do that we have to create an object and assign it to the reference variable. Set myLocalObject = New MyObject This creates a new object of type MyObject in the 'heap' somewhere. And VB increments the 'reference count' for that MyObject by one. [note inline ...] > Public Function CreateMyObject() as MyObject > ' declared an object reference ' this is on the stack - an 'auto variable' > dim myLocalObject as MyObject > ' create a MyObject on the heap and increment its ' reference count to 1 > Set myLocalObject = new MyObject ' VB Functions evaluate to the type of the return ' in this case an object reference variable. ' Think of it as a "hidden" variable ' the reference count of MyObject is logically* incremented by 1 [* more below] > Set CreateMyObject = myLocalObject ' there are now two references to MyObject ' ie, the reference count for MyObject is 2 ' auto variables go out of scope ' At the end of the function VB has hidden code that ' does essentially this ; Set myLocalObject = Nothing ' this decrements the reference count of MyObject to 1 ' had the reference count been decremented to zero, myObject would have ' been destroyed. > End Function > > and in another class I instantiate it > > Private Sub Test > ' you declare an object reference variable > Dim myRemoteObject as MyObject > ' VB is a busy little beaver at this point. ' two thing are going on ' 1) VB assigns the MyObject to the myRemoteObject ' incrementing the reference count by 1 to 2 ' 2) VB logically* decrements the reference count to the ' CreateMyObject "hidden" object reference by 1 - ' thus the reference count to MyObject is now 1 > Set myRemoteObject = Factory.CreateMyObject > ... > ' remember the auto clean-up of auto variables? ; Set myRemoteObject = Nothing ' the reference count to MyObject is decremented by one ' the reference count is now zero ' and VB's "garbage collection" will now destroy MyObject > End Sub > > Will myLocalObject get destroyed when Sub Test finishes? > yes Now the logically* part. There is some debate concerning just exactly how VB manages object returns. As most of the code is proprietory and you can get slightly different ASM depending on Object types, it isn't clear if VB actually performs reference counting, or simply assumes by logic* that there is an additional count up on assignment and a respective count down on return. Either way it doesn't matter - VB seems to do the right thing. <g> hth -ralph
From: Simon Woods on 13 Oct 2009 07:59
On 13/10/2009 12:45, Ralph wrote: <snip> That's very helpful. Thx to all Simon |