簡體   English   中英

Delphi內存泄漏TJSONTextReader

[英]Delphi memory leak TJSONTextReader

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

謝謝Dalija Prasnikar。

我帶去了東京的System.JSON.Readers.pas源,並且內存泄漏消失了。

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

函數TJsonReader.GetPath:中缺少此內容 方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM