Hi,
I got a problem with TListView and Data. It seems that I got memory leaks, because Data pointers are not released (if I understand it well).
Let me show you what I do:
Here is a simple TListView (LV_Data). I add few items to the list (Report Style) into 3 Columns.
var
i : Integer;
ListItem : TListItem;
PObject : PChar;
for i := 0 to 3 do
begin
// Caption
ListItem := AList.Items.Add;
ListItem.Caption := 'My Caption' + '_' + Inttostr(i+1);
// Language Name
ListItem.SubItems.Add('Language' + '_' + Inttostr(i+1));
// User Name
ListItem.SubItems.Add('User Name' + '_' + Inttostr(i+1));
// User Data Path
PObject := StrAlloc(250);
StrPCopy(PObject, 'PATH_TO_USER_DIRECTORY' + '_' + Inttostr(i+1));
ListItem.Data := PObject;
end;
As you can see, I also add "hidden" data (in this example a path to directory, as string). I can use it in program, without displaying it to user in listview.
I can read it in simple way (for examle, in ListView OnClick event:)
var
sPath : string;
if (LV_Data.Items.Count > 0) AND (LV_Data.ItemIndex <> -1) then
begin
sPath := StrPas(PChar(LV_Data.Items[LV_Data.ItemIndex].Data));
end;
Everything works very nice... but, when I close program, I see there are some memory leaks.
---------------------------
Unexpected Memory Leak
---------------------------
An unexpected memory leak has occurred. The unexpected small block leaks are:
473 - 520 bytes: Unknown x 3
---------------------------
OK
---------------------------
As far as I know, I have to free those List View Data bymyself. How to do this?
procedure TMainFrm.LV_DataDeletion(Sender: TObject; Item: TListItem);
begin
if Item.Data <> nil then
begin
FreeMem(Item.Data);
Item.Data := nil;
end;
end;
Above code not working (why!?), got Invalid Pointer Operation.
Could you please tell me how to free this data to avoid memory leak?
Thank you in advance,
-Pawel