php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #36759 Objects destructors are invoked in wrong order when script is finished
Submitted: 2006-03-16 15:02 UTC Modified: 2011-03-21 01:34 UTC
Votes:1
Avg. Score:3.0 ± 0.0
Reproduced:0 of 0 (0.0%)
From: alexander dot v at zend dot com Assigned: dmitry
Status: Assigned Package: Scripting Engine problem
PHP Version: 5CVS-2006-03-16 (snap) OS: all
Private report: No CVE-ID:
Have you experienced this issue?
Rate the importance of this bug to you:

 [2006-03-16 15:02 UTC] 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

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-03-16 15:12 UTC] dmitry@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-07-12 08:57 UTC] dmitry@php.net
Fixed in CVS HEAD and PHP_5_2.
 [2011-03-21 01:34 UTC] stas@php.net
-Status: Closed +Status: Assigned
 [2011-03-21 01:35 UTC] stas@php.net
According to Tim Starling, still happens, details to follow soon.
 [2011-03-21 03:04 UTC] 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.
 [2013-02-20 09:10 UTC] reeze@php.net
I seems been fixed already, see: http://3v4l.org/b2JWc
 [2013-02-20 09:12 UTC] reeze@php.net
Sorry I just try to reproduce, but didn't try the later script.
 [2014-03-09 11:47 UTC] ocramius at gmail dot com
Is the scope violation supposed to throw a fatal? While discussing with others, it seems like it is a copy-paste mistake.

It seems strange to me that PHP is not preventing access to that private property in the child class' __destruct() method.

If it is a copy-paste mistake, then this spawns another bug.
 [2014-03-09 11:55 UTC] ocramius at gmail dot com
Never mind, I've misread the test and was considering "Bar extends Foo" (isn't an inheritance)
 
PHP Copyright © 2001-2014 The PHP Group
All rights reserved.
Last updated: Wed Apr 16 22:02:05 2014 UTC