Memory Leak's in XMLRead unit

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Memory Leak's in XMLRead unit

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
==========

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Memory Leak's in XMLRead unit

vojtech.cihak

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 leak at all.

 

V.

______________________________________________________________
> Od: African Wild Dog <[hidden email]>
> Komu: FPC-Pascal users discussions <[hidden email]>
> 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.
....


----------

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Memory Leak's in XMLRead unit

Michael Van Canneyt
In reply to this post by African Wild Dog


On Tue, 7 Nov 2017, 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.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Memory Leak's in XMLRead unit

African Wild Dog
2017-11-07 23:57 GMT-02:00 Michael Van Canneyt <[hidden email]>:

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

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Memory Leak's in XMLRead unit

Michael Van Canneyt


On Wed, 8 Nov 2017, African Wild Dog wrote:

> 2017-11-07 23:57 GMT-02:00 Michael Van Canneyt <[hidden email]>:
>
>>
>> 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.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Memory Leak's in XMLRead unit

Marcos Douglas B. Santos
In reply to this post by African Wild Dog
On Wed, Nov 8, 2017 at 10:52 AM, African Wild Dog
<[hidden email]> wrote:
> 2017-11-07 23:57 GMT-02:00 Michael Van Canneyt <[hidden email]>:
>>
>> ...
>>
> 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
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal