复杂json对象中的TJSONObject内存泄漏

时间:2014-12-09 14:40:47

标签: json delphi delphi-xe5

我尝试使用带有Delphi Xe5的idHTTPserver在小型企业环境中本地提供REST API服务。我在将文件发送到客户端之前预处理该文件,此时出现了问题。完成该过程后,内存不会被释放 创建的JSON对象毕竟是正确发送到客户端(AngularJS App)

我做错了什么?

当我收到HTTP客户端请求时,我会这样做..

Procedure TMain.IdHTTPServer1CommandGet(AContext: TIdContext;
ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
Var
  // Here Var Types

  Json,LFilename:String;
  ROOTCOMANDAS,TORDEN,DATA,MSG : TJSONObject;
  Dlnew,d : TJSONArray
  files:tfilestream;

Begin

LFilename := ARequestInfo.Document;

if AnsiSameText(LFilename, '/resto/orders/jsondata') then
begin
  files := TFileStream.Create('htd' + LFilename, fmOpenRead + fmShareDenyWrite);
  Json := ReadStringFromStream(files);
  files.Free;
  ROOTCOMANDAS := TJSONOBJECT.ParseJSONValue(TEncoding.ASCII.GetBytes(Json), 0) as TJSONOBJECT;
  try
    Data := ROOTCOMANDAS.Get('data').JSONValue as TJSONOBJECT;
    d := Data.Get('d').JSONValue as TJSONArray;
    dlnew := TJSONArray.Create;

    for LValue in d do
      if (LValue as TJSONOBJECT).GetValue('ss').Value = '0' then
        dlnew.AddElement(LValue);

    TORDEN := TJSONOBJECT.Create;

    Msg := TJSONOBJECT.Create;
    Msg.AddPair(TJSONPair.Create('t', TJSONString.Create('m5000_325')));
    Msg.AddPair(TJSONPair.Create('tipo', TJSONNumber.Create(5)));

    TORDEN.AddPair(TJSONPair.Create('msg', Msg));

    Msg := TJSONOBJECT.Create;

    Msg.AddPair(TJSONPair.Create('et', TJSONString.Create(ETAGL)));
    Msg.AddPair(TJSONPair.Create('d', dlnew));

    TORDEN.AddPair(TJSONPair.Create('data', Msg));
    TORDEN.AddPair(TJSONPair.Create('ok', TJSONTrue.Create));
    TORDEN.AddPair(TJSONPair.Create('md', TJSONNumber.Create(iFD)));
    TORDEN.AddPair(TJSONPair.Create('time', TJSONString.Create(UTC)));

    Json := TORDEN.ToString;

    AResponseInfo.CacheControl := 'no-cache';
    AResponseInfo.CustomHeaders.Values['Access-Control-Allow-Headers'] := 'Content-Type';
    AResponseInfo.CustomHeaders.Values['Access-Control-Allow-methods'] := 'GET,POST,OPTIONS';
    AResponseInfo.CustomHeaders.Values['Access-Control-Allow-origin'] := '*';
    AResponseInfo.CharSet := 'utf-8';
    AResponseInfo.Pragma := 'Public';
    AResponseInfo.Server := 'Drone';
    AResponseInfo.ContentText := Json;
  finally
    ROOTCOMANDAS.Free;
  end;

  exit;
end;

1 个答案:

答案 0 :(得分:0)

您永远不会发布TORDEN变量,因此内存泄漏,以及尝试释放它时出现的错误是由以下行引起的:

for LValue in d do
  if (LValue as TJSONOBJECT).GetValue('ss').Value = '0' then
    dlnew.AddElement(LValue);

LValued所有,将在d发布后发布,并且您要将LValue添加到dlnew,并且还要发布它。这里有所有权问题,因为两个对象想要拥有并释放相同的包含对象。

尝试以下更改来解决问题:

for LValue in d do
  if (LValue as TJSONOBJECT).GetValue('ss').Value = '0' then
    begin
      dlnew.AddElement(LValue);
      // here you are saying that you don't want to release object when ROOTCOMANDAS is released
      LValue.Owned := false;
    end;
// release ROOTCOMANDAS and d along with it
ROOTCOMANDAS.Free;
// Set back owned property to true so you don't leak objects
for LVaue in dlnew do
  LValue.Owned := true;
...
    Json := TORDEN.ToString;
    TORDEN.Free;

... remove superfluous ROOTCOMANDAS.Free; in finally part