Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Cleaning up leftover objects
Message
De
20/11/2010 11:31:37
 
 
À
20/11/2010 10:13:21
Information générale
Forum:
Visual FoxPro
Catégorie:
Codage, syntaxe et commandes
Versions des environnements
Visual FoxPro:
VFP 9 SP1
Divers
Thread ID:
01489909
Message ID:
01489915
Vues:
88
>A question about cleaning up leftover objects:
>
>I have a number of objects which themselves contain objects (in properties). What is the best way for these objects to clean up after themselves?
>
    This.oObject.Release()
>    Release This.oObject
>    This.oObject = .Null.
>Are these all equivalent?
>
>Thanks



>>Are these all equivalent?
No

Each object holds a reference count. It can only be destroyed when its reference count becomes zero
Define class TheClass as Relation
	
	Instance	= ''
	OtherObject = null

function init(instance)
	
	this.Instance = m.instance
	return DoDefault()
endfunc

function Destroy()
	acti screen
	?'Destroy ', m.this.Instance
endfunc

function Release()
	release this
endfunc

enddefine
*_______________________________________________________________________________
function TestDestroy1()
	
	local obj1, obj2, obj3
	
	obj1 = createObject('TheClass', '#1')
	obj2 = createObject('TheClass', '#2')
	obj1.OtherObject = m.obj2
	
	release obj1, obj2
	&& Destroy #1
	&& Destroy #2
endfunc
function TestDestroy2()
	
	local obj1, obj2, obj3
	
	obj1 = createObject('TheClass', '#1')
	obj2 = createObject('TheClass', '#2')
	obj2.OtherObject = m.obj1
	
	release obj1, obj2
	&& Destroy #2
	&& Destroy #1
endfunc
If you have a circular reference, you may have a problem
function TestDestroy3()
	
	local obj1, obj2, obj3
	
	&& Circular reference
	obj1 = createObject('TheClass', '#1')
	obj2 = createObject('TheClass', '#2')
	obj3 = createObject('TheClass', '#3')
	obj1.OtherObject = m.obj2
	obj2.OtherObject = m.obj3
	obj3.OtherObject = m.obj1
	
	release obj1, obj2, obj3
	&& no object is being destroyed
	
	&& need to use :Clear all

endfunc
object.Release() forces the object's reference count to become zero
Same setup as TestDestroy3(), but now using object.Release()
function TestDestroy4()
	
	local obj1, obj2, obj3
	
	obj1 = createObject('TheClass', '#1')
	obj2 = createObject('TheClass', '#2')
	obj3 = createObject('TheClass', '#3')
	obj1.OtherObject = m.obj2
	obj2.OtherObject = m.obj3
	obj3.OtherObject = m.obj1
	
	
	=m.obj2.Release()
	&& Destroy #2
	?obj2
	&& Null
	?obj1.OtherObject 
	&& Null : obj1.OtherObject is null because it has been destroyed

	&& Destroy #3
	&& Destroy #1
	

endfunc
Gregory
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform