Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2017-11

Memory Leak's in XMLRead unit

6 messages · 4 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

African Wild Dog

Hello,

Using ReadXMLFile function from XMLRead unit generates memory leaks when trying to read a invalid XML content.

==== CODE ====

program xml_leak_test;
uses
  heaptrc,
  Classes,
  DOM,
  XMLRead;
var
  XMLDocument: TXMLDocument;
  InvalidStream: TStringStream;
const
  INVALID_XML_CONTENT = '<<<INVALID XML>>>';
begin
  SetHeapTraceOutput('heap.trc');
  XMLDocument := nil;
  InvalidStream := TStringStream.Create(INVALID_XML_CONTENT);
  try
     ReadXMLFile(XMLDocument, InvalidStream);
  finally
    InvalidStream.Free;
    XMLDocument.Free;
  end;
end.

=======

HEAP trace content:

==== HEAP ===
Heap dump by heaptrc unit
105 memory blocks allocated : 18943/19216
101 memory blocks freed : 18639/18912
4 unfreed memory blocks : 304
True heap size : 491520 (192 used in System startup) True free heap : 490400
Should be : 490512
Call trace for block $00000000011EA650 size 40
$000000010000B7AB
$0000000100010A62
$0000000100010B6C
$000000007720812D
$00000000771F855F
$000000007722BCB8
$000007FEFD0BA06D
$BAADF00DBAADF00D
Call trace for block $0000000001200B80 size 128
$00000001000141E6
$000000010000B78B
$00000001000107D7
$0000000100010896
$0000000100037D56
$0000000100037C6E
$0000000100037A82
$BAADF00DBAADF00D
Call trace for block $0000000001208B70 size 88
$0000000100014347
$000000010000B78B
$0000000100005E15
$000000010000513A
$000000010002B9E8
$000000010002C000
$000000010002E4AF
$BAADF00DBAADF00D
Call trace for block $00000000011EA350 size 48
$000000010000B6F2
$00000001000090A7
$00000001000360A1
$0000000100037C42
$0000000100037A82
$000000010003786B
$00000001000389F8
$BAADF00DBAADF00D
==========

Vojtěch Čihák
Hi,
if I run the code it writes "Unhandled exception". I tried to add another try..except block and it's OK. 
 
program xml_leak_test;
{$mode objfpc}
uses
  heaptrc,
  Classes,
  DOM,
  XMLRead;
var
  XMLDocument: TXMLDocument;
  InvalidStream: TStringStream;
 
const
  INVALID_XML_CONTENT = '<<<INVALID XML>>>';
 
begin
  SetHeapTraceOutput('heap.trc');
 
  XMLDocument := nil;
  InvalidStream := TStringStream.Create(INVALID_XML_CONTENT);
  try
    try
      ReadXMLFile(XMLDocument, InvalidStream);
    except
 
    end;
  finally
    InvalidStream.Free;
    XMLDocument.Free;
  end;
 
end.
 
Note: when I tried your original code in Lazaur method, it didn't
Show quoted text 5 lines
Od: African Wild Dog Komu: FPC-Pascal users discussions Datum: 07.11.2017 23:19 Předmět: [fpc-pascal] Memory Leak's in XMLRead unit

Hello,Using ReadXMLFile function from XMLRead unit generates memory leaks when trying to read a invalid XML content.....

----------

Michael Van Canneyt
to African Wild Dog: Using ReadXMLFile function from XMLRead unit generates memory leaks when trying to read a…
African Wild Dog wrote
Hello, Using ReadXMLFile function from XMLRead unit generates memory leaks when trying to read a invalid XML content.

The "memory leak" is the exception object: you are not catching the exception.

Change your program to the below, and you'll see that it is not leaking memory in the XML routines itself.

Michael.

---

program xml_leak_test;

uses
Classes,
DOM,

   XMLRead;
Procedure doit;
var
   XMLDocument: TXMLDocument;
   InvalidStream: TStringStream;
const
   INVALID_XML_CONTENT = '<<<INVALID XML>>>';
begin
   XMLDocument := nil;
   InvalidStream := TStringStream.Create(INVALID_XML_CONTENT);
   try
      ReadXMLFile(XMLDocument, InvalidStream);
   finally
     InvalidStream.Free;
     XMLDocument.Free;
   end;
end;
begin
   SetHeapTraceOutput('heap.trc');
   try
     doit;
   except
   end;
end.
African Wild Dog
to Michael Van Canneyt

2017-11-07 23:57 GMT-02:00 Michael Van Canneyt :

Show quoted text 9 lines
The "memory leak" is the exception object: you are not catching the exception. Change your program to the below, and you'll see that it is not leaking memory in the XML routines itself. Michael.

In fact, protecting with Try..Except does not generate the leak. I will try to reproduce this error again. I noticed this behavior in my FCGI APP: a client was sending a mal-formed XML content and my app was generating the leaks.

My environment: Linux Debian Stretch 64 bits - Lazarus 1.6.4 - fpc 3.0.2.

I'm quite impressed with the processing power of fastcgi fcl-web applications: they consume little memory (less than 2 MB per instance) and are very fast to process the requests.

Regards

Michael Van Canneyt
to African Wild Dog
African Wild Dog wrote 13 lines
2017-11-07 23:57 GMT-02:00 Michael Van Canneyt : > > The "memory leak" is the exception object: you are not catching the > exception. > > Change your program to the below, and you'll see that it is not leaking > memory in the XML routines itself. > > Michael. > In fact, protecting with Try..Except does not generate the leak.

That is because you catch the exception object.
If you enable line info, you see that the unreleased memory is the exception object and its error message.

Michael.

Marcos Douglas B. Santos
to African Wild Dog: 2017-11-07 23:57 GMT-02:00 Michael Van Canneyt : In fact, protecting with Try..Except…
African Wild Dog wrote 9 lines
2017-11-07 23:57 GMT-02:00 Michael Van Canneyt : > > ... > My environment: Linux Debian Stretch 64 bits - Lazarus 1.6.4 - fpc 3.0.2. I'm quite impressed with the processing power of fastcgi fcl-web applications: they consume little memory (less than 2 MB per instance) and are very fast to process the requests.

It is fast even on Windows+IIS. ;)

Best regards,
Marcos Douglas