Bug #67840 [Opn->Nab]: Object is not destructed after disassigned even if refcount was 1
Edit report at https://bugs.php.net/bug.php?id=67840&edit=1
ID: 67840
Updated by: bwoebi@php.net
Reported by: strate at yandex dot ru
Summary: Object is not destructed after disassigned even if
refcount was 1
-Status: Open
+Status: Not a bug
Type: Bug
Package: *General Issues
Operating System: Ubuntu
PHP Version: 5.5.15
Block user comment: N
Private report: N
New Comment:
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to
report
a bug at http://bugs.php.net/how-to-report.php
B::$instances[spl_object_hash($b)] = &$b;
// ...
$b = false;
In this case, B::$instances[$the_hash] will be false ($b being a referencing variable [it's the
variable reference which is assigned, not an object reference]) too, so there's no pointer
outside nor inside the object to the object. It'll be destructed.
If you'd have written:
B::$instances[spl_object_hash($b)] = $b; // note: no reference
You'd observe the same behaviour as in A.
In case of A, you assign the reference to $this, but $this won't be changed, whether it's
a reference or not makes no difference. So it still will have some pointer inside the object to the
object itself - it'll only be destructed when the garbage collector fires.
---
Some note about your refcounts... Objects have a separate refcounter, the variable outside the
object, holding it, is not the same one than $this!
Previous Comments:
------------------------------------------------------------------------
[2014-08-14 08:36:09] strate at yandex dot ru
Description:
------------
If we store reference to $this inside object's constructor to external variable, object will
not destructed.
As you can see object A destructed only at the end of script, but refcount was equals to 1 right
before invalidating variable, containing object.
Seems to be bug, because if we will store reference to variable, containing object outside the
constructor, everything works as expected.
Test script:
---------------
https://gist.github.com/Strate/b037e529ee752489a45f
Expected result:
----------------
php refcounttest.php
b: (refcount=2, is_ref=1)=class B { }
Expecting object destruction ... Object B destructed! Expectation finished
a: (refcount=1, is_ref=0)=class A { }
Expecting object destruction ... Object A destructed! Expectation finished
End of script
Actual result:
--------------
php refcounttest.php
b: (refcount=2, is_ref=1)=class B { }
Expecting object destruction ... Object B destructed! Expectation finished
a: (refcount=1, is_ref=0)=class A { }
Expecting object destruction ... Expectation finished
End of script
Object A destructed!
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=67840&edit=1
Thread (3 messages)