Assigning to TFPGObjectList.Items[Index] never frees the previous (overwritten) item
Original Reporter info from Mantis: Michalis @michaliskambi
-
Reporter name: Michalis Kamburelis
Original Reporter info from Mantis: Michalis @michaliskambi
- Reporter name: Michalis Kamburelis
Description:
When you create a list that owns it's children (like TObjectList with FreeObjects = true), the convention is that doing
List[I] := new value;
frees the previous value. TFPGObjectList doesn't honour it, making memory leak if you don't copy and/or free previous value manually.
Looking at FGL source, the reason is that TFPSList.Put doesn't call Deref before assigning InternalItems[Index]. (Deref is overridden in TFPGObjectList to free child if owned.)
Testcase shows the leaks on TFPGObjectList, compile and run as
$ fpc -gl -gh free_on_put.pas
$ ./free_on_put
Heap dump by heaptrc unit
31 memory blocks allocated : 1372/1456
28 memory blocks freed : 1360/1432
3 unfreed memory blocks : 12
True heap size : 393216
True free heap : 392976
Should be : 393000
Call trace for block $B77C8240 size 4
$08048820 main, line 24 of free_on_put.pas
$080A8E73
Call trace for block $B77C81F0 size 4
$08048800 main, line 23 of free_on_put.pas
$080A8E73
Call trace for block $B77C81A0 size 4
$080487E5 main, line 22 of free_on_put.pas
$080A8E73
Mantis conversion info:
- Mantis ID: 19854
- OS: Debian GNU/Linux
- OS Build: (testing)
- Platform: i386
- Fixed in version: 2.6.0
- Fixed in revision: 18063 (#079831b5)