Bug #71818 [Csd->Asn]: Memory leak when array altered in destructor

From: Date: Thu, 14 Jul 2016 09:08:25 +0000
Subject: Bug #71818 [Csd->Asn]: Memory leak when array altered in destructor
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-202298@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=71818&edit=1

 ID:                 71818
 Updated by:         dmitry@php.net
 Reported by:        james dot harris at icecave dot com dot au
 Summary:            Memory leak when array altered in destructor
-Status:             Closed
+Status:             Assigned
 Type:               Bug
 Package:            Scripting Engine problem
 Operating System:   OSX
 PHP Version:        7.0.4
-Assigned To:        
+Assigned To:        laruence
 Block user comment: N
 Private report:     N

 New Comment:

Fixed in PHP-7.1.
http://git.php.net/?p=php-src.git;a=commitdiff;h=8fc934b0a1781bc0685775f4c476c6704d796a76
The patch might be backported to PHP-7.0 after additional testing.


Previous Comments:
------------------------------------------------------------------------
[2016-07-14 09:05:57] dmitry@php.net

Automatic comment on behalf of dmitry@zend.com
Revision: http://git.php.net/?p=php-src.git;a=commit;h=8fc934b0a1781bc0685775f4c476c6704d796a76
Log: Fixed bug #71818 (Memory leak when array altered in destructor)

------------------------------------------------------------------------
[2016-04-15 01:39:11] james dot harris at icecave dot com dot au

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

------------------------------------------------------------------------
[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


Thread (11 messages)

« previous php.bugs (#202298) next »