我在计时器循环中刷新了DBXJSON TJSONArray,但是我发现我的算法泄漏内存时我不会以正确的方式使用库。这是错的吗?
我需要重新加载数组COMANDASTOT,这个数组由其他线程使用,用于替换数据和获取数据。
Idea是更新存储在TJSONArray中的信息,但是当我更新数组时,这开始泄漏
谢谢!
uses ....
var
COMANDASTOT:TJSONARRAY;
ROOTCOMANDAS:TJSONOBJECT;
procedure tmain.FormCreate(Sender: TObject);
RespContent: TMemoryStream;
s, id, strd, et: String;
LValues, pedidos: TJSONArray;
LJson, Comand: TJSONOBJECT;
LValue, pedido: TJsonValue;
F: TextFile;
RC: TRESTClient;
RR: TRESTResponse;
RQ: TRESTRequest;
begin
try
// USING TRESTCLIENT VCL TO GET JSON OBJECT FROM CLOUDSERVER
RC := TRESTClient.Create('https://cloudorders.example.com/getorders?id=3456);
RQ := TRESTRequest.Create(RC);
RQ.Execute;
RQ.Response.RootElement:='';
ROOTCOMANDAS := RQ.Response.JSONValue as TJSONOBJECT;
if not DirectoryExists('htd\resto\orders') then
CreateDir('htd\resto\orders');
if DirectoryExists('htd\resto\orders') then
begin
// SAVING JSON DATA TO DISK
assignfile(f,'htd\resto\orders\process');
rewrite(f);
writeln(f,RQ.Response.content);
closefile(f); }
end;
Comand := ROOTCOMANDAS.Get('data').JSONValue as TJSONOBJECT;
ETAG := Comand.GetValue('et').Value;
COMANDASTOT := Comand.Get('d').JSONValue as TJSONArray; // <<- ARRAY LOADED FIRST TIME
COMANDASTOT.Owned:=false;
RQ.Free;
RC.Free;
except
on E: Exception do
begin
Memo1.Lines.Add(ahora + 'Error Loading JsonArray: ' + E.Message);
end;
end;
end;
Procedure TMain.Timer1Timer(Sender: TObject);
begin
// Called each 30 sec. Other process change the file
If DataChange then
ReadFromDiskJSON;
end;
procedure tmain.ReadFromDiskJSON;
Var
fs:tfilestream;
orders,MD5s:string;
COM,ljson:tjsonobject;
begin
try
while NOT FileCanBeOpened('htd\resto\orders\process') do
begin
sleep(100);
end;
fs := TFileStream.Create('htd\resto\orders\process',fmOpenRead + fmShareDenyWrite);
orders := ReadStringFromStream(fs);
COM := TJSONOBJECT.ParseJSONValue(orders) as TJSONOBJECT;
LJson := COM.Get('data').JSONValue as TJSONOBJECT;
/// THIS SENTENCE LEAK MEMORY
/// Why??????
COMANDASTOT:= LJSON.Get('d').JSONValue as TJSONArray ; // <<<-THIS LEAK MEMORY!!
COMANDASTOT.Owned:=false;
com.Free;
fs.Free;
except on e: Exception do
begin
memo1.Lines.Add(SNow+' Error ReadFromDiskJSON'+e.Message);
fs.Free;
end;
end;
end;