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

Graeme Geldenhuys graemeg.lists at gmail.com
Thu Oct 8 16:18:06 CEST 2009


2009/10/8 Jonas Maebe <jonas.maebe at elis.ugent.be>:
>>  SetLength(tocarray, _Header.ntoc);
>>  p := _Data + _Header.tocoffsetsstart;
>>  Move(p, tocarray, SizeOf(tocarray));
>
> This has to be
>
> move(p^, tocarray^, length(tocarray)*sizeof(tocarray[0]));
                                 ^
This gives a compiler error. Illegal qualifier and points to the
second ^ in that statement.


    move(p^, tocarray, _Header.ntoc*sizeof(tocarray[0]));
Causes a segmentation fault at runtime.


      Move(p, tocarray[0], SizeOf(tocarray));
This causes an Access Violation at runtime.



My problem is not reading in the data. It reads in perfectly, it's
just the cleanup of the local array that I am battling with. Heaptrc
keeps telling me there is a memory leak.

Initially I tried to "overlay" a record structure over each offset
into the file, to simply read the data via the record structure. No
need for the local array, but for the life of me, I couldn't get that
to work. I kept reading corrupt data.


> Otherwise you are just moving the contents of "p" into tocarray, and the
> pointer to the memory allocated for tocarray is gone forever. You're lucky
> (or unlucky) the program didn't crash when finalising tocarray.

When I included the finalize() call, it does cause a crash.  :-(

-- 
Regards,
  - Graeme -


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



More information about the fpc-pascal mailing list