Please see this related question for some background information.
When I say "invalid reference" I mean a reference that points to no data.
Assume we have the following data structure containing cyclic references:
+-----------------------------------------------------+
| |
+-->+============+ +==========+ |
[ Reference ----->[ Blessed ] |
$parent -->+============+ [ Hash ] |
[ ] +==========+ |
[ children --->[ Array ] |
[ ] [ ] |
+==========+ [ 0: ---------+ |
[ ] | |
+==========+ | |
| |
+--------------------------------------------------+ |
| |
+-->+============+ +==========+ |
[ Reference ----->[ Blessed ] |
$child --->+============+ [ Hash ] |
[ ] |
[ parent: ----------------------+
[ ]
+==========+
I understand that I can use Scalar::Util
's weaken
function to "weaken" references . . . but what happens if I weaken the reference from parent->child
and also weaken the reference from child->parent
and then either $child
or $parent
goes out of scope, but not the other?
Example: $parent
goes out of scope so the reference is gone.
+-----------------------------------------------------+
| |
+-->+============+ +==========+ |
[ Reference ----->[ Blessed ] |
+============+ [ Hash ] |
[ ] +==========+ |
[ children --->[ Array ] |
[ ] [ ] |
+==========+ [ 0: ---------+ |
[ ] | |
+==========+ | |
| |
would this break the link? ------------> X X
| |
+--------------------------------------------------+ |
| |
+-->+============+ +==========+ |
[ Reference ----->[ Blessed ] |
$child --->+============+ [ Hash ] |
[ ] |
[ parent: ----------------------+ <--- would this parent object pointer now be invalid?
[ ]
+==========+
If I did this, and then the "parent" went out of scope, would the parent object be removed from memory because Perl's internal reference count for that object goes to 0? I ask this, because if $child
still exists and needs to use some data from the parent object this would cause problems because the child object would now hold an invalid pointer to the parent.