php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #9477 Object is destroyed before references all released
Submitted: 2001-02-27 08:57 UTC Modified: 2001-06-27 01:04 UTC
From: passionplay at hotmail dot com Assigned:
Status: Closed Package: Documentation problem
PHP Version: 4.0.4pl1 OS: Linux-Mandrake
Private report: No CVE-ID: None
 [2001-02-27 08:57 UTC] passionplay at hotmail dot com
standard php rpm 4.04p1 from rpmfind.net

Scripts are clearly marked and available in source format as well as showing necessary details at this URL.

http://www.poetryunlimited.com/test

This bug will create significant problems when dealing with object references as needed in advanced patterns (GoF 1994).

Hope you guys can figure it out. I would hate to have to have extra objects kicking around just because of whatever.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2001-03-08 06:43 UTC] stas@php.net
Could you please describe more in detail what the problem is
and provide a short scipt reproducing the behaviour?
 [2001-03-08 08:30 UTC] passionplay at hotmail dot com
Come on guys. Read the damn bug report. I sent you a complete URL with scripts that generate the problem even. Is it really that hard to follow?????????

If you read the bug report and went to the URL you would have seen the scripts that generated the problem. How hard is that? You can even see the output from the bug. Shall we try this again? I will clarify in case you missed it the first time.

GO TO THE URL LISTED. THERE YOU WILL FIND THE SHORT SCRIPT THAT GENERATES THE PROBLEM. YOU WILL ALSO SEE RESULT FILES THAT DESCRIBE THE ERROR IN DETAIL. ONCE YOU HAVE LOOKED THERE, COME AND ASK ME AGAIN TO CLARIFY. THANKS.
 [2001-03-08 16:16 UTC] torben@php.net
I'm sure the bugs are very obvious to you. However, your bug report, as given, really is
pretty useless. Salient information would include: what results were you expecting,
compared to the results you got? What, exactly, did not work as you expected? And when
we say 'short' script, we really mean 'short'. Can you provide a quick, obvious, and 
hopefully <= 25-50 line script which demonstrates what's bothering you? Many PHP
developers work on PHP in their free time, and attempting to dig through 313 lines of
someone else's code--when you have only given a vague idea of what we're supposed 
to be looking for--is unlikely to get high on anyone's priority list. Especially not when you 
get sarcastic and abusive.



 [2001-03-08 16:55 UTC] passionplay at hotmail dot com
a) The comment I got in my eyes said there was nothing that was looked at.
b) I do open source work too.
c) I have tried over and over in the past to get things included in php that were overlooked for almost 3 years until they finally got put in. Specifically detecting the different datatypes that some of the databases generate as field types.
d) The URL I gave has a total of 4 files. 2 source files. And to active scripts that when run, generate the results.
e) The one marked Normal, which completes with no errors, is obviously correct.
f) The other one fails.
g) There is only one line difference in the entire code.
i) I have a global object passed by reference that is then assigned to an array.
j) When this object reference in the array is set to nothing, the global object is destroyed.
k) If on the other hand I use a separate array to identify what array elements are defunt, the object still exists.
l) If you ran the non-normal script by clicking on it, it would report the line where the error occurred.
m) In my scripts, which unfortunately are part of another open source project, I have VERY CLEARLY marked the section of the code that is different in each instance.
o) If someone is wading through 313 lines of code to find my bug, then they really didn't think to click on the script, and I guess THAT is truly my REAL error as I did not specify that I had set up two identical scripts with one line that is different such that one completes and the other fails using the same algorithm, with one minor difference.
p) I'm just wondering whether anyone tried clicking on the scripts and saw what I indicated or looked at the script in detail long enough to look at the offending line and the demarcated difference in each case.
q) If my comments were interpreted as abusive, I apologize, but understand that when I have taken all the steps outlined and someone still tells me they cannot reproduce the behavior, or they cannot understand it, it tells me they didn't look at the problem. Had someone simply said, I don't understand what's different, I wouldn't have felt like no one gave a damn to even look at my bug report and had dropped it.
r) In the past my bug reports have been accompanied by no less than a full source code solution to the error discovered and you'll have to pardon my disillusionment in providing a solution where my answers are obviously ignored regardless of whether they are correct or not.

Bottom line: global object passed as a reference to a method and then assigned to an array which is a member of another object.  The second object is then requested to delete the reference to global object. At the moment the reference in the array is deleted, the global object goes kaboom too.

You can see this in the scripts at that URL because I have each object serialized as output prior to the error line as well as each object reporting it's operation so you can see where things are running.

Did I miss anything in helping debug this issue or did I just do too much?
 [2001-03-08 17:24 UTC] andre@php.net
> Did I miss anything in helping debug this issue or did I 
> just do too much?

