Delphi内存泄漏问题:TJSONTextReader

3
我使用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  ï  .  .       .  °  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .

这是一个非常有趣的问题。 - Kevin Böhmer
我在 TJSONxxx 中发现了许多错误,所以我并不感到惊讶,这就是为什么我不再使用它,而是只使用 TalJsonDoc(来自 https://github.com/Zeus64/alcinoe)。 - zeus
2
它在东京没有泄漏。显然是在此期间修复的某个错误。 - Dalija Prasnikar
1个回答

1

谢谢Dalija Prasnikar。

我使用了Tokyo的System.JSON.Readers.pas源代码,内存泄漏问题已经解决。

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

这个方法 function TJsonReader.GetPath: string; 中缺少了这个内容。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接