#26673 [Opn->Fbk]: Nested object reference count not incremented when object copied?

From: Date: Fri, 02 Jan 2004 01:56:21 +0000
Subject: #26673 [Opn->Fbk]: Nested object reference count not incremented when object copied?
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-52682@lists.php.net to get a copy of this message
ID: 26673 Updated by: sniper@php.net Reported By: samlw at aol dot com -Status: Open +Status: Feedback Bug Type: Scripting Engine problem Operating System: * PHP Version: 4.3.4 New Comment: Please provide a bit better script than this. And what makes you think this isn't expected behaviour? Previous Comments: ------------------------------------------------------------------------ [2003-12-19 18:17:10] samlw at aol dot com Description: ------------ In certain cases, the reference counting mechanism seems to become confused when dealing with nested objects. If class 'outer' has a member that is an object of class 'inner', then a copy of an instance of outer sometimes shares the original's reference to inner. As a result, if a change is made to the inner member of the copy, the original's inner is also changed. See the attached code for a working example of this problem. I have reproduced this problem in v 4.3.2 and 4.3.4 running on OS X. Reproduce code: --------------- class inner { var $val1; var $val2; function inner ($val1, $val2) { $this->set($val1, $val2); } function set ($val1, $val2) { $this->val1 = $val1; $this->val2 = $val2; } } class outer { var $val0; var $inner; function outer ($val0, $val1, $val2) { $this->val0 = $val0; $this->inner = new inner($val1, $val2); } function set ($val0, $val1, $val2) { $this->val0 = $val0; $this->inner->set($val1, $val2); } } // construct and dump an object with values [0, 1, 2] $myOuter = new outer(0, 1, 2); var_dump($myOuter); echo '<br />--------<br />'; // change values to [3, 4, 5] $myOuter->set(3, 4, 5); var_dump($myOuter); echo '<br />--------<br />'; // make a COPY and change values to [6, 7, 8] $myOuterCopy = $myOuter; $myOuterCopy->set(6, 7, 8); var_dump($myOuterCopy); echo '<br />--------<br />'; // dump original - should still be [3, 4, 5], but it is [3, 7, 8]!!! // the inner object is not getting copied on modification!!! var_dump($myOuter); echo '<br />--------<br />'; Expected result: ---------------- The final var_dump should produce: object(outer)(2) { ["val0"]=> int(3) ["inner"]=> &object(inner)(2) { ["val1"]=> int(4) ["val2"]=> int(5) } } Actual result: -------------- The final var_dump produces: object(outer)(2) { ["val0"]=> int(3) ["inner"]=> &object(inner)(2) { ["val1"]=> int(7) ["val2"]=> int(8) } } ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=26673&edit=1

« previous php.bugs (#52682) next »