#20240 [Com]: order of destructor calls

From: Date: Fri, 18 Apr 2003 22:49:30 +0000
Subject: #20240 [Com]: order of destructor calls
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-37875@lists.php.net to get a copy of this message
ID: 20240 Comment by: thekid at thekid dot de Reported By: vincent dot planchenault at atempo dot com Status: Open Bug Type: Zend Engine 2 problem Operating System: Linux PHP Version: 5.0.0-dev New Comment: Running this script with current PHP5 gives me: 2 3 3 I guess this can be marked as fixed then? Previous Comments: ------------------------------------------------------------------------ [2002-12-10 14:07:39] tad at tadland dot net There appears to be a bigger problem. Consider the following code: <?php class test { var $member; function test() { $this->member = 1; register_shutdown_function(array($this, 'destructor')); } function destructor() { print $this->member; } function add() { $this->member += 1; print $this->member."<br>\n"; } } $t = new test(); $t->add(); $t->add(); ?> One might expect this code to output 2 3 3 But instead, one gets 2 3 1 So, it appears that a copy of the object is being made by register_shutdown_function(). Tad ------------------------------------------------------------------------ [2002-11-04 03:47:39] vincent dot planchenault at atempo dot com Using this small PHP code : <?php class Referenced { private $m_references=0; function __construct() { } function ref() { $this->m_references++; } function unref() { $this->m_references--; } function __destruct() { echo 'in Referenced Destructor...<br>'; echo "references = ".$this->m_references."<br>"; echo 'out of Referenced Destructor<br>'; } } class Referencer { private $m_ref; function __construct($t) { $this->m_ref = $t; $t->ref(); } function __destruct() { echo "in Referencer Destructor...<br>"; $this->m_ref->unref(); echo "out of Referencer Destructor<br>"; } } $t = new Referenced(); echo 'instanciating first Referencer object...<br>'; $a = new Referencer($t); echo 'instanciating second Referencer object...<br>'; $b = new Referencer($t); echo 'deleting first Referencer object...<br>'; unset($a); echo '<b>terminating</b><br>'; ?> I expected PHP to call unref() on Referenced object for the second Referencer instance in the Referencer destructor, and then let Referenced object be freed. This simulated output correspond to this expectation : instanciating first Referencer object... instanciating second Referencer object... deleting first Referencer object... in Referencer Destructor... out of Referencer Destructor terminating in Referencer Destructor out of Referencer Destructor in Referenced Destructor... references = 0 out of Referenced Destructor... PHP gave me this output : instanciating first Referencer object... instanciating second Referencer object... deleting first Referencer object... in Referencer Destructor... out of Referencer Destructor terminating in Referenced Destructor... references = 1 out of Referenced Destructor in Referencer Destructor... Fatal error: Trying to access invalid object in <blablabla>/test.php on line 42 It seems that object instances are freed BEFORE the call of the object destructor; I think object variables have to be freed after the destructor call... ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=20240&edit=1

« previous php.bugs (#37875) next »