Delphi内存泄漏TJSONTextReader

时间:2018-07-13 14:03:53

标签: json delphi memory-leaks

我使用TJSONTextReader遇到内存泄漏。我或多或少地实现了embarcadero的示例。 http://docwiki.embarcadero.com/CodeExamples/Tokyo/en/RTL.JSONReader

这是一个错误还是我遗漏了任何东西?

memoryleak_example.txt的内容

{
    "products": [{
        "id": "14469654611354654",
        "name": "productname_xyz",
        "height": 111.550000,
    }],
    "products_feedback": null
}

这是我的示例代码:

var
  streamreader: TStreamReader;
  jsonreader: TJSONTextReader;
  arrstart, objstart: boolean;
  height: double;
  id, name: string;
begin
  // initialize or compiler warning
  arrstart := false;
  objstart := false;
  height := 0;
  streamreader := TStreamReader.Create('C:\temp\memoryleak_example.txt', TEncoding.UTF8);
  try
    jsonreader := TJSONTextReader.Create(streamreader);
    try
      while jsonreader.Read do begin
        case jsonreader.TokenType of
          // product object in products array
          TJsonToken.StartObject: if arrstart then objstart := true;
          // check for prodcuts array
          TJsonToken.StartArray: if (LowerCase(jsonreader.Path) = 'products') then arrstart := true;
          TJsonToken.Float:
            if objstart then
              if jsonreader.Path.EndsWith('height', true) then
                height := jsonreader.Value.AsExtended;
          TJsonToken.String:
            if objstart then begin
              if jsonreader.Path.EndsWith('id', true) then
                id := jsonreader.Value.AsString;
              if jsonreader.Path.EndsWith('name', true) then
                name := jsonreader.Value.AsString;
            end;
          // end product object
          TJsonToken.EndObject:
            if arrstart and objstart then begin
              objstart := false;
              if id <> '' then
                Memo1.Lines.Add(Format('id: %s - name: %s - height: %g', [id, name, height]));
              // reset values
              id := '';
              name := '';
              height := 0;
            end;
          // end of products array
          TJsonToken.EndArray: if (LowerCase(jsonreader.Path) = 'products') then arrstart := false;
        end;
      end;
    finally
      jsonreader.Free;
    end;
  finally
    streamreader.Free;
  end;
end;

这是关机时的内存泄漏报告:

---------------------------
Unexpected Memory Leak
---------------------------
An unexpected memory leak has occurred. The unexpected small block leaks are:
1 - 12 bytes: Unknown x 6
13 - 20 bytes: UnicodeString x 1
21 - 28 bytes: UnicodeString x 2, Unknown x 1
29 - 36 bytes: UnicodeString x 1
45 - 52 bytes: TList<System.JSON.Types.TJsonPosition> x 6
85 - 92 bytes: Unknown x 5
---------------------------
OK   
---------------------------

我用的是Delphi Seattle。 Embarcadero RAD Studio 10 Seattle版本23.0.21418.4207

我找到了这个讨论: https://forums.embarcadero.com/thread.jspa?threadID=118014 我不知道这是否相关。

--------------------------------2018/7/13 11:48:16--------------------------------
A memory block has been leaked. The size is: 12

This block was allocated by thread 0x18B0, and the stack trace (return addresses) at the time was:
41CA186 [System][System.@GetMem]
4241E94 [System.Generics.Defaults][System.Generics.Defaults.MakeInstance]
424256A [System.Generics.Defaults][System.Generics.Defaults.Comparer_Selector_Binary]
4242E84 [System.Generics.Defaults][System.Generics.Defaults._LookupVtableInfo]
EED8629 [System.JSON.Types][System.JSON][System.JSON.Types.{System.Generics.Defaults}TComparer<System.JSON.Types.TJsonPosition>.Default]
EEE1572 [System.JSON.Readers][System.JSON][System.JSON.Readers.{System.Generics.Collections}TList<System.JSON.Types.TJsonPosition>.Create]
EEDB91E [System.JSON.Readers][System.JSON][System.JSON.Readers.TJsonReader.GetPath]

The block is currently used for an object of class: Unknown

The allocation number is: 3192748

Current memory dump of 256 bytes starting at pointer address 79DC9440:
7C EF A0 04 01 00 00 00 10 00 00 00 FE C5 93 13 00 00 00 00 A0 93 DC 79 00 00 00 00 00 00 00 00
60 74 1E 04 00 00 00 00 B9 B7 30 00 86 A1 1C 04 94 1E 24 04 6A 25 24 04 84 2E 24 04 29 86 ED 0E
72 15 EE 0E 1E B9 ED 0E AD 16 EF 0E CC 0C EF 0E 73 54 EF 0E 04 81 20 04 B0 18 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 0C 00 00 00 00 00 00 00 DE 3A 6C EC 7C EF A0 04 01 00 00 00
10 00 00 00 21 C5 93 13 00 00 00 00 A0 93 DC 79 00 00 00 00 00 00 00 00 60 74 1E 04 00 00 00 00
C6 B7 30 00 86 A1 1C 04 94 1E 24 04 6A 25 24 04 84 2E 24 04 29 86 ED 0E 72 15 EE 0E 1E B9 ED 0E
E5 16 EF 0E CC 0C EF 0E 73 54 EF 0E 04 81 20 04 B0 18 00 00 00 00 00 00 00 00 00 00 00 00 00 00
|  ï     .  .  .  .  .  .  .  .  .  þ  Å  “  .  .  .  .  .     “  Ü  y  .  .  .  .  .  .  .  .
`  t  .  .  .  .  .  .  ¹  ·  0  .  †  ¡  .  .  ”  .  $  .  j  %  $  .  „  .  $  .  )  †  í  .
r  .  î  .  .  ¹  í  .  ­  .  ï  .  Ì  .  ï  .  s  T  ï  .  .       .  °  .  .  .  .  .  .  .
.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  Þ  :  l  ì  |  ï     .  .  .  .  .
.  .  .  .  !  Å  “  .  .  .  .  .     “  Ü  y  .  .  .  .  .  .  .  .  `  t  .  .  .  .  .  .
Æ  ·  0  .  †  ¡  .  .  ”  .  $  .  j  %  $  .  „  .  $  .  )  †  í  .  r  .  î  .  .  ¹  í  .
å  .  ï  .  Ì  .  ï  .  s  T  ï  .  .       .  °  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .

1 个答案:

答案 0 :(得分:0)

谢谢Dalija Prasnikar。

我带去了东京的System.JSON.Readers.pas源,并且内存泄漏消失了。

try
  Result := TJsonPosition.BuildPath(Positions);
finally
  if Positions <> FStack then
    Positions.Free;
end;

函数TJsonReader.GetPath:string; 方法中缺少此内容。