On Sat, Oct 29, 2016 at 1:28 PM, Peter Jakobsson <[email protected]>
wrote:
>
> The question is, what does 4D do to object spaces that are only
referenced by locals ? I imagine that to avoid memory leaks, it must keep
track of all references and delete the object space when all references
have dropped out of scope. Conversely it seems to imply that if we
reference an object with a process variable then the object space will
never be deleted until 4D quits = another “Watcha” !
I assume objects - it means, object variables - are just a references to
some internal storage that contain name-value pairs. The storage is
ref-counted, what means it tracks how many references point to it.
I prefer a 'reference' to 'pointer' as under pointer I imagine a single
number that does not know anything about what it points to.
In your example 'CODE BLOCK 2', $myObject is reference to some storage
with one reference ($myObject.)
APPEND TO ARRAY($arrOBJECTS;$myObject) does not create another storage, it
just increases a reference count - both myObject and $arrOBJECTS[1] points
to it. At the end, you still have only one storage containing value pair
"myName'="George", referenced by four (local) variables (myObject and three
elements of $arrOBJECTS.)
YEs, 4D need to handle destruction of local variables at the end of method
and destruction of process variables at the termination of process, but
with current OO programming languages this is not a big deal.
Situation will fast get more interesting - imagine following code
ARRAY OBJECT($arrOBJECTS;0)
C_OBJECT($myObject)
APPEND TO ARRAY($arrOBJECTS;$myObject)
APPEND TO ARRAY($arrOBJECTS;$myObject)
OB SET(arrOBJECTS{1};"myName";"Mickey”) `
OB SET(arrOBJECTS{2};"myName";"Goofy") // [1]=“ Mickey” [2]=“ Goofy”
but
ARRAY OBJECT($arrOBJECTS;0)
C_OBJECT($myObject)
$object=JSON Parse("{}”) // init object
APPEND TO ARRAY($arrOBJECTS;$myObject)
APPEND TO ARRAY($arrOBJECTS;$myObject)
OB SET(arrOBJECTS{1};"myName";"Mickey”) `
OB SET(arrOBJECTS{2};"myName";"Goofy") // [1]=“ Goofy” [2]=“ Goofy”
Object can contain other object, what seems to mean in 4D that object (its
storage) contain reference to another object (another piece in the storage.)
C_OBJECT($object1;$object2;$object3)
$object1:=JSON Parse("{}") // init object"
$object2:=JSON Parse("{}") // init object"
OB SET($object1;"object";$object2)
$object3:=OB Copy($object1)
Now $object3 contain a copy of $object1, does its "object" property contain
a reference to the same object ($object2) or did 4D copied an included
object as well - i.e. are object properties of $object1 and $object3
referring the same or different storage? It can be tested with
OB SET($object2;"name";"George")
and it seems it is the later case..
You can do circular references:
C_OBJECT($object1;$object2)
$object1:=JSON Parse("{}”) // init object
$object2:=JSON Parse("{}”) // init object
OB SET($object1;"object"; $object2)
OB SET($object2;"object"; $object1)
Now $object1 and $object2 create a circular reference (and debugger will
show it cannot display such object), they may not be cleared from memory
when method ends (will not be cleared ever.)
Now, you may decide to disconnect the loop by calling
OB SET($object2;"object";JSON Parse("{}"))
You would say that should reset the loop - $object1 will still contain
reference to $object2, but $object2 will not contain reference to $object1.
Alas, not. Try it.
So you can have a lot of fun with objects!
--
Peter Bozek
**********************************************************************
4D Internet Users Group (4D iNUG)
FAQ: http://lists.4d.com/faqnug.html
Archive: http://lists.4d.com/archives.html
Options: http://lists.4d.com/mailman/options/4d_tech
Unsub: mailto:[email protected]
**********************************************************************