Bug #71818 [NEW]: Memory leak when array altered in destructor

From: Date: Mon, 14 Mar 2016 07:40:11 +0000
Subject: Bug #71818 [NEW]: Memory leak when array altered in destructor
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-199806@lists.php.net to get a copy of this message
From:             james dot harris at icecave dot com dot au
Operating system: OSX
PHP version:      7.0.4
Package:          Scripting Engine problem
Bug Type:         Bug
Bug description:Memory leak when array altered in destructor

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 bug report at https://bugs.php.net/bug.php?id=71818&edit=1
-- 
Try a snapshot (PHP 5.4):   https://bugs.php.net/fix.php?id=71818&r=trysnapshot54
Try a snapshot (PHP 5.5):   https://bugs.php.net/fix.php?id=71818&r=trysnapshot55
Try a snapshot (trunk):     https://bugs.php.net/fix.php?id=71818&r=trysnapshottrunk
Fixed in SVN:               https://bugs.php.net/fix.php?id=71818&r=fixed
Fixed in release:           https://bugs.php.net/fix.php?id=71818&r=alreadyfixed
Need backtrace:             https://bugs.php.net/fix.php?id=71818&r=needtrace
Need Reproduce Script:      https://bugs.php.net/fix.php?id=71818&r=needscript
Try newer version:          https://bugs.php.net/fix.php?id=71818&r=oldversion
Not developer issue:        https://bugs.php.net/fix.php?id=71818&r=support
Expected behavior:          https://bugs.php.net/fix.php?id=71818&r=notwrong
Not enough info:            https://bugs.php.net/fix.php?id=71818&r=notenoughinfo
Submitted twice:            https://bugs.php.net/fix.php?id=71818&r=submittedtwice
register_globals:           https://bugs.php.net/fix.php?id=71818&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=71818&r=php4
Daylight Savings:           https://bugs.php.net/fix.php?id=71818&r=dst
IIS Stability:              https://bugs.php.net/fix.php?id=71818&r=isapi
Install GNU Sed:            https://bugs.php.net/fix.php?id=71818&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=71818&r=float
No Zend Extensions:         https://bugs.php.net/fix.php?id=71818&r=nozend
MySQL Configuration Error:  https://bugs.php.net/fix.php?id=71818&r=mysqlcfg



Thread (11 messages)

« previous php.bugs (#199806) next »