Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re^2: Who's referencing my variables?

by Anonymous Monk
on Apr 01, 2014 at 12:40 UTC ( #1080547=note: print w/ replies, xml ) Need Help??


in reply to Re: Who's referencing my variables?
in thread Who's referencing my variables?

I'm afraid my original post wasn't entirely clear. Consider the following code:

package Foo; my @objects; sub new { my $self = bless {}, shift; push @objects, $self; return $self; } sub DESTROY { print "bye bye" } package main; { my $foo = Foo->new; } ETERNITY: while(1) { # DESTROY is NOT called yet. }
When you run this, "bye bye" is not printed while we're waiting inside ETERNITY because @Foo::objects still holds a reference to $foo, even though its instantiation happened inside a scope we've since left.

What I want to be able to do is to discover whether and where similar @Foo::objects-like data structures exist in a large code base. So I need to walk the symbol tables and probe what's there. How do I find such symbols? Can I?


Comment on Re^2: Who's referencing my variables?
Download Code
Re^3: Who's referencing my variables?
by Anonymous Monk on Apr 01, 2014 at 12:51 UTC
    Here is how you should have written that
    our @objecgts; ## @Foo::objects ... push @objects, weaken $object;

    If you're going to do things the fudgy way, do it the easy fudgy way

      I guess your implied advice is: go through all the modules (a few dozen) and make all variables 'our', then probe the symbol table. I'm not yet convinced that this is the best approach.

        I guess your implied advice is: go through all the modules (a few dozen) and make all variables 'our', then probe the symbol table. I'm not yet convinced that this is the best approach.

        Its the easiest and most straight forward approach

        Think of it this way -- you wrote leaky code

        The best approach is a complete code review (effort)

        The next best approach is papering over the leaks (minimum effort possible)

Re^3: Who's referencing my variables?
by AnomalousMonk (Abbot) on Apr 01, 2014 at 16:24 UTC

    Hi RutgerVos: I assume you posted the code shown above.

    When you write and show us code that demonstrates some painful consequence and then ask "How can I avoid this pain?", the obvious, simple and complete answer is "Don't do that!"

    I suppose what you really mean is that other people, users of your code, are indulging nasty, unclean practices that are the source of the pain, such as exemplified by tobyink here. In this case, the only true solution is code review: go around and whack everyone's hand with a ruler until they stop doing the nasty. If a ruler doesn't work, use a hatchet. Others have pointed out tools to aid the review process, but you must ultimately persuade others not to do Bad Things with your code. Other than Good Practice, I'm not aware of any procedure in Perl that will 'armor' a reference, object or otherwise, against being circularized or otherwise given an infinite lifetime.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (5)
As of 2014-12-21 00:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (100 votes), past polls