in reply to DBD::CSV Memory Leak

Turn on "use strict;" and "use warnings;" and solve all the issues it raises. Declare your variables als 'late' as possible. at the lowest possible level. If all that failes, isolate portions of the code to see if the mem leak happens there.

Replies are listed 'Best First'.
Re: Re: DBD::CSV Memory Leak
by AcidHawk (Vicar) on Aug 05, 2002 at 13:41 UTC
    I am using strict and warnings, and I get no errors or warnings in the complete code. In terms of isolating the code to find the memory leak.. I have only added the code to the origional question that is causing the mem leak. I need to verify if there is anything funny in that piece of code.

    -----
    Of all the things I've lost in my life, its my mind I miss the most.

Re: Re: DBD::CSV Memory Leak
by AcidHawk (Vicar) on Aug 05, 2002 at 13:52 UTC
    For what it is worth, The full code is on my scratchpad.

    -----
    Of all the things I've lost in my life, its my mind I miss the most.

      In taking a look at your scratchpad, I don't immediately see the cause of your memory leak, but I see something that should help.

      At the top of your code, you've predeclared all of your variables. This means that, regardless of where they are used, they exist. For example, you only use $hd_key in your &Setup_Config subroutine. This means that you should only declare it there, and preferably (though some argue this point) you should only declare it right before you use it. This means that all variables should have the smallest possible scope. You have, in effect, used global variables and eliminated part of the benefit of using strict. Consider the following example:

      my $foo; sub bar { # lots o' code $foo++; # more code } sub baz { # do a few laps around the pool ($foo) = (split)[2]; # shower and go home }

      In the above example, we've reused the variable $foo. For what good? Is it really necessary? This will confuse someone coming to maintain it later. Further, when the subs exit, $foo hangs around when it may not need to. Here's an example of a nice variable declaration:

      sub versive { # some pushups while ( my ($key,$value)= each %data ) { # process things } # }

      In that example, $key and $value exist only in the while loop. This means that the memory they use is available for reuse immediately after the scope of the loop ends (IIRC, the memory is not released back to the OS) and there is no worry about accidentally reusing one of those variables later and possibly having an incorrect value. If you get in the habit of coding like that, your code will be much easier to work with in the long run.

      Cheers,
      Ovid

      Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.