You are not logged in.
Pages: 1
type
TTestVO = class
private
fcode : Integer;
fname : String;
fanswers : String;
ftotal : Real;
published
property code : integer read fcode write fcode;
property name : string read fname write fname;
property answers : string read fanswers write fanswers;
property total : real read ftotal write ftotal;
end;
var
StringJson : RawUTF8;
ListObj : TList;
Itens : Integer;
TestVO : TTestVO;
begin
StringJson :='[{"code": 1,"name": "Margaret","answers": "yes","total": 10},{"code": 2,"name": "Mirian","answers": "no","total": 20}]';
ListObj := JSONToObjectList(TTestVO,StringJson);
try
for Itens := 0 to ListObj.Count -1 do
TestVO := ListObj[Itens];
finally
for Itens := 0 to ListObj.Count - 1 do
TTestVO(ListObj.Items[Itens]).Free;
FreeAndNil(ListObj);
end;
I am trying to remove this object from memory and I have the following error:
"Invalid pointer operation."
Offline
By default TObjectList.OwnsObjects is true, so FreeAndNil(ListObj) will destroy all your objects. But you destroy it before, so pointers to your object is incorrect. Just remove this loop
for Itens := 0 to ListObj.Count - 1 do
TTestVO(ListObj.Items[Itens]).Free;
And casting to TTestVO is overhead also.
To insert a code block place it inside the [ code ] .. [ /code ] (w/o spaces) - see BBCode help
Last edited by mpv (2017-11-16 21:40:41)
Offline
Perfect, thank you...
Offline
Pages: 1