[fpc-pascal] possible json parser memory leak
Michael Van Canneyt
michael at freepascal.org
Sun Dec 18 18:39:35 CET 2011
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.
Michael.
More information about the fpc-pascal
mailing list