#39328 [NEW]: Memory leak when object contains reference to "parent" object

From: Date: Wed, 01 Nov 2006 08:52:20 +0000
Subject: #39328 [NEW]: Memory leak when object contains reference to "parent" object
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-104244@lists.php.net to get a copy of this message
From: taneli at crasman dot fi Operating system: Linux PHP version: 5.1.6 PHP Bug Type: Scripting Engine problem Bug description: Memory leak when object contains reference to "parent" object Description: ------------ The example says it the best. Reproducible in both 5.1.6 and 4.4.4 (with minor changes), all extensions disabled. Definately related to references, although I'm not sure if the code below is good practice, but I've run into it on more than one occasion. I think it should be freeing the objects when they go out of scope, but the reference counting must be broken somehow. Might be related to #22055, but this one's been around for ages. Reproduce code: --------------- <?php { class A { var $ba; function A() { $this->ba = array(); for ($i = 0; $i < 100; $i++) $this->ba[] = new B($this); } function __destruct() { // never gets called die("Freeing class A\n"); } } class B { var $a; function B($a) { // function B(&$a), and: $this->a = $a; // $this->a =& $a; leaks in php 4.4.4 // remove line above to prevent leak } function __destruct() { // never gets called either die("Freeing class B\n"); } } $smu = memory_get_usage(); while (true) { $ba = array(); for ($i = 0; $i < 100; $i++) $ba[] = new A(); $emu = memory_get_usage(); echo sprintf("leak %.1f kB\r", ($emu - $smu) / 1024.0); } }?> Expected result: ---------------- No memory leak. Actual result: -------------- Rapidly increasing memory leak. -- Edit bug report at http://bugs.php.net/?id=39328&edit=1 -- Try a CVS snapshot (PHP 4.4): http://bugs.php.net/fix.php?id=39328&r=trysnapshot44 Try a CVS snapshot (PHP 5.2): http://bugs.php.net/fix.php?id=39328&r=trysnapshot52 Try a CVS snapshot (PHP 6.0): http://bugs.php.net/fix.php?id=39328&r=trysnapshot60 Fixed in CVS: http://bugs.php.net/fix.php?id=39328&r=fixedcvs Fixed in release: http://bugs.php.net/fix.php?id=39328&r=alreadyfixed Need backtrace: http://bugs.php.net/fix.php?id=39328&r=needtrace Need Reproduce Script: http://bugs.php.net/fix.php?id=39328&r=needscript Try newer version: http://bugs.php.net/fix.php?id=39328&r=oldversion Not developer issue: http://bugs.php.net/fix.php?id=39328&r=support Expected behavior: http://bugs.php.net/fix.php?id=39328&r=notwrong Not enough info: http://bugs.php.net/fix.php?id=39328&r=notenoughinfo Submitted twice: http://bugs.php.net/fix.php?id=39328&r=submittedtwice register_globals: http://bugs.php.net/fix.php?id=39328&r=globals PHP 3 support discontinued: http://bugs.php.net/fix.php?id=39328&r=php3 Daylight Savings: http://bugs.php.net/fix.php?id=39328&r=dst IIS Stability: http://bugs.php.net/fix.php?id=39328&r=isapi Install GNU Sed: http://bugs.php.net/fix.php?id=39328&r=gnused Floating point limitations: http://bugs.php.net/fix.php?id=39328&r=float No Zend Extensions: http://bugs.php.net/fix.php?id=39328&r=nozend MySQL Configuration Error: http://bugs.php.net/fix.php?id=39328&r=mysqlcfg

« previous php.bugs (#104244) next »