Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Status Update 3: In a potentially unrelated issue, while working on the code base that originally prompted the question I ran into another garbage collection issue on a different windows machine. I'm still in the process of getting the test suit to fully pass after the fix but I thought it useful to post what I found since it is relevant to at least one thread in this overall node. For those in the know it turns out that perl does two-stage garbage collection and for objects that have circular references (which mine do in spades) the object destruction is deferred to the exit phase not just when the variable where the object is stored goes out of scope. There is a warning about this in perldoc for perl 5.8 but the perldocs for 5.22 Destruction are silent on this issue luckily my google foo was strong when I found this. Additionally, The very underrated but handy Perl Cookbook has a specific recipe (13.13) to fix this as well! I have included two sets of example code that represent information gleaned from both sources. The first download is an object module that is self referential but still cleans up as soon as it goes out of scope (Ring.pm) and the second is a script which uses the module for demonstration and then shows some de-referencing magic for simple recursively referenced objects. I have chosen to use the Cookbook style solution in my case.

Ring.pm

#!/usr/bin/env perl package Ring; # return an empty ring structure sub new { my $class = shift; my $node = { }; $node->{NEXT} = $node->{PREV} = $node; my $self = { DUMMY => $node, COUNT => 0 }; bless $self, $class; return $self; } # $node = $ring->search( $value ) : find $value in the ring # structure in $node sub search { my ($ring, $value) = @_; my $node = $ring->{DUMMY}->{NEXT}; while ($node != $ring->{DUMMY} && $node->{VALUE} != $value) { $node = $node->{NEXT}; } return $node; } # $ring->insert( $value ) : insert $value into the ring structure sub insert_value { my ($ring, $value) = @_; my $node = { VALUE => $value }; $node->{NEXT} = $ring->{DUMMY}->{NEXT}; $ring->{DUMMY}->{NEXT}->{PREV} = $node; $ring->{DUMMY}->{NEXT} = $node; $node->{PREV} = $ring->{DUMMY}; ++$ring->{COUNT}; } # $ring->delete_value( $value ) : delete a node from the ring # structure by value sub delete_value { my ($ring, $value) = @_; my $node = $ring->search($value); return if $node == $ring->{DUMMY}; $ring->delete_node($node); } 1;

Recursive_Object_Test.pl

#!/usr/bin/env perl package Subtle; sub new { my $test; $test = \$test; warn "CREATING " . \$test; return bless \$test; } sub DESTROY { my $self = shift; warn "DESTROYING $self"; } package main; warn "starting program"; { my $a = Subtle->new; my $b = Subtle->new; $$a = 0; # break selfref warn "leaving block"; } warn "just exited block"; warn "time to die..."; use Smart::Comments; use Ring; $COUNT = 10; for (1 .. 3) { my $r = Ring->new(); for ($i = 0; $i < $COUNT; $i++) { $r->insert_value($i) } ### $r } exit 1;

Update: removed link to the pirated copy of the Perl Cookbook with apologies


In reply to Re: Hard to Debug windows memory error by jandrew
in thread Hard to Debug windows memory error by jandrew

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (5)
As of 2024-04-16 06:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found