php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #5620 Memory leaks with classes and references to objects
Submitted: 2000-07-15 14:04 UTC Modified: 2000-08-15 14:32 UTC
From: h dot raaf at i-k-c dot net Assigned:
Status: Closed Package: Scripting Engine problem
PHP Version: 4.0.1pl2 OS: Linux
Private report: No CVE-ID: None
 [2000-07-15 14:04 UTC] h dot raaf at i-k-c dot net
While working on my current project I run into
the following memory leak problem:

class A {
	var $Bref=0;

	function CreateB() {
		$ob=new B;
		$this->Bref=&$ob;
		$this->Bref->Aref=&$this;
	}
}

class B {
	var $Aref=0;
}

$oa=new A;
$oa->CreateB();

--- Results in:

./zend_execute.c(392) :  Freeing 0x085D570C (12 bytes), script=/var/www/users/test8/www/classleak.php
Last leak repeated 1 time
zend_hash.c(271) :  Freeing 0x085422A4 (40 bytes), script=/var/www/users/test8/www/classleak.php
Last leak repeated 1 time
zend_hash.c(200) :  Freeing 0x088E6614 (20 bytes), script=/var/www/users/test8/www/classleak.php
Last leak repeated 1 time
./zend_execute.c(1895) :  Freeing 0x085478E4 (48 bytes), script=/var/www/users/test8/www/classleak.php
zend_API.c(208) : Actual location (location was relayed)
Last leak repeated 1 time

------

My real project code (a HRXSLT) generates
many, many more of this leaks...

I just tried to make the shortest program
which illustrates the problem.

Greetings,
 Hans Raaf

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2000-07-16 02:13 UTC] stas at cvs dot php dot net
PHP doesn't do circular references. Don't do it.
 [2000-07-16 02:16 UTC] stas at cvs dot php dot net
BTW, why you are using references at all in this case? At least Bref shouldn't be a reference.
 [2000-07-16 13:10 UTC] h dot raaf at i-k-c dot net
Hmm... As stated in my initial text. This was a try to do the "smalles" possible code to produce the problem.

I need to have one class which generates objects of a second class and keeps a list of them. This second class objects need to have a reference to modify data in the class which created (first class) them. And in addition the first class needs to maintain a 'pointer' to different 'active' second class objects. Both classes make heavily use of recursive functions.

There is also a third class whose objects are generated by the second class (and also need a reference to the creating object). This objects are used by the first class and the second class....

I know that this all sounds a bit strange... I talk about working code (my XLST Processor). But something going wrong and one thing is this leaking of memory. If the stuff I do is 'forbidden' it would be fine if some error/warning would prevent me to do it.

I made another example code to illustrate the problem
-----------------------------------------------------

<?php
class A {
	var $name="A";
	var $B=0;

	function CreateB() {
		$this->B=new B;
		$this->B->Aref=&$this;
	}

	function CallB() {
		$this->B->Test();
		print("NewNameA: ".$this->name."<br>");
	}

}

class B {
	var $name="B";
	var $Aref=0;
	function Test() {
		print("NameB: ".$this->name."<br>");
		print("NameA: ".$this->Aref->name."<br>");
		$this->Aref->name="BA";
	}
}

$oa=new A;
$oa->CreateB();
$oa->CallB();
?>

Produces following debug infos:

./zend_execute.c(392) :  Freeing 0x084436BC (12 bytes), script=/var/www/users/test8/www/classleak2.php
Last leak repeated 1 time
zend_hash.c(271) :  Freeing 0x08445D54 (40 bytes), script=/var/www/users/test8/www/classleak2.php
Last leak repeated 3 times
zend_hash.c(200) :  Freeing 0x094428D4 (20 bytes), script=/var/www/users/test8/www/classleak2.php
Last leak repeated 1 time
./zend_execute.c(1895) :  Freeing 0x094419FC (48 bytes), script=/var/www/users/test8/www/classleak2.php
zend_API.c(208) : Actual location (location was relayed)
Last leak repeated 1 time
zend_compile.c(1554) :  Freeing 0x08419CBC (12 bytes), script=/var/www/users/test8/www/classleak2.php
./zend-scanner.l(1343) :  Freeing 0x095EA804 (2 bytes), script=/var/www/users/test8/www/classleak2.php
--------------------------------

Thanks!
 Hans Raaf
 [2000-07-16 13:21 UTC] h dot raaf at i-k-c dot net
I made another example of what I think is a problem:

<?php

class A {
	var $Bref=0;
}

class B {
	var $Aref=0;
}

$oa=new A;
$ob=new B;
$oa->Bref=&$ob;
$ob->Aref=&$oa;
?>

./zend_execute.c(392) :  Freeing 0x093D0C04 (12 bytes), script=/var/www/users/test8/www/classleak3a.php
Last leak repeated 1 time
zend_hash.c(271) :  Freeing 0x093D10D4 (40 bytes), script=/var/www/users/test8/www/classleak3a.php
Last leak repeated 1 time
zend_hash.c(200) :  Freeing 0x0970C79C (20 bytes), script=/var/www/users/test8/www/classleak3a.php
Last leak repeated 1 time
./zend_execute.c(1895) :  Freeing 0x097135E4 (48 bytes), script=/var/www/users/test8/www/classleak3a.php
zend_API.c(208) : Actual location (location was relayed)
Last leak repeated 1 time

 [2000-07-17 05:19 UTC] stas at cvs dot php dot net
PHP does not do circular references. Didn't I explain this yet?
 [2000-07-17 09:16 UTC] h dot raaf at i-k-c dot net
Sorry, maybe I am just stupid... :(

I really don't want to bother you... please excuse me!

I just don't understand where the 'circular reference' is in respect to my last example code and why the code works even if it uses something that is not supported by php4:

class A { var $Bref=0; }
class B { var $Aref=0; }
$oa=new A;
$ob=new B;
$oa->Bref=&$ob;
$ob->Aref=&$oa;

Would you be please so kind and explain it to me (or give me some reference to look it up by myself)
 [2000-08-15 14:32 UTC] waldschrott@php.net
1)
leaking memory is the intended behaviour using circular
references
2)
you can do (in your code)...

$oa->Bref->Aref->Bref->Aref->Bref.............
how often you want to, that?s circular


 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Mon Mar 25 03:01:25 2019 UTC