Edit report at https://bugs.php.net/bug.php?id=36759&edit=1
ID: 36759 Comment by: re...@php.net Reported by: alexander dot v at zend dot com Summary: Objects destructors are invoked in wrong order when script is finished Status: Assigned Type: Bug Package: Scripting Engine problem Operating System: all PHP Version: 5CVS-2006-03-16 (snap) Assigned To: dmitry Block user comment: N Private report: N New Comment: I seems been fixed already, see: http://3v4l.org/b2JWc Previous Comments: ------------------------------------------------------------------------ [2011-03-21 03:04:57] tstarling at wikimedia dot org r216213 fixes that one test case, but it seems like the actual complaint here is that objects stored in member variables are destroyed before the objects that reference them. I can see three problems with r216213. Two of them mean that this bug is not fixed, and the third causes another bug. 1. It assumes that all objects which hold references to other objects will be in the global symbol table. For objects that aren't in the global symbol table, it fails. For example if you take the test case and replace this: $y = new Bar(); $x = new Foo($y); with this: function foo() { static $x, $y; $y = new Bar(); $x = new Foo($y); } foo(); then the destructors are called in the wrong order. 2. It only deletes objects which have a reference count of 1. When a zval is referred to by two symbol table entries, both symbol table entries point to the same zval, and both have a reference count of 2, so neither is deleted. So if you replace the same part of the test case with: $y = new Bar(); $x = new Foo($y); $z = $x; then the destructors are called in the wrong order. 3. It deletes variables, instead of just calling __destruct() on them as expected. This was a hack to reuse the reference-counting code in zval_ptr_dtor(). It's unnecessary and causes several user-visible side effects, such as bug #54157. ------------------------------------------------------------------------ [2011-03-21 01:35:02] s...@php.net According to Tim Starling, still happens, details to follow soon. ------------------------------------------------------------------------ [2006-07-12 08:57:50] dmi...@php.net Fixed in CVS HEAD and PHP_5_2. ------------------------------------------------------------------------ [2006-03-16 15:12:50] dmi...@php.net Reproduce code: --------------- <?php class Foo { private $bar; function __construct($bar) { $this->bar = $bar; } function __destruct() { echo __METHOD__,"\n"; unset($this->bar); } } class Bar { function __destruct() { echo __METHOD__,"\n"; unset($this->bar); } } $y = new Bar(); $x = new Foo($y); ?> Expected result: ---------------- Foo::__destruct Bar::__destruct Actual result: -------------- Bar::__destruct Foo::__destruct ------------------------------------------------------------------------ [2006-03-16 15:02:04] alexander dot v at zend dot com Description: ------------ Object destructors are invoked in an arbitrarily order. Objects from a global_symbol_table should be destructed first. Reproduce code: --------------- too large and not deterministic Expected result: ---------------- none Actual result: -------------- none ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=36759&edit=1