Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: Hard to Debug windows memory error

by jandrew (Chaplain)
on Mar 05, 2016 at 20:06 UTC ( [id://1156896]=note: print w/replies, xml ) Need Help??


in reply to Hard to Debug windows memory error

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1156896]
help
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found