Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Objects Using Up the Stack

by graq (Curate)
on Mar 01, 2004 at 17:10 UTC ( [id://332977]=perlquestion: print w/replies, xml ) Need Help??

graq has asked for the wisdom of the Perl Monks concerning the following question:

Hi.

We have a group of scripts that use classes that use each other with no sensible structure or entry point. They appear to be eating up the CPU (building up recursively in the stack until Perl decides it's had enough).

I didn't write any of this and don't even have access to try and fix any (of the scripts or modules) yet, but have tried searching here and googling for some info on such a problem - but haven't managed to come across anything useful.

Does anyone have any good pointers where to start looking for solutions to this problem?

Many Thanks.

Replies are listed 'Best First'.
Re: Objects Using Up the Stack
by broquaint (Abbot) on Mar 01, 2004 at 17:20 UTC
    You could always do a trace through the perl debugger i.e
    shell> perl -de 'require "yourscript.pl"' Loading DB routines from perl5db.pl version 1.22 Editor support available. Enter h or `h h' for help, or `man perldebug' for more help. main::(-e:1): require "yourscript.pl" DB<1> t Trace = on DB<1> b Your::Class::new DB<2> c ...
    Or perhaps just use one of the hook modules around the constructor methods (and maybe the destructor methods too).
    HTH

    _________
    broquaint

Re: Objects Using Up the Stack
by flyingmoose (Priest) on Mar 01, 2004 at 17:24 UTC
    I've had similar problems with inherited C++ and Java code. It can be very hard to track down leaks and performance problems when they aren't really true leaks!

    You don't need to find the entry point. With bad OOP, that can be very tough. I'd recommend you make a utility class and in each constructor log the constructed object with the utility class, or (simpler) just print out to STDERR when you construct things so you can analyze the output later. Debugging by STDERR is gross sometimes, but it's incredibly versatile when trying to understand what your app does.

    I like to have a Util::debug function (or equivalent) that I can use to disable printouts at various levels during runtime or by changing a constant.

Re: Objects Using Up the Stack
by kvale (Monsignor) on Mar 01, 2004 at 17:23 UTC
    Without seeing code, it is hard to say much, but two possibilities for unlimited stack growth are

    (1) constructor cycles: object A creates an object B, and oject B creates an object A. The memory would blow up at the first creation of object A or B.

    (2) Some object or class method uses a recursive algorithm that isn't terminating.

    In any case, I think you need to look at the code and trace through a run using print statments or the debugger to narrow it down further.

    -Mark

Re: Objects Using Up the Stack
by exussum0 (Vicar) on Mar 01, 2004 at 17:21 UTC
    A method call has the same base effects of a regular function call. Values are pushed onto the stack, and new code is executed. With methods, the only difference is an extra call to get the object for which the method is being called.

    It sounds like you have a bad case of spaghetti code. A first step would be figuring out the "when" and "why" the functions are called. Understand the problem insie and out. Finally refactor. Identify the loops that the function call loops it goes through and find a way to break it.

    Wish I could help s'more.

      There was an interesting article on refactoring on perl.com a few months ago. The article wasn't especially about OO, but helpful to get started.
      best regards,
      neniro

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (5)
As of 2024-03-29 12:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found