Gary Stainburn wrote:
>
> My first forrey into Perl objects sees me trying to model a railway.  I've got
> a hash of named blocks of track, which is added to when I create a new block
> object.
>
> My problem is one of destroying a block (object), making sure that I have no
> memory leakage.
>
> I create a new track block such:
>
> my $T1=Trainset->track('T1','Block');
>
> This also created $Trainset::_BLOCKS{T1} which references the object.
>
> My problem is how can I destroy the object when I no longer want it?
>
> If I call
>
> $T1=$T1->delete;
>
> Then the object is destroyed (display in DESTROY shows this).  However, if I
> simply call
>
> $T1->delete;
>
> The object isn't destroyed because even though the ref in %_BLOCKS is deleted,
> the ref in $T1 is still there.  Is there and way to ensure that the object is
> destroyed - i.e. force the refcount to zero?
>
> The only solution I've come up with is to explicitly call DESTROY from within
> the delete function, and within DESTROY empty the hash. Is this sufficient,
> or will this still tie up memory?  Is there a better way?

Hi Gary.

We really need to see your 'delete' and 'DESTROY' methods. But you can get
around the problem by setting $T1 to anything that's not a reference to the
object. At present you're setting it to the result of your 'delete' method,
but you could equally write

  $T1 = 99;
or
  undef $T1;

Alternatively you could let $T1 go out of scope (at the end of a block)
and be implcitly deleted itself by Perl. That only works if it's a 'my'
variable.

HTH,

Rob



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to