Edit report at https://bugs.php.net/bug.php?id=71818&edit=1
ID: 71818
User updated by: james dot harris at icecave dot com dot au
Reported by: james dot harris at icecave dot com dot au
Summary: Memory leak when array altered in destructor
-Status: Feedback
+Status: Open
Type: Bug
Package: Scripting Engine problem
Operating System: OSX
PHP Version: 7.0.4
Block user comment: N
Private report: N
New Comment:
Those are the conditions under which the leak does NOT occur. Sorry, I worded it a bit backwards.
I realise this is a bit academic and strange, and I've certainly worked around whatever code I
found doing this (I can't remember what it was now), but I figured it was still worth bring up
incase it was an issue with the array implementation itself.
Thanks,
James
Previous Comments:
------------------------------------------------------------------------
[2016-04-14 14:35:40] jhdxr@php.net
"$this is removed from the array in the destructor"
In fact, you did NOT removed $thif from the array. $this->things[] = null; add a new
element into the array.
You can use
$this->things = null;
or
`unset($this->things);
------------------------------------------------------------------------
[2016-03-14 07:40:00] james dot harris at icecave dot com dot au
Description:
------------
A memory leak occurs when array that contains a reference to $this is altered in the object's
destructor.
The leak does not occur if:
* the array does not contain $this
* the array is not altered in the destructor
* $this is removed from the array in the destructor
Affected PHP versions: https://3v4l.org/lHeRV
This gist (https://gist.github.com/jmalloc/b88752e391d464609339) also contains the script to
reproduce.
Test script:
---------------
class MemoryLeak
{
public function __construct()
{
$this->things[] = $this;
}
public function __destruct()
{
$this->things[] = null;
}
private $things = [];
}
ini_set('memory_limit', '10M');
for ($i = 0; $i < 100000; ++$i) {
$obj = new MemoryLeak();
}
echo "Done";
Expected result:
----------------
Done
Actual result:
--------------
Fatal error: Allowed memory size of 10485760 bytes exhausted (tried to allocate 20480 bytes) in
/Users/james/<snip>/leak.php on line 30
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=71818&edit=1