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

Graeme Geldenhuys graemeg.lists at gmail.com
Thu Oct 8 15:51:34 CEST 2009


Hi,

I enabled heaptrc and it reported on block of memory not being freed.
I tracked it down to the following method in my code. I have tried
everything I can think of, but the memory leak still persists. Could
anybody help me please.

A quick description of what this code does. I'm reading content out of
a file. The content is an array of offsets into the file where data is
located.

* 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));
  for EntryIndex := 0 to _Header.ntoc-1 do
  begin
    pEntry := _Data + tocarray[EntryIndex];
    Topic := TTopic.Create(_Data,
                           _Header,
                           _Dictionary,
                           pEntry );
    Topic.HelpFile := Self;
    Topic.Index := EntryIndex;
    _Topics.Add(Topic);
  end;
//  Finalize(tocarray);                        <--- doesn't work
  Finalize(tocarray, _header.ntoc);       <--- doesn't work
  tocarray := nil;                                  <--- doesn't work
end;
--------------------------------------

-- 
Regards,
  - Graeme -


_______________________________________________
fpGUI - a cross-platform Free Pascal GUI toolkit
http://opensoft.homeip.net/fpgui/



More information about the fpc-pascal mailing list