Lazarus

Free Pascal => Unix => Topic started by: McKay1717 on May 29, 2017, 08:42:26 am

Title: Fcl-JSON and CMEM
Post by: McKay1717 on May 29, 2017, 08:42:26 am
Hello,

I've some memory leak in my app so i've run valgrind to found the problem.
Valgrind give this output: https://paste.ubuntu.com/24700111/

I can read a greats number of problems with fcl-json and that problem to be point to cmem.

Have you any idea to solve this problem?

Thank's in advence.
Title: Re: Fcl-JSON and CMEM
Post by: sky_khan on May 29, 2017, 09:12:21 am
Those more look like non-freed form resources/components, not json related? Btw, source code would be much easier to examine than excessive amount of errors
Title: Re: Fcl-JSON and CMEM
Post by: McKay1717 on May 29, 2017, 10:01:27 am
Indeed, i think i have do some mistake with the graphic component, what's the best way to close and free a graphical object ?
Title: Re: Fcl-JSON and CMEM
Post by: sky_khan on May 29, 2017, 10:19:34 am
As a general rule, if you create a component with an owner (putting it on a form covers this) it's owner is responsible to free it but if you create a component without owner in code then you are responsible. You need to call it's free procedure. This is for component derivatives. Other objects like stringlist, bitmap etc. always needs to be freed manually. Best practice like below
Code: Pascal  [Select][+][-]
  1. var
  2.   bmp : TBitmap;
  3. begin
  4.   bmp:=TBitmap.Create;
  5.   try
  6.     // Do something with bmp
  7.   finally
  8.     bmp.free;
  9.   end;
  10. end;
  11.  

Title: Re: Fcl-JSON and CMEM
Post by: McKay1717 on May 29, 2017, 12:40:04 pm
That's is strange because i never create graphical object explicitly.
There is my code :
https://paste.debian.net/hidden/7460d837/ <- The Update Thread
https://paste.debian.net/hidden/214db026/ <- The Model
https://paste.debian.net/hidden/a43e12df/ <- The TForm

Thank's you for your help
Title: Re: Fcl-JSON and CMEM
Post by: balazsszekely on May 29, 2017, 02:28:28 pm
Hi McKay1717,

The leak is caused by the GetJSON function. I mean the function is not leaking, however you must free jData in the end.

regards,
GetMem
Title: Re: Fcl-JSON and CMEM
Post by: McKay1717 on May 29, 2017, 02:36:00 pm
i've add jData.Free at the end of my function but that's haven't any effect
Title: Re: Fcl-JSON and CMEM
Post by: sky_khan on May 29, 2017, 02:38:38 pm
It does not have to be graphical object. I took a glance of your code and could not see that you are freeing most objects like TFPObjectList instances. If you dont free these lists you create memory leaks both for theirselves and for their contents. Keep in mind, for every object you create you need to have destruction (by calling free) somewhere. Most probably in an overriden destructor (Destroy) in TLaserUpdateThread. Plus, notice that you need to free what GetJSON returns. It creates new JsonData and you're responsible to free it. Last note, thread destruction is more tricky if you want to wait them to finish and read their results but you need to free them too somehow.
Title: Re: Fcl-JSON and CMEM
Post by: McKay1717 on May 29, 2017, 03:06:19 pm
All TFPObjectList as a Free. TFPObjectList who wasn't free in thread are free in the "Model" and all of TFPObjectList who was'nt Free in "Model" was free in controller.  Finally I think, I can be wrong.
Title: Re: Fcl-JSON and CMEM
Post by: BlueIcaro on May 29, 2017, 03:58:35 pm
Hi, local Pointers used in the procedures like this:
Code: [Select]
procedure TLaser.Update;
var
   p: Pointer; <--- This one
   tmp: TFPObjectList;
begin
(..)
for p in tmp.List do
        begin
             PointList.Add(TPointStruct(p).clone);
        end;
(...)
I think aren¡t freed,
Can be this the problem?
/BlueIcaro
Title: Re: Fcl-JSON and CMEM
Post by: McKay1717 on May 29, 2017, 04:27:38 pm
I've finally found the problem. I've disable cmem and the memory usage will be stable.
Thanks you everyone for your help
(You can find the difference of memory usage in the following graph http://imgur.com/a/7iKMU)
TinyPortal © 2005-2018