[fpc-pascal] possible json parser memory leak

ik idokan at gmail.com
Sun Dec 18 22:11:43 CET 2011


On Sun, Dec 18, 2011 at 22:27, Frank Church <vfclists at gmail.com> wrote:

>
>
> On 18 December 2011 17:34, silvioprog <silvioprog at gmail.com> wrote:
>
>> 2011/12/18 ik <idokan at gmail.com>:
>> > Hello,
>> >
>> > I'm using FPC 2.6 (rc) x86_64 on Linux with JSONParser.
>> >
>> > I've written the following code:
>>
>> Use:
>>
>> var
>>  parser: TJSONParser;
>>  json_data: TJSONData;
>>  json_file: TFileStream;
>> begin
>>  json_file := TFileStream.Create('test.json', fmOpenRead);
>>  parser := TJSONParser.Create(json_file);
>>  json_data := parser.Parse;
>>  WriteLn(json_data.AsJSON);
>>  json_data.Free;
>>  json_file.Free;
>>  parser.Free;
>> end;
>>
>> Thx.
>>
>>
> I am getting some issues with memory leaks and want to see if this topic
> sheds some light on the issue.
> From the original posters question it appears that parse.Parse.AsJSON
> create an object which needed to be free although there was no explicit
> Txxx.Create in the code.
>
> >writeln('JSON: ', parser.Parse.AsJSON);
>
> Does this represent a common case where using AsXXX creates an object?
>

Nop, AsJSON is a function with string. Parse is function that return a
class. The thing is, that the function is designed to not to manage memory.
It can not actually.  If you wanted a memory manager it should have been
property and not a function.



>
>  Is there some way of telling whether some types of functions create
> objects which need to be freed even although they do not offer handles
> which can be used to free them?
>

I think most of them, if not all. Because you can not manage memory that
you do not own. When you return a created object and send it as a result.
Then it can not follow it anymore.


>
> It looks to me that lot of leaky code is created which looks right unless
> you know more about the functions used create objects..
>

The memory leak was in my code, due to usage of the library, not in the
library. But only after I saw the answer, I noticed it was a function and
not a property.


>
> I am using the XML libraries and beginning to suspect that lots of this
> kind of problem are lurking within the code I am writing.
>

>
> PS. Does every 'Call trace xxx' represent an object created that wasn't
> freed? I asked about this in another thread but haven't gotten a reply yet
>
>
>
>> --
>> Silvio Clécio
>> ===============================================
>> Blog - <silvioprog.com.br>
>> Twitter - <twitter.com/silvioprog>
>> Facebook - <facebook.com/silvioclecio>
>> LazSolutions - <code.google.com/p/lazsolutions>
>> Lazarus-BR - <groups.google.com.br/group/lazarus-br?hl=pt-BR>
>> ===============================================
>>    * Conheça nosso canal IRC sobre Lazarus: #lazarus-br *
>> ===============================================
>> _______________________________________________
>> fpc-pascal maillist  -  fpc-pascal at lists.freepascal.org
>> http://lists.freepascal.org/mailman/listinfo/fpc-pascal
>>
>
>
>
> --
> Frank Church
>
> =======================
> http://devblog.brahmancreations.com
>
> _______________________________________________
> fpc-pascal maillist  -  fpc-pascal at lists.freepascal.org
> http://lists.freepascal.org/mailman/listinfo/fpc-pascal
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20111218/f1c168ed/attachment.html>


More information about the fpc-pascal mailing list