[Lazarus] Memory leaks when I read an XML file

esby at free.Fr esby at free.Fr
Mon Jan 11 13:56:26 CET 2010


Besides what you corrected, the various 'exit' statement in your code
will leave an xdoc unfreed.

Pierre Delore wrote:
> Hi,
>
> I'm finishing to develop an application. Everything works fine.
Yesterday I made some tests and I noticed that I have a huge memory leak
when I try to load new data.
>
> I load 6000 lines of data that I transfer in TFPObjectList. I first
thought that the problem came from the list. I removed the list code and
the problem doesn't disappear. So, for me, the problem come from the XML
reading procedure. I try to search in the wiki and I didn't found thing
that help me. I sure that the method I use to read the XML is not right
one.
>
> Somebody can help me to correct my code?
>
>
> The method without TFPObjectList code is below. Before reading the file
the program use 16Mo of memory. After the execution of the 'LoadCGX'
procedure the program use 204Mo :( .
>
>
>>
-------------------------------------------------------------------------------------------------------------------
> procedure TCGData.LoadCGX(s:String);
> var
> xdoc: TXMLDocument; // variable to document
> RootNode,
> node, node1, TrackpointNode : TDOMNode; // variable to nodes
> i, j, err : Integer;
> tmpCourseName : string;
> tmpDescription : string;
> tmps, tmps1 : string;
> tmpTotalDistance,td : Double;
> tmpVersion : String;
> tmpTotalTimeSecond : integer;
> ts : String;
> Begin
>
> DateSeparator:='/';
> ShortDateFormat:='d/m/y';
>
> //-- Open the XML file --
> ReadXMLFile(xDoc, s);
>
>
> //-- Search the <CourseGenerator> node --
> RootNode := xDoc.FindNode('CourseGenerator');
>
> if RootNode=NIL then exit; //Not found exit
>
> For i:=0 to (RootNode.ChildNodes.Count - 1) do begin
> Node:=RootNode.ChildNodes.Item[i];
> tmps:=Node.NodeName;
>
> if tmps='Version' then begin
> //-- <Version> node --
> tmpVersion:= Node.TextContent;
> //Todo: test!
> end
> else if tmps='TotalDistance' then begin
> //-- <TotalDistance> node --
> Val(Node.TextContent,tmpTotalDistance,err);
> if err<>0 then exit;
> end
> else if tmps='TotalTimeSecond' then begin
> //-- <TotalTimeSecond> node --
> Val(Node.TextContent,tmpTotalTimeSecond,err);
> if err<>0 then exit;
> end
> else if tmps='CourseName' then begin
> //-- <CourseName> node --
> tmpCourseName:= Node.TextContent;
> end
> else if tmps='Description' then begin
> //-- <Description> node --
> tmpDescription:= Node.TextContent;
> end
>
> else if tmps='Trackpoint' then begin
> //-- <Trackpoint> node --
> TrackpointNode := Node;
>
> //-- Scan level "trackpoint" --
> for j := 0 to (TrackpointNode.ChildNodes.Count - 1) do begin
> Node1:=TrackpointNode.ChildNodes.Item[j];
> tmps1:=Node1.NodeName;
>
> if tmps1 = 'LatitudeDegrees' then begin
> //<LatitudeDegrees>123.456</LatitudeDegrees>
> val(Node1.TextContent,td,err);
> if err<>0 then begin
> td:=0;
> //Error
> end;
> end
> else if tmps1 = 'LongitudeDegrees' then begin
> //<LongitudeDegrees>123.456</LongitudeDegrees>
> val(Node1.TextContent,td,err);
> if err<>0 then begin
> td:=0;
> //Error
> end;
> end
> else if tmps1 = 'AltitudeMeters' then begin
> //<AltitudeMeters>123.456</AltitudeMeters>
> val(Node1.TextContent,td,err);
> if err<>0 then begin
> td:=0;
> //Error
> end;
> end;
> end; //For j
> //-- Store data --
> //List.Add(TmpData);
> end;
> end; //For i
>
>
> RootNode.free;
> xDoc.Free;
>
> End;
>>
-----------------------------------------------------------------------------------------------------
>
>
> Thanks for your help.
>
> Regards,
>


-- 
Yves Tennevin / esby <dot> free.fr

http://esby.free.fr/contact.html





More information about the Lazarus mailing list