Bug #67840 [Com]: Object is not destructed after disassigned even if refcount was 1

From: Date: Mon, 18 Aug 2014 08:02:01 +0000
Subject: Bug #67840 [Com]: Object is not destructed after disassigned even if refcount was 1
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-187161@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=67840&edit=1 ID: 67840 Comment by: strate at yandex dot ru Reported by: strate at yandex dot ru Summary: Object is not destructed after disassigned even if refcount was 1 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: bwoebi@php.net, thank you for explanation! But I missed one thing - you tell that $this destructed only when gc fired. But this should mean that all objects should be destroyed only on gc hit, because all objects has cycle references, yes? But, in my example, object of B class destructed right after setting $b to false. ----- About refcounter of object. Is there any documentation about it? Because I am trying to found memory leaks in my application and I am really can not understand why my objects are not destroyed even with refcount == 1. Previous Comments: ------------------------------------------------------------------------ [2014-08-16 20:48:05] bwoebi@php.net 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! ------------------------------------------------------------------------ [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

« previous php.bugs (#187161) next »