Recent

Author Topic: [SOLVED] XML parser Validating problem / leak problem  (Read 721 times)

cris75

  • Jr. Member
  • **
  • Posts: 68
[SOLVED] XML parser Validating problem / leak problem
« on: July 04, 2023, 01:51:34 pm »
Hi, as a premise I would say that I'm an inexperienced programmer and have no idea if TDOMParser for XML validation can be used in a TTHread (if it's thread safe) but I tried and it worked..
So, i wrote this app (to be correct it's a daemon) inside there's an XML validation loop that does it's job every time there are new files available in an input queue;
although it was working, there was an increase of used memory at each cycle.
I checked twice everywhere for leaks and found none, it was really frustrating..
I rewrote the "XML validating loop" in an external test program and following exactly the wiki at https://wiki.lazarus.freepascal.org/XML_Tutorial and also with this little app it was there was a leak..
As a "totally random try" I added a TheDoc.Free to the TXMLDocument (the one used for the Parser.Parse call) just after the "Parser Free", and to my great surprise, the leak disappeared!
Nowhere in the wiki in the "Validating a document" section it's mentioned to "free" the TXMLDocument, so I'm asking if it's needed or not and in that case if the wiki is incorrect..
Could someone of you pro guys give a look in this?

Also, coul I have advice about what i explained in the beginning of the post, as an inexperienced programmer I used this validation loop with TDomParser inside a thread, is it dangerous somehow?
Thank you  :)
« Last Edit: July 13, 2023, 12:53:32 pm by cris75 »
Lazarus: 3.2 / FPC: 3.2.2 [x86_64-win64-win32/win64]
Win10 x64
Debian 12

Zvoni

  • Hero Member
  • *****
  • Posts: 2961
Re: XML parser Validating problem / leak problem
« Reply #1 on: July 04, 2023, 03:02:50 pm »
i would agree, that the Wiki is misleading, since "TheDoc" is an out-parameter in the "Parse"-procedure.

No idea about the Thread-thing
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

PascalDragon

  • Hero Member
  • *****
  • Posts: 5938
  • Compiler Developer
Re: XML parser Validating problem / leak problem
« Reply #2 on: July 05, 2023, 11:29:55 pm »
I rewrote the "XML validating loop" in an external test program and following exactly the wiki at https://wiki.lazarus.freepascal.org/XML_Tutorial and also with this little app it was there was a leak..
As a "totally random try" I added a TheDoc.Free to the TXMLDocument (the one used for the Parser.Parse call) just after the "Parser Free", and to my great surprise, the leak disappeared!
Nowhere in the wiki in the "Validating a document" section it's mentioned to "free" the TXMLDocument, so I'm asking if it's needed or not and in that case if the wiki is incorrect..
Could someone of you pro guys give a look in this?

The examples on the Wiki should be fixed. As Zvoni wrote, the parameter for the TXMLDocument is an out-parameter and the user is responsible for cleaning it up.

Also, coul I have advice about what i explained in the beginning of the post, as an inexperienced programmer I used this validation loop with TDomParser inside a thread, is it dangerous somehow?

The parsing is self contained and does not rely on global state (at least as far as I'm aware of), thus you can safely use it inside a thread.

cris75

  • Jr. Member
  • **
  • Posts: 68
Re: XML parser Validating problem / leak problem
« Reply #3 on: July 13, 2023, 12:53:12 pm »
Thank you very much to both of you,
Cris  :)
Lazarus: 3.2 / FPC: 3.2.2 [x86_64-win64-win32/win64]
Win10 x64
Debian 12

 

TinyPortal © 2005-2018