Beefy Boxes and Bandwidth Generously Provided by pair Networks RobOMonk
Think about Loose Coupling
 
PerlMonks  

Re^2: A way to test that circular references are actually freed

by Jeffrey Kegler (Friar)
on Oct 10, 2007 at 01:05 UTC ( #643839=note: print w/ replies, xml ) Need Help??


in reply to Re: A way to test that circular references are actually freed
in thread A way to test that circular references are actually freed

It uses trickery from the Scalar::Util module. There were a lot of gotchas involved -- avoiding autovivification and avoiding strengthening weak references by copying. Limitations are that you've got to pass a closure and create the object in that. And it's totally destructive (at least it is when the test is successful). Anything returned by the closure is potentially (and hopefully) gone forever.

Perhaps the most severe limitation is that it can't have references to any globals or any memory which is supposed to hang around -- it's not smart about that, but just assumes everything must go. If there's interest I may in a future version allow the caller to specify exceptions

The basic trick is to create weak references to the strong references and strong references to the weak references, free up the base reference, and see if all the others go to undefined. I've just added some code to make it smart about undefs in the original object.

I'll get it on CPAN in a couple of days, and you'll be able to read the code. It's not long.

jeffrey


Comment on Re^2: A way to test that circular references are actually freed
Re^3: A way to test that circular references are actually freed
by Jeffrey Kegler (Friar) on Oct 10, 2007 at 03:13 UTC
    Here it is in all its alpha status glory. (It needs some formatting and more comments.) It passes a simple test suite, and has handled very complicated structures from another CPAN project (the reason I originally wrote it). I've applied to PAUSE for the name Test::Weaken.

    There does seem to be a bug with the count of the total references, but it does not affect the count of freed references, and therefore fail/succeed reporting is unaffected.

    UPDATE: I've removed the code. Test::Weaken is now on CPAN and anyone can see the latest version there. It's still alpha, but it has a start in the way of documentation and there are test scripts, which make good examples.

    The version in this post did turn out to have the bug mentioned above, which is fixed in the CPAN version. I was reluctant to post the code but senior monks asked more than once to see it, and this humblest and most unworthy of Sextons was unwilling to refuse. Those who really feel they need to look at my first attempts, can look at earliest versions on backpan.

    jeffrey

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (5)
As of 2014-04-20 12:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (485 votes), past polls