Board index » delphi » readln( f, s) causes memory leak in D2?!?

readln( f, s) causes memory leak in D2?!?

following the example in the help file I wrote :

var
        s : string;
        f : text;
        buf : pchar;
begin
        assignfile(f,filename);
        getmem(buf,4000);
        system.settextbuf(f,buf^,4000);
        reset(f);

        while not eof(f) do begin
                readln(f,s);
etc...

the readln line causes a memory leak. changing 's' to s : string[255] stops the leak but
causes other problems.

This is causing me a terrible headache.

Any one know what I can do? I have tried setlength(s,500) to initialise, and also in the
loop, without success.

Yours

        Greg

--

Greg Lorriman co. inc. & sons Ltd

 

Re:readln( f, s) causes memory leak in D2?!?


Quote
>    getmem(buf,4000);

Greg,

When you allocate memory using GetMem, you are also responsible of freeing
this memory after you're done with it by calling FreeMem(buf) !

HTH,

        Niels.

--------------------------------------------------------------------------
Niels Vanspauwen
Student @ KULeuven
Faculty of Civil Engineering
Department of Computer Science

E-Mail: Niels.Vanspauwen!NSPM!Student.Kuleuven.Ac.Be
(Replace !NSPM! with @ to reply...)
--------------------------------------------------------------------------

Re:readln( f, s) causes memory leak in D2?!?


In article: <01bd6989$50c3fee0$217a3a86@Niels>  "Niels Vanspauwen"

Quote
<niels.vanspauwen!NS...@student.kuleuven.ac.be> writes:

> >       getmem(buf,4000);

> Greg,

> When you allocate memory using GetMem, you are also responsible of freeing
> this memory after you're done with it by calling FreeMem(buf) !

Sorry, Neils, I should have put in all the source. I was freeing the buffer correctly.

 I discovered that my problem was due to using a string in the readln function. I don't
know why (I'm now using an fixed lenght arrays), but I believe it has something to do with
unix files not using normal (dos?) EOL char sequences. Readln seems unable to handle
unix files properly and I get all sorts of mess happening. As it happens it doesn't really
matter for my app, and I fixed the leak anyway. Tstringlist.savetofile is similarly affected
and I had to manually write each line out instead.

Thanks though,

        Greg

Other Threads