On Mon, Sep 23, 2019 at 11:42 PM Christian Sakowski via 4D_Tech <
[email protected]> wrote:
> > I thought that only clears a reference to the object, not the object
> itself.
>
> Depends,
>
> if the ref-counter goes to 0, the object itself will be cleared too.
>
That's why I prefer Wayne's suggestion of using New object. Or setting it
to null.
But the lesson here is keeping in mind exactly what it is you are trying to
do.
$root:=New object("a";100;"details";New object("b";200;"c";"foo"))
$child:=$root.details
$child.d:="bar" // root: {a:100,details:{b:200,c:foo,d:bar}}
CLEAR VARIABLE($root) // $root = null; $child: {b:200,c:foo,d:bar}
// --------------------------------------------------------
$root:=New object("a";100;"details";New object("b";200;"c";"foo"))
$child:=$root.details
$child.d:="bar" // root: {a:100,details:{b:200,c:foo,d:bar}}
$root:=New object // $root = {}; $child: {b:200,c:foo,d:bar}
In both cases $child persists because the object itself ($child) exists
with its own ref counter. This may or may not be what you want.
What do you think happens when $child:=New object?
// --------------------------------------------------------
$root:=New object("a";100;"details";New object("b";200;"c";"foo"))
$child:=$root.details
$child.d:="bar" // root: {a:100,details:{b:200,c:foo,d:bar}}
$child:=New object // $root = {a:100,details:{b:200,c:foo,d:bar}}; $child:
{}
How the hell do I make $child and $root.details be empty? Maybe:
// --------------------------------------------------------
$root:=New object("a";100;"details";New object("b";200;"c";"foo"))
$child:=$root.details
$child.d:="bar" // root: {a:100,details:{b:200,c:foo,d:bar}}
$root.details:=New object // $root = {a:100,details:{}}; $child:
{b:200,c:foo,d:bar}
Why doesn't $child become empty? It's in the docs: "... New object creates
an empty object and returns its reference." $root.details contained a
reference, because the data are actually stored and managed by 4D, to the
object created by the first call to New object. Next I assigned $child the
reference to that same object. So its ref count is now 2. When I
call $root.details:=New object a new ref is created and assigned to
$root.details. This also lowers the ref count for the original object from
2 to 1. Because the ref count is not 0 the data persists and so $child,
which actually is a reference to the original object, still 'has' the
data.
This is what we encounter when using Storage too. Each object has to be
tracked with respect to its ref counter. That's why it's so fussy about
only allowing other shared objects to mingle.
This is a much different way of thinking about data outside of records and
fields or variables. In those cases the object is the data, as it were. And
this makes a strong case for scoping objects as local vars as much as
feasible because when a method ends the local vars are destroyed and any
references assigned only to local vars are cleared.
Here's a final take on this example. In this case let's define $child first:
// --------------------------------------------------------
$child:=New object("b";200;"c";"foo")
$root:=New object("a";100;"details";$child)
$joey:=$root.details what's the ref count on $child now?
$joey.d:="bar" // root: {a:100,details:{b:200,c:foo,d:bar}}
For each ($prop;$root.details)
OB REMOVE($root.details;$prop)
End for each
// $root: {a:100,details:{}} , $child: {}, $joey: {}
I hope this helps. It's been a real challenge for me to get the hang of how
references work and I don't profess to be an expert at it.
BTW - I did those examples in 17r6 but they should be the same in any
previous version.
--
Kirk Brooks
San Francisco, CA
=======================
What can be said, can be said clearly,
and what you can’t say, you should shut up about
*Wittgenstein and the Computer *
**********************************************************************
4D Internet Users Group (4D iNUG)
Archive: http://lists.4d.com/archives.html
Options: https://lists.4d.com/mailman/options/4d_tech
Unsub: mailto:[email protected]
**********************************************************************