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

Monks I hope this makes sense - I must say I'm struggling to get my head around it I'm not very experienced with mod_perl. Background My company offers a web product which allows users their own sub domains. All the content of the sub domains are dynamically generated from our database (user has contol over look and feel). The server runs mod_perl and all the Perl scripts and modules are compiled on the webserver startup using Apache::RegistryLoader. The httpd.conf is set up so the first thing that is called when a client accesses the site is index.cgi, this handles what data (i.e what site) to display to the client. There is another handler inside the conf
<Location /site/> SetHandler perl-script PerlHandler Apache::ROOT::cgi_2dbin::index_2ecgi PerlSendHeader On AllowOverride None
which I guess handles the url rewrite. The problem i'm having is that any modules loaded (and an object subsequently created) within this index.cgi never seems to go out of scope. For instance I have two modules Session and Community, where and instance gets created on every call of index.cgi. Just to see what was going on I added the constructor
sub DESTROY { my $self = shift; my $me = ref($self); print carp "i'm dying.....$me\n"; }
to both of the modules, now when the script has finished and the objects HAVE gone out of scope the distructors are not called. However very regularly I get 'attempt to free unreferenced scalar' which coincides with what the perdoc says about this problem. Now when I shut down the webserver I suddenly get a whole load of the modules shut down i.e.
[Tue Feb 19 11:08:57 2002] null: i'm dying.....Session [Tue Feb 19 11:08:57 2002] null: at /dev/null line 0 [Tue Feb 19 11:08:57 2002] null: i'm dying.....Community [Tue Feb 19 11:08:57 2002] null: at /dev/null line 0 [Tue Feb 19 11:08:57 2002] null: i'm dying.....Session [Tue Feb 19 11:08:57 2002] null: at /dev/null line 0 [Tue Feb 19 11:08:57 2002] null: i'm dying.....Community [Tue Feb 19 11:08:57 2002] null: at /dev/null line 0 [Tue Feb 19 11:08:57 2002] null: i'm dying.....Session [Tue Feb 19 11:08:57 2002] null: at /dev/null line 0 [Tue Feb 19 11:08:59 2002] [notice] caught SIGTERM, shutting down
Also on shutdoen I get a series of
[Tue Feb 19 11:11:39 2002] null: Attempt to free unreferenced scalar.
So it seems the objects never 'automatically' go out of scope !! Has anyone experienced anything like this. I have looked on supersearch but to no avail ! Any guidance will be greatly appreciated.

Replies are listed 'Best First'.
Re: Modules not going out of scope
by trs80 (Priest) on Feb 19, 2002 at 17:18 UTC
    I hate this type of reply, but I really have to do it.

    Did you read the mod_perl Guide?
    If you haven't read that, all of it, skim if you must, but it has some really good information in it. I am constantly amazed at how thorough that guide really is. I didn't say well organized, I said thorough.

    If you still don't find a satisfactory anwser there and no one here has then please direct your issues to the mod_perl mailing list. You can find links to it and more information at perl.apache.org

Re: Modules not going out of scope
by dash2 (Hermit) on Feb 19, 2002 at 14:40 UTC
    What sort of scope do you create these objects in? Are they my or what?

    dave hj~

      Thanks for your reply Dave I use strict, warnings on, the objects are created within a sub using 'my'. I stripped down the script to see what was going on but to no avail.
      #!/usr/bin/perl -w use CGI; use Session; use Community; use CGI::Carp; main(); sub main { my $query = new CGI; my $uri = 'www.moonfruit.com'; setup($uri, $query); } sub setup { my ($uri, $query) = @_; my $dbh = db_connect(); my $session= new Session($dbh, $query, "guest", $uri); my $community = new Community($session); print $query->header(); return; }
      So the objects should go out of scope once the sub has finished. However as I mentioned they don't automatically get 'destroyed'. I know this as the DESTROY method of the objects are not called, they are however called when I stop the server.
        This part of your code looks okay. It's possible that you have either circular references between objects, or a problem with closures. Problems with closures under Apache::Registry happen when you have a lexical variable declared outside the scope of a subroutine that uses it. This code above doesn't have that problem, but your real code might.
Re: Modules not going out of scope
by perrin (Chancellor) on Feb 19, 2002 at 14:54 UTC
    You haven't told us much about your program and how it stores these object references. Are you using globals? Those never get cleaned up with Apache::Registry. Maybe if you posted some code, we could be more helpful.
      See my reply to Dave, The code their is a stripped down version of the script (its too big to post) but it still suffers from the same problem. Thanks for your feedback though. Regards