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

Most Wise Monks I recently added a post about Modules not going out of scope click here to view. After looking further into this I believe I made a mistake assuming the problem was to do with Mod_perl. Further investigations into the code (I didn't write the original code I so please don't slate me too much) I saw that within my session class it created a new object of type community, passing a reference to itself.
sub community { my $self = shift; $self->{'community'} = new Community($self); }
and in the startup of the Community class it does
sub new { my($class, $session) = @_ my $self = { }; bless($self, $class); $self->{'session'} = $session; }
Would I be right in saying this is causing a memory leak ? I need to be able to access the methods of the session object within the community object, so is there any way of accomplishing this without causing this memory leak ? I know I need to break this circular reference but not sure how to !! I looked on Super Search and found some similar nodes memory leak however they didn't solve my problem. I guess this problem is indeed whats causing the error logs to fill up with
Attempt to free unreferenced scalar !
Thanks in advance

Replies are listed 'Best First'.
Re: Is this a Memory leak ?
by broquaint (Abbot) on Feb 21, 2002 at 15:07 UTC
    One way to avoid the leak would be using weak references
    # declare stuff package stuff here ... use WeakRef; sub new { my($class, $session) = @_ my $self = { }; bless($self, $class); weaken($self->{'session'} = $session); }
    So when your instance goes out of scope it's refcount should reach 0 and be released.
    HTH

    broquaint

Re: Is this a Memory leak ?
by perrin (Chancellor) on Feb 21, 2002 at 17:59 UTC
    Yes, that will certainly cause a leak. Maybe you should reconsider the way you're structuring your objects. Is the session really an attribute of the community, or is an attribute of the current request context? You might try making a global class method that just returns the current session which you can call from anywhere. Something like Site::Util->get_session(); would do the trick. Then keep the session in a global (or $r->pnotes under mod_perl) that is only read and written to by that method.
Re: Is this a Memory leak ?
by rinceWind (Monsignor) on Feb 21, 2002 at 18:04 UTC

    Surely, each community has more than one session. If so, which session does the object hold? If the relation is really one to one, they should be the same object.

    Is there any reason why you need the circular object reference chain? Presumably, the relation of sessions to communities is many to one. In that case, a session object hasa community

    If you need to find which sessions belong to a community (presumably you are maintaining a list of active sessions), then scan this list for matching communities. This could be done as a method of the community class.

    --rW

Re: Is this a Memory leak ?
by ropey (Hermit) on Feb 22, 2002 at 09:51 UTC
    Thanks for all your help guys, Going to try WeakenRef just as soona s I can get the bloody thing to install :). As for the other points, I believe your right , i'm sure theres another way of doing it to avoid the circular reference, I have recently 'inherited' the project so am trying to understand exactly what everything does before re-doing it !! When I do I will certainly take those points on board! Again many thanks