php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #39328 Memory leak when object contains reference to "parent" object
Submitted: 2006-11-01 08:52 UTC Modified: 2006-11-01 09:04 UTC
From: taneli at crasman dot fi Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.1.6 OS: Linux
Private report: No CVE-ID: None
 [2006-11-01 08:52 UTC] taneli at crasman dot fi
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.

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-11-01 09:01 UTC] derick@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

.
 [2006-11-01 09:04 UTC] taneli at crasman dot fi
That was quick. Looks like another version of this is assigned already (just found it), so how come this was marked "Bogus"? :)

http://bugs.php.net/bug.php?id=33595
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Wed Dec 03 15:00:01 2025 UTC