Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 1999-08

Pattern found!

5 messages · 3 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Unknown author
to Markku Niskanen: I have done a lot of testing with the heap functions and now I suspect I have found a…
Markku Niskanen wrote 12 lines
I have done a lot of testing with the heap functions and now I suspect I have found a pattern in the getmem/StrAlloc and Freemem/StrDispose problems. Just run the following code and reserve heap memory starting from 100 bytes and up. You can comment/uncomment the Getmem/StrAlloc and Freemem/StrDispose lines to check both pairs of memory functions. I get four values with a memory leak of 4 bytes and four values with zero memory leak. Has this something to do with the 64-byte allocation alignment or what?
1) There recently was a bug in the delphi-like memory allocator;
   this was solved.

2) the output of your program is the following:

home: >testm
Bytes to reserve: 1000
Mem : 262112
After StrAlloc : 261104(-1008)
After strPcopy : 261104(-1008)
After strDispose: 262112(-0)
Bytes to reserve: 100
Mem : 262112
After StrAlloc : 262000(-112)
After strPcopy : 262000(-112)
After strDispose: 262112(-0)
Bytes to reserve: 0

Is this now correct or wrong ? Seems OK to me ?

The fact that it reserves 8 bytes more is a consequence of the old-style calls in sysutils, combined with the new delphi-style memory manager automatically used if you use delphi or objfpc mode. This will be again reduced to 4 ASAP.

Michael.

Reply by email

Markku Niskanen
to Unknown author
you wrote
Is this now correct or wrong ? Seems OK to me ?

Okay, I must get the latest compiler then.
Funny, I have compiled my version only a
few days ago, must have messed up with
the CVS somehow.

Thanks, I'll do my best to continue with
my excavations :-)

Markku

Reply by email

Markku Niskanen

I have done a lot of testing with the heap functions and now I suspect I have found a pattern in the getmem/StrAlloc and Freemem/StrDispose problems.

Just run the following code and reserve heap memory starting from 100 bytes and up. You can comment/uncomment the Getmem/StrAlloc and Freemem/StrDispose lines to check both pairs of memory functions.

I get four values with a memory leak of 4 bytes and four values with zero memory leak. Has this something to do with the 64-byte allocation alignment or what?

As for StrPcopy() there is an additional memory loss of 8 bytes each the function is called and it follows the same pattern as the rest of the behaviour.

The GetMem/Freemem works in Turbo Pascal, at least.

I have no idea how to correct the problem, maybe it is in the ASM part of FPC.

-------------- clip ---------------------
program memtest;
uses sysutils;
var pStr: Pchar;
 st  : String;
 Bytes: Integer;
 BytesLost: LongInt;
 Free0,Free1,Free2,Free3: LongInt;
begin
    repeat
      Write( 'Bytes to reserve: ');
      Readln(Bytes);
      If Bytes = 0 then exit;
      St := '12345678901234567890';
      Free0 := Memavail();
      Writeln('Mem             : ',memavail());
      pStr := StrAlloc(bytes);
      //GetMem(pStr,Bytes);
      Free1 := Memavail();
      Writeln('After StrAlloc  : ',memavail(),'(-',Free0-Free1,')');
      // strPcopy(pStr,st); { this adds more problems if uncommented}
      Free2 := MemAvail();
      Writeln('After strPcopy  : ',memavail(),'(-',Free0-Free2,')');
      StrDispose(pStr);
      //FreeMem(pStr,Bytes);
      Free3 := MemAvail();
      Writeln('After strDispose: ',memavail(),'(-',Free0-Free3,')')
    until false;
end.

Reply by email