in reply to Memory leak!

I didn't look to closely. The indenting in your code looks a bit random (hard to follow the flow). Try perltidy to fix that. But there are a few things that you should take a look at:

First, you call a function like this:

for (;;) { &Prog; }
I think you really meant
while(1) { Prog(); }

The function cleanUp calls opendir but not closedir. Looks like you do the same in Prog. One of the golden rules is: Always close handles as soon as you don't need them anymore.

Don't use '#ff0000':
use Acme::AutoColor; my $redcolor = RED();
All colors subject to change without notice.

Replies are listed 'Best First'.
Re^2: Memory leak!
by BrowserUk (Patriarch) on Dec 12, 2011 at 19:53 UTC

    If you deparse for(;;){ you get:

    perl -MO=Deparse -e"for(;;){ print 'hello' }" while (1) { print 'hello'; }

    Whilst I agree while(1) { is clearer, for(;;){ isn't actually an error.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

    The start of some sanity?

      Ah, thanks. Didn't realize Perl already does that. Good catch!

      BREW /very/strong/coffee HTTP/1.1
      Host: goodmorning.example.com
      
      418 I'm a teapot

      for(;;){ isn't actually an error.

      This is intentional. In both C and Perl, any combination of the three expressions can be omitted.

      • If the initialisation expression is missing, no initialisation is performed before entering the loop.
      • If the exit condition is missing, the loop doesn't exit.
      • If the counting expression is missing, no code is executed at the end of every pass.
        This is intentional. In both C and Perl, any combination of the three expressions can be omitted.

        I know.

        It means that instead of this:

        C:\test>perl -E"while(<>){ print }" hello hello goodbye goodbye ^Z

        We could write:

        C:\test>perl -E"for(;<>;){ print }" fred fred bill bill john john ^Z

        But few, if any, would.


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.

        The start of some sanity?

Re^2: Memory leak!
by joeymac (Acolyte) on Dec 12, 2011 at 19:44 UTC

    Thanks for the advice on the closedir. I will add these in and see if it has an effect on the problem. Just for reference, is there much difference between infinitely looping using a

    while(1) {}

    loop rather than a

    for (;;){}

    loop?

      The first one makes it a bit clearer that you are intentionally looping an infinite number.

      I'm not sure about this, but the while loop may also be a tiny bit more efficient in this case.

      BREW /very/strong/coffee HTTP/1.1
      Host: goodmorning.example.com
      
      418 I'm a teapot

        cavac:

        You shouldn't speculate--just measure it with Devel::Benchmark!

        Roboticus ducks and runs while wondering if his machine can do more infinite loops per second than yours... ;^D

        ...roboticus

        When your only tool is a hammer, all problems look like your thumb.

        How could doing more work (evaluating a constant) be more efficient?

        It's not less efficient either, though. while (CONSTANT) { ... } gets compiled as for (;;) { ... } or (); depending on whether the constant is true or false.

      No, while (1) {} gets optimised to for (;;) {} (which I use and pronounced "for ever").
        No, while (1) {} gets optimised to for (;;) {} (which I use and pronounced "for ever").

        Er, no. for (;;) {} gets deobfuscated to:

        perl -MO=Deparse -e"for(;;){}" while (1) { (); } -e syntax OK

        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.

        The start of some sanity?