[fpc-pascal]ReAlloc Question

Michael Van Canneyt michael.vancanneyt at wisa.be
Fri May 16 13:41:35 CEST 2003


On Fri, 16 May 2003, James Mills wrote:

> Hi,
>
> Can any problems with stringsclass.pas ?
> Reason I ask is that, yes my fairly large program works that uses
> dynamic memory allocation, however it has memory leaks galore! :(
> stringsclass.pas is a dynamic string class I created which I take pride
> in, but I'm not convinced I wrote the: procedure del(index: Integer);
> correctly...
> Could someone have a look and comment on that please ? It will aid
> greatly in my efforts to debug my memory leaks...

With all due respect, but this is not exactly the most efficient code ever.
Did you have a look at the standard TStringList class ? It works very well,
and does all you do.


Also, Class destructors MUST be called 'Destroy', not 'done'.

Your method of calling init/done is also not quite correct, you're not
supposed to call the destructor unless you actually want to free your
class.

If you want to stick to your class, try replacing your code with this=

procedure TStrings.del(index: Integer);
var
 I : Integer;

begin
 For I:=index to nstrings-1 do
   Strings[i]:=Strings[i+1];
 Dec(nstrings);
 ReallocMem(strings,sizeof(Shortstring)*nstrings);
end;

Using this I get no memory leaks.

Michael.





More information about the fpc-pascal mailing list