[fpc-pascal] possible json parser memory leak

ik idokan at gmail.com
Mon Dec 19 08:19:37 CET 2011


On Sun, Dec 18, 2011 at 19:39, Michael Van Canneyt
<michael at freepascal.org>wrote:

>
>
> On Sun, 18 Dec 2011, ik wrote:
>
>  Hello,
>>
>> I'm using FPC 2.6 (rc) x86_64 on Linux with JSONParser.
>>
>> I've written the following code:
>>
>> -----------------------
>> uses fpjson, jsonparser, SysUtils, classes;
>>
>> var
>>   parser    : TJSONParser;
>>   json_file : TFileStream;
>>
>> begin
>>   json_file := TFileStream.Create('/tmp/test.**json', fmOpenRead);
>>   json_file.position := 0;
>>   parser := TJSONParser.create(json_file);
>>   writeln('JSON: ', parser.Parse.AsJSON);
>>
>
> You are not freeing the result of the parse operation ?
>
> The user is responsible for freeing the result of .parse()
>
> So you must do
>
> Var
>  D: TJSONData;
>
>    parser := TJSONParser.create(json_file);
>   D:=Parser.parse;
>   writeln('JSON: ', D.AsJSON);
>   D.free; // Free result of parse.
>
> And while you're at it, you must always use try/finally blocks.
> Otherwise any exception (an error in the json) will result in your code
> not freeing the file stream or parser instance.
>

The fruit of this questions:
http://translate.google.com/translate?sl=auto&tl=en&js=n&prev=_t&hl=en&ie=UTF-8&layout=2&eotf=1&u=http%3A%2F%2Fidkn.wordpress.com%2F2011%2F12%2F19%2Fworking-withh-json-using-fpc%2F&act=url:)

They translated very good but not perfect as you'll see. And they destroyed
the source code there. I might upload it to github or something.


>
> Michael.
> _______________________________________________
> fpc-pascal maillist  -  fpc-pascal at lists.freepascal.org
> http://lists.freepascal.org/mailman/listinfo/fpc-pascal
>

Ido
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20111219/947d6819/attachment.html>


More information about the fpc-pascal mailing list