you did the wrong thing!
the following passage is the only one I have read, I have
not visited your site, nor anything else (we simply do not
have the time to do this kind of things, it's exactly the
same in any bigger OS project I know of (eg. bugzilla,
provide minimized testcases or your bug will never be fixed)

<quote>
Bottom line: global object passed as a reference to a method
and then assigned to an array
which is a member of another object.  The second object is
then requested to delete the
reference to global object. At the moment the reference in
the array is deleted, the global
object goes kaboom too.
</quote>

what I have done then, is creating PHP source reading the
words above:

function foo (&$foo) {
   $zoo->array['foo']=&$foo;
   unset($zoo->array['foo']); // simplified
   }
$foo->test=TRUE;
foo($foo);
var_dump($foo);

***
This is works for me and this is how we expect code
snipplets, feel free to modify it to match exactly your
problem...


 [2001-03-08 20:07 UTC] passionplay at hotmail dot com
I'm really getting tired of this. I guess what I REALLY needed to do is strip out the do nothing functions in the skeletons I had on that page because even though they were base classes, everyone was missing the point.

Here is the simplest case that generates an error. Anyone want to take a stab at why it happens?

<?php
class obj {
  var $array;
  // Assigns object reference to the associative array
  function test($name,$obj){$this->array[$name]=&$obj;}

  // Removes the reference from the associative, supposedly
  // but what it does decrease the actual object to nothing
  function junk($name){$this->array[$name]="";}

  // If I do it this way it works. The object isn't nuked.
  // What gives? Do I *HAVE* to use =&$ when assigning refs?

  // Is there an array function that can be used to simply
  // remove the array element out, w/o expensive copying?
  function nojunk($name){$t="";$this->array[$name]=&$t;}
};

// Our do nothing class.
class me{
  function test(){echo "Hello, I'm still valid\n";}
};

// Please don't tell me not to send the reference to the
// object because I need to have only one copy of the object
// and not a million of them especially since these objects
// might be opening files and sockets.
$m=& new me();
$o=& new obj();

// This section works
$o->test("test",&$m);
$m->test();
$o->nojunk("test");
$m->test();

// This section fails....
$o->test("test",&$m);
$m->test();
$o->junk("test");
// Right here
$m->test();
?>

So? What's the deal? Once you assign a reference to a variable, you're done? You can't ever reassign the reference? Does what I'm proposing make sense syntactically speaking? I want the array to contain the reference to the object, but then I want to be able to unassign the reference from the array so that that particular object doesn't get used. An unregistration as it were.
 [2001-03-08 20:55 UTC] andre@php.net
bogusifying

apparently you do not know what you are doing here exactly,
please read "references explained" from the manual again...

/* your version - you do not destroy the reference, you
simply overwritte all referenced variables (remember they
are all ONE variable know) with "" */
function junk($name){$this->array[$name]="";}


/* corrected version - this one does what it is written in
your description */
function junk($name){unset($this->array[$name]);}

the corrected one works for me.

/* to clear things up a bit - the following code does the
following: remove the reference by replacing it with another
(to $t), I could imagine you thought it does something
different */
function nojunk($name){$t="";$this->array[$name]=&$t;}


please note I still did not read your comments, your code
reduced code was enough to understand the situation completely

note: please do not use pass-by-ref in call time
 [2001-03-09 00:32 UTC] passionplay at hotmail dot com
I retract this bug. I think the issue is that I was looking for pointer semantics in a language that does not support it but at the same time is not strongly typed so that what appears to be correct, does not in fact reset the reference but actually overwrites the original object by assignment.

Thank you all for your help. I now have a proper reference implementation of the subscriber/publisher pattern for PHP.

Hopefully, I'll be able to keep this all straight for any other patterns I conver to PHP.

Just out of curiosity, has ANYONE questioned the whole =& new construct's necessity in terms of why the semantics allows for = new as well??? When would you want to assign the a copy of the object you just created?

Any help would be appreciated.
 [2001-03-09 01:28 UTC] andre@php.net
this all makes sense and generally you do NOT need to use
"=& new" your case included, this is due to PHPs nature and
this all has been discussed
search the archives (marc.theaimsgroup.com) for
"reference counting" and "this+constructor" or maybe "=&" to
understand when it required to use "=&" with new

there are some resources on reference counting in the manual
or at zend.com IRC
 [2001-03-09 01:54 UTC] passionplay at hotmail dot com
Ok. This is absolutely amazing. I went to the archives. There are a good number of people all wondering about the same issue. Why is =&new required when every other language semantically understands that new is an operator and =new is not an assignment but rather implicit construction of an object?

Any takers?
 [2001-04-28 15:36 UTC] jmoore@php.net
Docu problem not script engine problem.

- James
 [2001-06-27 01:04 UTC] danbeck@php.net
Resubmitting as a new documentation bug.  Much of this has nothing to do with what needs to be documented.
 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Thu Jul 18 11:01:25 2019 UTC