locked_user sundialsvc4 has asked for the wisdom of the Perl Monks concerning the following question:

I thought that this code-snippet would call DEMOLISH (in Moose)... but it doesn't seem to do so:

my $foo = Some::Moose::Object->new(); $foo = undef; # Here...

Assuming (say...):

package Some::Moose::Object; use Moose; sub DEMOLISH { print STDERR "Vanity, vanity, all is vanity...\n"; };

I would intuitively assume that when I set the variable to "undef," it is now “out-of-scope” such that a DESTROY/DEMOLISH routine would now run. However, is my assumption simple-minded?

If that be so, oh monks, when does DESTROY run? I was considering to use it for flushing session-data to disk...

Replies are listed 'Best First'.
Re: Am I mistaken about Moose 'DEMOLISH'?
by locked_user sundialsvc4 (Abbot) on Jan 23, 2009 at 18:25 UTC

    Following your suggestion, I stumbled-upon Test::Memory::Cycle (from my re-re-readings of Task::Kensho), and ran it.

    Although there were no strong memory-cycles, there are two weakened ones:

    # Cycle #1 # BAH::MyMojo::Context A->{_session_schema} => # BAH::Schema::WebSchema B # BAH::Schema::WebSchema B->{source_registrations} => %C # %C->{WsSession} => DBIx::Class::ResultSource::Table D # w->DBIx::Class::ResultSource::Table D->{schema} # => BAH::Schema::WebSchema B # Cycle #2 # BAH::MyMojo::Context A->{_session_schema} # => BAH::Schema::WebSchema B # BAH::Schema::WebSchema B->{storage} # => DBIx::Class::Storage::DBI::mysql E # w->DBIx::Class::Storage::DBI::mysql E->{schema} # => BAH::Schema::WebSchema B

    My guess is that the schema-classes are causing the problem. (I confess that I don't quite understand how to read the dump... is the percent-sign in “%C” meant to signify a weak-ref?) But anyhow, I can work around the issue, and I think that I just discovered another useful test-tool.

    Hmmm....   No matter what you want, or want to do, it's in CPAN somewhere ... if only you can (a) find it, and (b) understand it once you find it. My batting-average so far is not that good...   :*)

Re: Am I mistaken about Moose 'DEMOLISH'?
by Fletch (Bishop) on Jan 23, 2009 at 17:41 UTC

    Strange, works fine for me.

    $ cat Foo.pm package Foo; use Moose; sub DEMOLISH { print STDERR "I R GONE\n"; } 1; __END__ $ perl -MFoo -e 'my $f = Foo->new; print qq{\$f is $f\n}; $f = undef;' $f is Foo=HASH(0xa073a8) I R GONE

    Moose 0.65, Class::MOP 0.76, perl 5.8.8 on OS X 10.5.6.

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

Re: Am I mistaken about Moose 'DEMOLISH'?
by ikegami (Patriarch) on Jan 23, 2009 at 18:02 UTC

    Are you sure you didn't do something more like

    my $foo = Some::Moose::Object->new(); my $bar = $foo; $foo = undef;

    There's still a reference to the object, so it can't be destroyed yet.

Re: Am I mistaken about Moose 'DEMOLISH'?
by locked_user sundialsvc4 (Abbot) on Jan 30, 2009 at 02:34 UTC

    I'd like to "re-ping" this thread to see if there are any further Monkish thoughts on it, because I suspect that it might well be a genuine issue in my program ... and if so I need to confirm it.

    A key feature of the app is that it has a Mojolicious "context" object which, among other things, provides a reference to a DBIx::Class schema. My first suspicion is that I notice that the DESTROY method isn't being called when I think it should. The second clue is that, during a live-demo test today (this being, o'course, the only time when really weird things happen), I got a "out of database handles"-type message ... on a freshly booted single-user laptop. Hmmm....

    How might I proceed?