in reply to Global objects and GC

When you say global, do you mean a lexical with package or file-level scope or do you mean a true global accessible through the symbol table?

If the former, as I suspect, then Perl's reference counting GC may be tripping you up. In that case, every subroutine that refers to a lexical in an outer scope is a closure and increases the refcount of the lexical. At shutdown time, when Perl GCs the subroutines themselves, that reference count decreases.

It sounds to me like you need a separate finalize() method to call explicitly, rather than waiting for DESTROY(). That way, you can avoid the rather messy and unspecified order of destruction errors I think you're having.

Perl's global destruction is a race for the exit. Who knows what order things will happen?

Replies are listed 'Best First'.
Re: Re: Global objects and GC
by TimToady (Parson) on Mar 04, 2004 at 21:40 UTC
    If oyasuminasai-san isn't using a lexical, that would be the first thing to try. Lexicals should always go away before globals.
Re: Re: Global objects and GC
by larsen (Parson) on Mar 05, 2004 at 11:38 UTC
    I had similar troubles with global destruction. I found this thread (Object reference disappearing during global destruction) and I thought I understood what was my error: to rely on the reference-count mechanism even during global destruction. Seeing the topic popping out again, I decided to do some experimentations in order to acquire a (?better) grasp on the subject. Here's the code:
    use strict; use warnings; package Bar; use Devel::Peek 'Dump'; sub new { bless [], shift } sub bar { print "Hi, I'm a bar instance\n\n" } sub dump { print "This is my ID card:\n"; Dump( shift ) } package Foo; sub new { my $bar = Bar->new(); bless { bar => $bar }, shift; } sub DESTROY { my $self = shift; $self->{ bar }->bar(); $self->{ bar }->dump(); } package main; my $f = Foo->new();
    My expectation was to see a message like Can't call method "bar" on an undefined value with unpredictable frequence. I run the script several times, without warnings. Since this is a semidecidible issue, and I don't have all that time :), I ask if my understanding is correct, and further explanations if appropriate. Thank you.

      At the end of the program, I'd expect $f to go out of scope first, calling its DESTROY. Since it contains a reference to a Bar, the contained object should still exist, so things are okay.

      If $f were global or if it had more than one reference, things might be stickier.

Re: Re: Global objects and GC
by Anonymous Monk on Mar 05, 2004 at 07:26 UTC
    It is a lexical with file level scope. And I suspected that the closures were incrementing the reference count... that's why I tried removing those closures to see what happenned. I guess I was trying to avoid forcing the user to remember to call the finalize method - the people that will use my library are not what you would call expert Perl programmers and I wanted to make it so that changes were not committed unless they knew what they were doing... Thank you for your help - I will most likely just throw in the finalize and perldoc it that way.

    oyasuminasai