Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

Re: when is destroy function called

by tobyink (Abbot)
on Feb 27, 2013 at 16:53 UTC ( #1020924=note: print w/replies, xml ) Need Help??

in reply to when is destroy function called

Maybe this is helpful?

use v5.14; package Mouth { use Moo; has teeth => (is => 'ro'); sub DESTROY { say 'Mouth->DESTROY' } } package Head { use Moo; has mouth => (is => 'ro'); sub DESTROY { say 'Head->DESTROY' } } my $mouth = Mouth->new(teeth => []); my $head = Head->new(mouth => $mouth); say 'undefining $mouth'; # Note: DESTROY not called, because $head still refers to $mouth undef $mouth; say 'undefining $head'; # Now both DESTROY methods will be called undef $head;

Compare that with the ZombieHead which allows its mouth to randomly disintegrate by using a weak reference...

use v5.14; package Mouth { use Moo; has teeth => (is => 'ro'); sub DESTROY { say 'Mouth->DESTROY' } } package ZombieHead { use Moo; has mouth => (is => 'ro', weak_ref => 1); sub DESTROY { say 'Head->DESTROY' } } my $mouth = Mouth->new(teeth => []); my $head = ZombieHead->new(mouth => $mouth); say 'undefining $mouth'; undef $mouth; # DESTROY is called!! say 'undefining $head'; undef $head;

References can be weakened using Scalar::Util.

package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name

Replies are listed 'Best First'.
Re^2: when is destroy function called
by 7stud (Deacon) on Feb 27, 2013 at 18:26 UTC

    Maybe this is helpful?

    I don't think your first example helps answer the op's question. Did the gc destroy the objects immediately after you undefined $head or did the gc merely mark them as ready for destruction, and then when the program ended the memory was released?

    Some evidence from "Intermediate Perl (2nd)", p. 257:

    To do the proper cleanup operations when Perl destroys an object, we need to know when that happens. Thankfully, Perl provides such notification upon request. We can request this notification by giving the object a DESTROY method.
    When the last reference to an object, say $bessie, disappears, Perl invokes that objectís DESTROY method automatically, *as if we had called it ourselves*:
    This method call is like most other method calls: Perl starts at the class of the object and works its way up the inheritance hierarchy until it finds a suitable method. However, unlike most other method calls, thereís no error if Perl doesnít find a suitable method.

    That suggests that DESTROY is called immediately after the reference count goes to 0--in other words the object is not merely marked for destruction with DESTROY being called at the gc's leisure.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1020924]
and cookies bake in the oven...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (4)
As of 2018-02-21 05:50 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (275 votes). Check out past polls.