[fpc-pascal]ReAlloc Question

Michael Van Canneyt michael.vancanneyt at wisa.be
Fri May 16 14:11:24 CEST 2003


On Fri, 16 May 2003, James Mills wrote:

> On Fri, May 16, 2003 at 01:41:35PM +0200, Michael Van Canneyt wrote:
> >
> >
> > 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.
>
> Yeah I found out there was TStringList after I wrote it. And I'll agree
> with you, it's not the best code. However other classes that dynamically
> allocate objects in memory use similar code to that of stringsclass.pas
> so I need to get this right :)
>
> >
> >
> > Also, Class destructors MUST be called 'Destroy', not 'done'.
> Okay.
>
> >
> > 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;
>
> Would this same technique work for a general dynamic array of objects ?

It should, unless you use memory pointers that point to specific memory
locations in the array.

Michael.





More information about the fpc-pascal mailing list