[fpc-pascal] How to free this memory and avoid memory leak

Mattias Gärtner nc-gaertnma at netcologne.de
Thu Oct 8 16:28:56 CEST 2009


Zitat von Graeme Geldenhuys <graemeg.lists at gmail.com>:

> [...]
> * ntoc  = number of entries in the array. Each entry is a LongWord (or
> Int32 in the code below)
> * tocarray is my local array that gets populated with information from
> the file, using the Move() procedure.
> * tocoffsetsstart is the starting offset of the TOC array in the file.
> * I then simply look through the number of entries in the array
>   - position p to file offset as specified by each entry of the array
>   - extract my data, and continue with the next item in the array
>
> At the end I try to free / dispose of the memory used by my local
> array: tocarray
> ...but whatever I try, heaptrc keeps telling me I have a memory leak
> in this code.
>
> --------------------------------------
> procedure THelpFile.ReadContents;
> var
>   Topic: TTopic;
>   EntryIndex: longint;
>   pEntry: pTTOCEntryStart;
>   tocarray: array of Int32;
>   p: PByte;
> begin
>   _Topics.Capacity := _Header.ntoc;
>
>   SetLength(tocarray, _Header.ntoc);
>   p := _Data + _Header.tocoffsetsstart;
>   Move(p, tocarray, SizeOf(tocarray));

if _Header.ntoc>0 then
   Move(p^,tocarray[0],SizeOf(Int32)*_Header.ntoc);

>   for EntryIndex := 0 to _Header.ntoc-1 do
>   begin
>     pEntry := _Data + tocarray[EntryIndex];

Why not simply:
   pEntry := _Data + PInt32(p)[EntryIndex];

>     Topic := TTopic.Create(_Data,
>                            _Header,
>                            _Dictionary,
>                            pEntry );
>     Topic.HelpFile := Self;
>     Topic.Index := EntryIndex;
>     _Topics.Add(Topic);
>   end;
> //  Finalize(tocarray);                        <--- doesn't work

not needed:

>   Finalize(tocarray, _header.ntoc);       <--- doesn't work
>   tocarray := nil;                                  <--- doesn't work
> end;


Mattias





More information about the fpc-pascal mailing list