php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #16334 memory leak when passing object
Submitted: 2002-03-28 17:02 UTC Modified: 2002-06-01 10:03 UTC
Votes:2
Avg. Score:3.5 ± 0.5
Reproduced:2 of 2 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: roland at inkoeln dot com Assigned:
Status: Closed Package: Class/Object related
PHP Version: 4.1.1 OS: linux
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: roland at inkoeln dot com
New email:
PHP Version: OS:

 

 [2002-03-28 17:02 UTC] roland at inkoeln dot com
I found a serious memory leaks which occures when passing 
objects. I have included two small code snipplets which 
will reproduce the leak:


Code Snipplet-1: 

memory leak when passing $this object.

<?php
 
 
class Tok {
 
  var $_filter = array();
 
  function filter($name, &$obj) {
    $this->_filter[$name] =& $obj;
  }
 
}
 
 
class A {
 
  var $_tok;
 
  function A() {
    $this->_tok = new Tok();
    $this->_tok->filter('xyz', $this); // memory leak
  }
 
}
 
 
$a = new A();
 
?>

Leak message in error.log:

zend_hash.c(260) :  Freeing 0x08210544 (39 bytes), 
script=/home/share/server/test/index2.php
Last leak repeated 2 times
zend_hash.c(176) :  Freeing 0x082104F4 (32 bytes), 
script=/home/share/server/test/index2.php
Last leak repeated 2 times
./zend_execute.c(753) :  Freeing 0x08210494 (44 bytes), 
script=/home/share/server/test/index2.php
zend_variables.c(117) : Actual location (location was 
relayed)
Last leak repeated 1 time
zend_API.c(593) :  Freeing 0x0821019C (44 bytes), 
script=/home/share/server/test/index2.php
zend_API.c(581) : Actual location (location was relayed)
Last leak repeated 1 time
./zend_execute.c(1940) :  Freeing 0x0820E714 (12 bytes), 
script=/home/share/server/test/index2.php
Last leak repeated 1 time


Here an another example (i think related):


Code-Snipplet-2:

<?php
 
 
class A {
 
  var $_parent;
 
  function setParent(&$obj) {
    $this->_parent =& $obj;
  }
 
 
  function setParentCopy($obj) {
    $this->_parent = $obj;
  }
 
}
 
 
 
$a1 = new A();
$a2 = new A();
 
$a1->setParent($a2); // works
$a1->setParent($a1); // memory leak
$a1->setParentCopy($a1); // memory leak too
 
echo "done";
 
?>


Leak message in error.log:

zend_hash.c(260) :  Freeing 0x085698BC (43 bytes), 
script=/home/share/server/test/index.php
zend_hash.c(176) :  Freeing 0x0856986C (32 bytes), 
script=/home/share/server/test/index.php
./zend_execute.c(425) :  Freeing 0x0856980C (44 bytes), 
script=/home/share/server/test/index.php
zend_variables.c(126) : Actual location (location was 
relayed)
./zend_execute.c(1940) :  Freeing 0x08368BCC (12 bytes), 
script=/home/share/server/test/index.php


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-05-24 09:34 UTC] ferelloc at club-internet dot fr
OS SunOS 5.7 / Netscape Enterprise 3.2 / PHP 4.1.2

Script below causes memory leak, it seems to be the same problem... (circular reference ...)
class aclass{
		var $child;	
		function aclass(){
			this->increaseMemoryUse = array( "ABCEDFGHIJKLMNOPQRSTUVWXYZ");
		}
		function setChild(){
			$this->child = new achild();
			// without the line below, there is no memory leak !!!
			$this->child->father = $this;
		}
	}
	class achild{
		var $father;
		function achild(){
			$this->increaseMemoryUse=array("ABCEDFGHIJKLMNOPQRSTUVWXYZ");
		}
		
	}
	
	function create(){
		$aNewclass = new aclass;
		$aNewclass->setChild();
		return $aNewclass;
	}
	for($i=0 ; $i <= 5000 ; $i++){
		$myClass = create();
		if (!($i % 1000)){
			echo "TEST : $i mem=" . exec ('ps -o vsz -p ' . getmypid() ) . '<br>';
		}
	}
?>	
-------------
test output :
TEST : 0 mem=10672
TEST : 1000 mem=11888
TEST : 2000 mem=13128
TEST : 3000 mem=14360
TEST : 4000 mem=15600
TEST : 5000 mem=16840
 [2002-06-01 10:03 UTC] mfischer@php.net
This is fixed in ZE2.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Dec 22 03:01:28 2024 UTC