[fpc-pascal] More memory leaks and other problems: request for review of probably stupid mistake

José Mejuto joshyfun at gmail.com
Wed Mar 31 20:41:03 CEST 2010


Hello FPC-Pascal,

Wednesday, March 31, 2010, 7:52:43 PM, you wrote:

J> I'm trying to learn FreePascal by writing a file indexer a la locate and
J> I'm hitting more walls after receiving quick and clear help from Michael
J> Van Canneyt  regarding memory leaks.
[...]
J> Call trace for block $00000000035CAEF0 size 35
J>   $0000000100030659
J>   $00000001000324B1
J>   $0000000100032616
J>   $0000000100038CCF  TFILESEARCH__SEARCHPERFILTER,  line 638 of search.pp
J>   $0000000100038CCF  TFILESEARCH__SEARCHPERFILTER,  line 638 of search.pp
J>   $0000000100038CCF  TFILESEARCH__SEARCHPERFILTER,  line 638 of search.pp
J>   $0000000100038CCF  TFILESEARCH__SEARCHPERFILTER,  line 638 of search.pp
J>   $0000000100038CCF  TFILESEARCH__SEARCHPERFILTER,  line 638 of search.pp

Your code looks too tricky, specially the AddFileToResult. You are
using records to store the item, in theAddFileToResult you assign an
ansistring to one or more fields, in this process a reference to the
ansistring is generated, and this references are quite sure your
memory leaks in some way as the allocation point by heaptrc is in the
RTL (no traceback).

Instead using records, use a class and instead TList use TObjectList,
this way when you invoke Res.Free all objects inside will be freed
also, or when you delete an object it is automagically freed.

Working with records allocated with New and adding them to a list is a
complex task to keep the track of each one, specially if you have to
mix different record types.

If you still want to use records, before the Dispose(APResultRecord)
set the ansistring fields to '', this will garantee that the reference
is freed.

-- 
Best regards,
 José




More information about the fpc-pascal mailing list