Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does SplObjectStorage leave memory leak references if it destructs while objects are still attached?

Tags:

php

spl

If an SplObjectStorage instance destructs while still having some objects attached, does it implicitly detach the objects first, or does a memory leak result by the SplObjectStorage's references to those dangling objects? I'm trying to determine if userland code to "detach anything that's left before destructing" is necessary to prevent such a memory leak.

$storage = new SplObjectStorage();
$x = new stdClass();
$y = new stdClass();
$storage->attach($x);
$storage->attach($y);
$storage = null; 
// did not explicitly detach $x and $y... does $storage's destruction do it?
// or do the zval counts on $x and $y now off by one?
$x = null;
$y = null;
// at this point, are there two dangling references to $x and $y,
// simply because $storage did not dereference from them before destroying itself?
like image 931
ashnazg Avatar asked Nov 03 '22 07:11

ashnazg


1 Answers

The simple answer is: it should free those two objects.

If this is not the case, this should be considered a bug.

To test: create a class with a destructor, and ensure that it's called.

like image 77
Evert Avatar answered Nov 13 '22 18:11

Evert