Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

phillup (4419)

phillup
  (email not shown publicly)
http://www.philsplace.org/

Journal of phillup (4419)

Thursday November 11, 2004
04:16 PM

Unexpected behavior

[ #21817 ]

I'm experiencing behavior that was not expected, and I'm wondering if this is "normal".

I have a "session" object that has a "has a" relationship with CGI::Session.

If I make sure to undef my object before the program ends, then everything is fine.

However, if I don't specifically undef my session object... and instead rely on the default garbage collection... it looks like the CGI::Session object I'm using gets destroyed BEFORE my session object gets destroyed.

It would seem to me that there should still be a reference count to the CGI::Session object as long as my object exists and that my session object would be destroyed first. Isn't this the way it is supposed to happen?

I managed to squeak out a diagnostic message that said

DESTROY created new reference to dead object

and have litterd my code with diagnostic print statements. Everything is fine until I make a call to the CGI::Session's param method and then the program spits out:

Can't call method "param" on an undefined value...

I am about to write a "minimal" object to try and replicate (and hopefully fix) the problem. But, I was wondering if I'm just totally mixed up on Perl's garbage collection.

TIA

---

UPDATE: By adding a print statement to my destructor and the destructor in CGI::Session I can definitely say that CGI::Session is being destroyed before my object is.

---

UPDATE 2: I've posted code that demonstrates the problem in a new journal entry.

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.
  • I've seen similar weirdness when DESTROY is being called during the interpreter shut down. It seems like sometimes contained objects get destroyed before their containers.

    During other object destruction (when an object goes out of scope but the program keeps going), the order of destruction seems to be correct.
    • Yes, it is when the program ends.

      Interestingly, if I have something like this then everything is OK.

      #! /usr/bin/perl
       
      use strict;
      use warnings;
       
      use My::Session;
      my $session = new My::Session;
       
      print $session->valid() ? "Session is valid\n" : "Session is NOT valid\n";

      But, if I do something like this then I see the error message.

      #! /usr/bin/perl
       
      use strict;
      use warnings;
       
      use My::Session;
      my $session = new My::Session;
       
      sub skipme{
        my $id = $session->id();