php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #20240 order of destructor calls
Submitted: 2002-11-04 03:47 UTC Modified: 2003-06-01 12:44 UTC
From: vincent dot planchenault at atempo dot com Assigned:
Status: Closed Package: Scripting Engine problem
PHP Version: 5.0.0-dev OS: Linux
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 this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: vincent dot planchenault at atempo dot com
New email:
PHP Version: OS:

 

 [2002-11-04 03:47 UTC] 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...

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-12-10 14:07 UTC] 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
 [2003-04-18 17:49 UTC] thekid at thekid dot de
Running this script with current PHP5 gives me:

2
3
3

I guess this can be marked as fixed then?
 [2003-06-01 12:44 UTC] helly@php.net
This bug has been fixed in CVS.

In case this was a PHP problem, snapshots of the sources are packaged
every three hours; this change will be in the next snapshot. You can
grab the snapshot at http://snaps.php.net/.
 
In case this was a documentation problem, the fix will show up soon at
http://www.php.net/manual/.

In case this was a PHP.net website problem, the change will show
up on the PHP.net site and on the mirror sites in short time.
 
Thank you for the report, and for helping us make PHP better.


 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 09:01:27 2024 UTC