Re: Memory leak with Inline::C

From: Tassilo v. Parseval (tassilo.von.parseval_at_rwth-aachen.de)
Date: 07/11/04


Date: Sun, 11 Jul 2004 22:22:59 +0200

Also sprach Anno Siegel:

> I'm seeing a memory leak with the Inline code below. The sub by_inline()
> returns a varying number of scalars, which is probably the crux of the
> matter.

Actually, the problem is that your program may produce infinite loops.

> The code works as expected (returning the exponents of 2 used in the
> binary representation of the argument), but the loop leaks. It leaks
> just the same when by_inline() is called in void context.
>
> I don't see anything obviously wrong with the code, in particular the
> newly created SVs are carefully mortalized, but I'd like a second opinion.
>
> I'd also be grateful if one or the other could find the time to run
> the code and report back. For me, the process grows to 500+ MB before
> the loop runs out (I kill it when swapping becomes audible).
>
> Conditions here:
> Darwin 7.4.0, Perl 5.8.4, Inline 0.44, gcc 3.3
>
> Update:
> It also leaks on a Linux with Perl 5.8.1, same Inline, unknown gcc.
>
> Anno
>
> #!/usr/local/bin/perl
> use strict; use warnings; $| = 1;
> use Vi::QuickFix;
>
> my @res = by_inline( int rand 2 ** 32) for 1 .. 100_000;

A value in the above range might not fit into a signed 32bit integer. In
this case it gets wrapped around and 'x' in the below C function ends up
being negative. So 'x' will eventually become -1 and never turn zero.
And there you have your infinite while-loop.

> use Inline C => <<EOC;
> void by_inline( int x) {
> int i;
> Inline_Stack_Vars;
> Inline_Stack_Reset;
> i = 0;
> while ( x ) {
> if ( x & 1 ) {
> Inline_Stack_Push( sv_2mortal( newSViv( i)));
> }
> x >>= 1;
> i ++;
> }
> Inline_Stack_Done;
> }
> EOC
> __END__

One fix would be to increase the range of your numbers:

    void by_inline(double X) {
        long long x = X;
        ...
    }

Tassilo

-- 
$_=q#",}])!JAPH!qq(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
pam{rekcahbus})(rekcah{lrePbus})(lreP{rehtonabus})!JAPH!qq(rehtona{tsuJbus#;
$_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+sexisexiixesixeseg;y~\n~~dddd;eval


Relevant Pages

  • Re: socket connect call hangs
    ... Also I am not calling WSACleanup inside the loop. ... I even tried to trace for leaks using Process explorer, ... Volodymyr M. Shcherbyna, blog: http://www.shcherbyna.com/ ...
    (microsoft.public.win32.programmer.networks)
  • Re: Leak after using setlocale
    ... >escalate in the process tab of task manager (if you have the Memeory ... After increasing your loop limit from 100 to 1000000, ... and dumping leaks around it reveals no leaks at all; ... normal block at 0x00324500, 512 bytes long. ...
    (microsoft.public.vc.stl)
  • Re: Memory leak - What the ...?
    ... > mosteo@gmail.com writes: ... Gnatmem shows that it leaks heavily. ... package body Test_Aux is ... for I in Arr'Range loop ...
    (comp.lang.ada)
  • Re: automation error
    ... Although there are leaks when using any iterative loop with Outlook objects ... The problem is that Outlook creates internal variables for ... MAPITable and that's not only faster but has fewer memory leaks. ...
    (microsoft.public.office.developer.outlook.vba)