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: 2021-05-16 04:22 UTC
Votes:3
Avg. Score:4.3 ± 0.9
Reproduced:2 of 2 (100.0%)
Same Version:2 (100.0%)
Same OS:2 (100.0%)
From: alexander dot v at zend dot com Assigned: cmb (profile)
Status: No Feedback Package: Scripting Engine problem
PHP Version: 5CVS-2006-03-16 (snap) OS: all
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: alexander dot v at zend dot com
New email:
PHP Version: OS:

 

 [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

Pull Requests

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)
 [2017-10-24 07:56 UTC] kalle@php.net
-Status: Assigned +Status: Open -Assigned To: dmitry +Assigned To:
 [2021-05-03 11:37 UTC] cmb@php.net
-Status: Open +Status: Feedback -Assigned To: +Assigned To: cmb
 [2021-05-03 11:37 UTC] cmb@php.net
Is this still an issue with any of the actively supported PHP
versions[1]?

[1] <https://www.php.net/supported-versions.php>
 [2021-05-16 04:22 UTC] php-bugs at lists dot php dot net
No feedback was provided. The bug is being suspended because
we assume that you are no longer experiencing the problem.
If this is not the case and you are able to provide the
information that was requested earlier, please do so and
change the status of the bug back to "Re-Opened". Thank you.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon Oct 14 14:01:27 2024 UTC