You are here: Re: [PHP] references, circular references, oop, and garbage collection in PHP5 « PHP « IT news, forums, messages
Re: [PHP] references, circular references, oop, and garbage collection in PHP5

Posted by Curt Zirzow on 12/08/05 09:34

On Wed, Dec 07, 2005 at 09:30:36PM -0500, Alan Pinstein wrote:
>
> On Dec 7, 2005, at 12:36 AM, Curt Zirzow wrote:
> >
> >I'm not sure how you mean a weak reference, and well a refcount is
> >rather meaning less in php userland.
>
> So, this gets interesting. I don't know if you're familiar with the
> circular-reference problem. But if you have two instances that have
> references to each other, even once you remove all references to the
> objects, they will not be GC'd since they have a mutual deadlock on
> each other:
>
> $a = new MyObj;
> $b = new MyObj;
> $a->setB($b); // does $this->b = $b;
> $b->setA($a); // does $this->a = $a;
>
> $a = NULL;
> $b = NULL;
>
> The actual instances pointed to by $a and $b WILL NOT GET FREED HERE
> as you would *wish*. However this is expected behavior.

I would have to disagree with the 'as you would *wish*' part.

if I pass something to a function/method via copy (vs reference) i
would hardly expect code outside the function/method to affect
their existance.

>
> Only by changing MyObj to store "weak references", that is references
> to the objects that are NOT reference-counted, can you get the GC to
> free the instances.
>
> function setB(&$B) { $this->b = &$a; }
> function setA(&$B) { $this->a = &$b; }
>
> Now, the instances will be freed when the $a and $b are null'd out.

I think you meant to type:

function setB(&$a) { $this->b = &$a; }
function setA(&$b) { $this->a = &$b; }

And consider the definition written as:

function setB(&$a) { $this->b = $a; }
function setA(&$b) { $this->a = $b; }

does the $a = null; and $b = null; outside the class cause the
objects to get null'd inside the class as well?

> So, while I now feel more confident of how references act with
> respect to objects (which is, they act the same as they do for any
> variable), I still am not sure what "$this->this" is and why it
> worked so magically.

You lost me on your $this->this statement.

Curt.
--
cat .signature: No such file or directory

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация