[Chicago-talk] linked list of object

Steven Lembark lembark at wrkhors.com
Fri Nov 11 10:36:32 PST 2005



-- Andy_Bach at wiwb.uscourts.gov

> Hey.
> 
> Our main program uses a linked list of objects and passes around global 
> pointers to the head of the list.  They often walk the chain by:
>  my $LocalList = $main::gListPtr;
> 
>   while ($LocalList) {
> ....
>    $LocalList = $LocalList->next();
>   }
> 
> (yes, the next() method returns the next object).  Just looking at some 
> code where they undef() the global pointer before appending a new list
> and  got to wondering if GC would be then able to recover the 'orphaned' 
> objects.  The first on the list won't have an refs so it can go and so
> the  2nd ... etc.  Just wondering if this *not* a good idea or what a way
> to  examine the memory overhead.

This breaks if you use doubly-linked lists; which can 
be fixed by weakening the references.

You might want to weaken the links sent out to sub-calls
and keep the list-head alive in a struct in the creating
class or its client. That saves you from having problems
with metadata keeping the objects alive accidentally.

A leak test basically creates and tears down a million 
object links and asks if the amout of memory in use
afterwards is different than at the start. There are
good memory leak tests in XML::LibXML and DBD::mysql
if you want examples.


-- 
Steven Lembark                                       85-09 90th Street
Workhorse Computing                                Woodhaven, NY 11421
lembark at wrkhors.com                                     1 888 359 3508


More information about the Chicago-talk mailing list