xml read memory leak question lazarus / 61-Beginners ---------------------------------------------------------------------- k1attila1 · 2021-12-07 05:45 Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i uses Laz2_DOM, laz2_XMLRead; var    doc:TXMLDocument;    sz1 : TDOMNode;begin  doc:=TXMLDocument.Create;  ReadXMLFile(doc,'alma.xml');   sz1:=doc.FindNode('szamla');   sz1.Free;                     <-- THE QUESTION : is this line important ?  doc.Free;                     //i think not because sz1 only a pointer in docend; and what about if : sz1.CreateElement(...   // in this case do we need sz1.free;  ???? thank you Attila [Edited to add code tags - please read How to Use the Forums.] ---------------------------------------------------------------------- dbannon · 2021-12-07 06:56 Assuming you are using the Lazarus IDE, go to Projects->ProjectOptions->Debugging and click the "Use HeapTrc Unit". Then, run your app with and without the line you are are interested in and see the difference. On Linux, the output appears on the Console output, on Windows (and Mac I think) pops up on a separate window on application close.  Its quite important you run that test from time to time.... Davo ---------------------------------------------------------------------- k1attila1 · 2021-12-07 14:45 thank you, but after this i get 8 unfreed blocks :  doc:=TXMLDocument.Create;  ReadXMLFile(doc,'c:\szla1.xml');  FreeAndNil(doc); ---------------------------------------------------------------------- wp · 2021-12-07 15:54 This would be logical, but the authors of the xml routines decided to go the confusing way. ReadXMLFile reads the xml document, but it also creates it - nothing in the procedure name to indicate that... So, do not create the XMLDocument explicitely. The following code is enough: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i uses  laz2_xmlread, laz2_dom;var  doc: TXMLDocument;begin  ReadXMLFile(doc, 'project1.lpi');  doc.Free;end; There is no need to free nodes which belong to the doc tree. They all are destroyed by the tree. ---------------------------------------------------------------------- k1attila1 · 2021-12-08 05:17 thank you it was the problem. ---------------------------------------------------------------------- PascalDragon · 2021-12-08 08:07 Quote from: wp on December 07, 2021, 04:54:08 pmThis would be logical, but the authors of the xml routines decided to go the confusing way. ReadXMLFile reads the xml document, but it also creates it - nothing in the procedure name to indicate that... But in the declaration of the routine: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure ReadXMLFile(out ADoc: TXMLDocument; const AFilename: String); overload; Pity that the compiler doesn't yet warn that the value assigned to doc isn't used... ---------------------------------------------------------------------- wp · 2021-12-08 10:03 Quote from: PascalDragon on December 08, 2021, 09:07:41 amCode: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure ReadXMLFile(out ADoc: TXMLDocument; const AFilename: String); overload; Pity that the compiler doesn't yet warn that the value assigned to doc isn't used... Most users won't understand such a warning (including myself...). It would have been better to write the routine such that the user would be forced to create the doc outside ReadXMLFile. The current version leads to the same mess which Lazarus has with Listbox.Items := FindAllFiles(...). Unfortunately, it cannot be changed any more.