Recent

Author Topic: SOLVED! - There may be a problem in the library fpjson  (Read 3962 times)

esvignolo

  • Full Member
  • ***
  • Posts: 159
  • Using FPC in Windows, Linux, Macos
SOLVED! - There may be a problem in the library fpjson
« on: February 07, 2017, 03:00:51 pm »
Hi!! i have a problema, i can't free some objects with this code:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.    jData : TJSONData;
  4.    jObject : TJSONObject;
  5.    jArray : TJSONArray;
  6.    sCampo:String;
  7.    i:integer;
  8.    rValor: Real;
  9.    sJson:string;
  10. begin
  11.      sJson:='[{"id":"076b1e63-6a57-4bcd-83db-c5c472a26003","sdescripcion":"0,10","rvalor":0.1,"insertedon":"2016-02-11T15:46:02.600Z","updatedon":null,'+
  12.                '"deletedon":null},{"id":"087369d0-4978-45df-a28d-a6541cc0dcc0","sdescripcion":"500,00","rvalor":500,"insertedon":"2016-02-11T15:46:06.568Z",'+
  13.                '"updatedon":null,"deletedon":null},{"id":"0fa2f456-abde-4c8f-a155-1a55e6d97c3f","sdescripcion":"100,00","rvalor":100,'+
  14.                '"insertedon":"2016-02-11T15:46:05.849Z","updatedon":null,"deletedon":null}]';
  15.      jData:=GetJSON(sJson);
  16.      jArray:=TJSONArray.Create();
  17.      jArray:=TJSONArray(jData);
  18.      try
  19.        if jArray.Count>0 then
  20.        begin
  21.               for i:=0 to jArray.Count-1 do
  22.               begin
  23.                  jObject:= TJSONObject(jArray[i]);
  24.                  try
  25.                    sCampo:=jObject.Find(lowercase('sdescripcion')).Value;
  26.                    rValor:=jObject.Find(lowercase('rvalor')).AsFloat;
  27.                    ListBox1.AddItem(sCampo+': '+FormatFloat('0.00',rValor), nil);
  28.                  finally
  29.                     FreeAndNil(jObject);
  30.                  end;
  31.               end;
  32.        end;
  33.      finally
  34.            FreeAndNil(jData);  //here come a exception
  35.            FreeAndNil(jArray); //here come a exception
  36.      end;
  37.  
  38. end;
  39.  

If i dont free this objects, i have memory leaks.

I attach a example!
« Last Edit: February 07, 2017, 04:23:18 pm by esvignolo »

lainz

  • Hero Member
  • *****
  • Posts: 4460
    • https://lainz.github.io/
Re: There may be a problem in the library fpjson
« Reply #1 on: February 07, 2017, 03:27:15 pm »
Here is the fixed code, no leaks:

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
  9.   fpjson;
  10.  
  11. type
  12.  
  13.   { TForm1 }
  14.  
  15.   TForm1 = class(TForm)
  16.     Button1: TButton;
  17.     ListBox1: TListBox;
  18.     procedure Button1Click(Sender: TObject);
  19.   private
  20.  
  21.   public
  22.  
  23.   end;
  24.  
  25. var
  26.   Form1: TForm1;
  27.  
  28. implementation
  29.  
  30. {$R *.lfm}
  31.  
  32. { TForm1 }
  33.  
  34. procedure TForm1.Button1Click(Sender: TObject);
  35. var
  36.    jData : TJSONData;
  37.    jObject : TJSONObject;
  38.    jArray : TJSONArray;
  39.    sCampo:String;
  40.    i:integer;
  41.    rValor: Real;
  42.    sJson:string;
  43. begin
  44.      sJson:='[{"id":"076b1e63-6a57-4bcd-83db-c5c472a26003","sdescripcion":"0,10","rvalor":0.1,"insertedon":"2016-02-11T15:46:02.600Z","updatedon":null,'+
  45.                '"deletedon":null},{"id":"087369d0-4978-45df-a28d-a6541cc0dcc0","sdescripcion":"500,00","rvalor":500,"insertedon":"2016-02-11T15:46:06.568Z",'+
  46.                '"updatedon":null,"deletedon":null},{"id":"0fa2f456-abde-4c8f-a155-1a55e6d97c3f","sdescripcion":"100,00","rvalor":100,'+
  47.                '"insertedon":"2016-02-11T15:46:05.849Z","updatedon":null,"deletedon":null}]';
  48.      jData:=GetJSON(sJson);
  49.      //jArray:=TJSONArray.Create();
  50.      jArray:=TJSONArray(jData);
  51.      try
  52.        if jArray.Count>0 then
  53.        begin
  54.               for i:=0 to jArray.Count-1 do
  55.               begin
  56.                  jObject:= TJSONObject(jArray[i]);
  57.                  try
  58.                    sCampo:=jObject.Find(lowercase('sdescripcion')).Value;
  59.                    rValor:=jObject.Find(lowercase('rvalor')).AsFloat;
  60.                    ListBox1.AddItem(sCampo+': '+FormatFloat('0.00',rValor), nil);
  61.                  finally
  62.                     //FreeAndNil(jObject);
  63.                  end;
  64.               end;
  65.        end;
  66.      finally
  67.            //FreeAndNil(jData);  //here como exception
  68.            jArray.Free;
  69.      end;
  70.  
  71. end;
  72.  
  73. end.
  74.  

esvignolo

  • Full Member
  • ***
  • Posts: 159
  • Using FPC in Windows, Linux, Macos
Re: There may be a problem in the library fpjson
« Reply #2 on: February 07, 2017, 03:46:37 pm »
Thanks Lainz, works!
But I can not understand the problem with the original code and because it generates an exception.

 :o

lainz

  • Hero Member
  • *****
  • Posts: 4460
    • https://lainz.github.io/
Re: There may be a problem in the library fpjson
« Reply #3 on: February 07, 2017, 03:59:49 pm »
Thanks Lainz, works!
But I can not understand the problem with the original code and because it generates an exception.

 :o

Code: Pascal  [Select][+][-]
  1. jArray:=TJSONArray.Create();
  2. jArray:=TJSONArray(jData);

this lines, in the first you create an object, in the second you change the pointer of the object, so you get a memory leak of the first object created.

Code: Pascal  [Select][+][-]
  1. jObject:= TJSONObject(jArray[i]);
  2.                  try
  3.                    sCampo:=jObject.Find(lowercase('sdescripcion')).Value;
  4.                    rValor:=jObject.Find(lowercase('rvalor')).AsFloat;
  5.                    ListBox1.AddItem(sCampo+': '+FormatFloat('0.00',rValor), nil);
  6.                  finally
  7.                     //FreeAndNil(jObject);
  8.                  end;

here the same, you're not creating jObject, is just a reference to an item in the array, so you don't need to free it because it's inside a container: the container manages the array not you.

Code: Pascal  [Select][+][-]
  1. //FreeAndNil(jData);  //here como exception
  2. jArray.Free;
  3.  

jArray is actually jData, with a different type cast, so you need to free only one.

esvignolo

  • Full Member
  • ***
  • Posts: 159
  • Using FPC in Windows, Linux, Macos
Re: There may be a problem in the library fpjson
« Reply #4 on: February 07, 2017, 04:23:02 pm »
Thanks @lainz! everyday i learn something new :)  8-)

 

TinyPortal © 2005-2018