memleak in fphttpclient? lazarus / 62-General ---------------------------------------------------------------------- billyb123 · 2017-01-24 16:51 valgrind says this prog leaks memory (about 80kb). BUT i noticed when i put 'http' (for example, http://www.example.com/) instead of 'https' it doesnt leak memory. can someone explain this to me? why do i have memleak here? Code: [Select]{$MODE DELPHI} program test; uses   fphttpclient; const   url = 'https://api.darksky.net/'; var   page: ansistring;   http: tfphttpclient; begin   http := tfphttpclient.create (nil);   try     page := http.get (url);   finally     http.free;   end;   writeln (page); end. ---------------------------------------------------------------------- Thaddy · 2017-01-24 17:01 fphttpclient uses sslsockets for https.(in the implementation uses clause) 80Kb looks like a context not being free'd by sslsockets. I would not worry too much unless the leak is progressive (increases when doing multiple https visits). Will have a look. ---------------------------------------------------------------------- molly · 2017-01-24 17:06 Would be great if you could have a look Thaddy. Secure or not, heaptrc keeps telling me there is leakage (3.0, win32) ---------------------------------------------------------------------- rvk · 2017-01-24 17:27 I'm getting an 404 exception with your program (with that page). Try the following: 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 {$MODE DELPHI}program project1;uses  fphttpclient; const  url = 'https://api.darksky.net/'; var  page: ansistring;  http: tfphttpclient; begin  http := tfphttpclient.create (nil);  try    try      page := http.get (url);    except      writeln('woops, exception');    end;  finally    http.free;  end;  writeln (page);end. So without proper exception handling it seems not everything is freed. With above example I don't get a leak. ---------------------------------------------------------------------- molly · 2017-01-24 17:32 Thanks rvk, i can confirm. Strange. ---------------------------------------------------------------------- rvk · 2017-01-24 17:39 Yes. Without the exception handling this is what I get. So the page returns a 404. Get only expects a 200 and otherwise throws an exception. Internally there is a leak that with throwing an exception something is not freed. But then it's weird that if I handle the exception at a higher level that block does get freed. Code: [Select]An unhandled exception occurred at $00414579: EHTTPClient: Unexpected response status code: 404   $00414579  TFPCUSTOMHTTPCLIENT__READRESPONSE,  line 1089 of ./fcl- web/src/base/fphttpclient.pp   $00414E75  TFPCUSTOMHTTPCLIENT__DONORMALREQUEST,  line 1179 of ./fcl-web/src/base/fphttpclient.pp   $00415133  TFPCUSTOMHTTPCLIENT__DOMETHOD,  line 1232 of ./fcl- web/src/base/fphttpclient.pp   $004156A9  TFPCUSTOMHTTPCLIENT__HTTPMETHOD,  line 1331 of ./fcl- web/src/base/fphttpclient.pp   $00415867  TFPCUSTOMHTTPCLIENT__GET,  line 1367 of ./fcl- web/src/base/fphttpclient.pp   $004158BC  TFPCUSTOMHTTPCLIENT__GET,  line 1398 of ./fcl- web/src/base/fphttpclient.pp   $0040180A  main,  line 16 of project1.lpr Heap dump by heaptrc unit 376 memory blocks allocated : 17237/18384 372 memory blocks freed     : 17080/18216 4 unfreed memory blocks : 157 True heap size : 491520 (96 used in System startup) True free heap : 490960 Should be : 491000 Call trace for block $01814660 size 64   $00409D6C  fpc_raiseexception,  line 165 of C:/dev/fpc/rtl/inc/except.inc   $004145B3  TFPCUSTOMHTTPCLIENT__READRESPONSE,  line 1090 of ./fcl- web/src/base/fphttpclient.pp   $00414E75  TFPCUSTOMHTTPCLIENT__DONORMALREQUEST,  line 1179 of ./fcl-web/src/base/fphttpclient.pp   $00415133  TFPCUSTOMHTTPCLIENT__DOMETHOD,  line 1232 of ./fcl- web/src/base/fphttpclient.pp   $004156A9  TFPCUSTOMHTTPCLIENT__HTTPMETHOD,  line 1331 of ./fcl- web/src/base/fphttpclient.pp   $00415867  TFPCUSTOMHTTPCLIENT__GET,  line 1367 of ./fcl- web/src/base/fphttpclient.pp   $004158BC  TFPCUSTOMHTTPCLIENT__GET,  line 1398 of ./fcl- web/src/base/fphttpclient.pp   $0040180A  main,  line 16 of project1.lpr Call trace for block $0174D9D8 size 24   $00409D6C  fpc_raiseexception,  line 165 of C:/dev/fpc/rtl/inc/except.inc   $004145B3  TFPCUSTOMHTTPCLIENT__READRESPONSE,  line 1090 of ./fcl- web/src/base/fphttpclient.pp   $00414E75  TFPCUSTOMHTTPCLIENT__DONORMALREQUEST,  line 1179 of ./fcl-web/src/base/fphttpclient.pp   $00415133  TFPCUSTOMHTTPCLIENT__DOMETHOD,  line 1232 of ./fcl- web/src/base/fphttpclient.pp   $004156A9  TFPCUSTOMHTTPCLIENT__HTTPMETHOD,  line 1331 of ./fcl- web/src/base/fphttpclient.pp   $00415867  TFPCUSTOMHTTPCLIENT__GET,  line 1367 of ./fcl- web/src/base/fphttpclient.pp   $004158BC  TFPCUSTOMHTTPCLIENT__GET,  line 1398 of ./fcl- web/src/base/fphttpclient.pp   $0040180A  main,  line 16 of project1.lpr Call trace for block $0178CA60 size 49   $0042434B  FORMAT,  line 403 of C:/dev/fpc/rtl/objpas/sysutils/sysformt.inc   $00424822  FORMAT,  line 1066 of C:/dev/fpc/rtl/objpas/sysutils/sysstr.inc   $00427D9E  EXCEPTION__CREATEFMT,  line 190 of C:/dev/fpc/rtl/objpas/sysutils/sysutils.inc   $004145A7  TFPCUSTOMHTTPCLIENT__READRESPONSE,  line 1090 of ./fcl- web/src/base/fphttpclient.pp   $00414E75  TFPCUSTOMHTTPCLIENT__DONORMALREQUEST,  line 1179 of ./fcl-web/src/base/fphttpclient.pp   $00415133  TFPCUSTOMHTTPCLIENT__DOMETHOD,  line 1232 of ./fcl- web/src/base/fphttpclient.pp   $004156A9  TFPCUSTOMHTTPCLIENT__HTTPMETHOD,  line 1331 of ./fcl- web/src/base/fphttpclient.pp   $00415867  TFPCUSTOMHTTPCLIENT__GET,  line 1367 of ./fcl- web/src/base/fphttpclient.pp Call trace for block $0174D978 size 20   $004145A7  TFPCUSTOMHTTPCLIENT__READRESPONSE,  line 1090 of ./fcl- web/src/base/fphttpclient.pp   $00414E75  TFPCUSTOMHTTPCLIENT__DONORMALREQUEST,  line 1179 of ./fcl-web/src/base/fphttpclient.pp   $00415133  TFPCUSTOMHTTPCLIENT__DOMETHOD,  line 1232 of ./fcl- web/src/base/fphttpclient.pp   $004156A9  TFPCUSTOMHTTPCLIENT__HTTPMETHOD,  line 1331 of ./fcl- web/src/base/fphttpclient.pp   $00415867  TFPCUSTOMHTTPCLIENT__GET,  line 1367 of ./fcl- web/src/base/fphttpclient.pp   $004158BC  TFPCUSTOMHTTPCLIENT__GET,  line 1398 of ./fcl- web/src/base/fphttpclient.pp   $0040180A  main,  line 16 of project1.lpr   $004158BC  TFPCUSTOMHTTPCLIENT__GET,  line 1398 of ./fcl- web/src/base/fphttpclient.pp ---------------------------------------------------------------------- molly · 2017-01-24 17:42 in case it matter, i tried simple www.google.com and that throws a 302 with similar outcome (mode objfpc). ---------------------------------------------------------------------- rvk · 2017-01-24 18:28 The problem isn't in fphttpclient at all. It is in sysutils. When you don't handle an exception in your console program and sysutils is used... you get a leak when the program ends. This is only when the finalization handles the exception. Example: 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 program project1;uses sysutils;var  i: integer;begin  try    i := 0;    i := 7 div i;  finally    writeln('finally');  end;  writeln('normal end');end. Solution... always handle any exception in a console program when you're using sysutils (which is used by fphttpclient). It's also not a recurring leak (only when the program end, so it might not matter a lot). ---------------------------------------------------------------------- billyb123 · 2017-01-24 19:19 your example still leaks here. if i put a normal address in url like 'https://webflow.com/discover/popular' which gets http response 200, and the 'except' section, it still leaks 80kb. Quote from: rvk on January 24, 2017, 06:27:56 pmI'm getting an 404 exception with your program (with that page). Try the following: 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 {$MODE DELPHI}program project1;uses  fphttpclient; const  url = 'https://api.darksky.net/'; var  page: ansistring;  http: tfphttpclient; begin  http := tfphttpclient.create (nil);  try    try      page := http.get (url);    except      writeln('woops, exception');    end;  finally    http.free;  end;  writeln (page);end. So without proper exception handling it seems not everything is freed. With above example I don't get a leak. ---------------------------------------------------------------------- rvk · 2017-01-24 19:32 Quote from: billyb123 on January 24, 2017, 08:19:18 pmyour example still leaks here. if i put a normal address in url like 'https://webflow.com/discover/popular' which gets http response 200, and the 'except' section, it still leaks 80kb. Not for me. Source below gives me: Code: [Select]no leak. page-length: 402866 Heap dump by heaptrc unit 379 memory blocks allocated : 20288435/20289736 379 memory blocks freed     : 20288435/20289736 0 unfreed memory blocks : 0 True heap size : 425984 (160 used in System startup) True free heap : 425824On: Lazarus 1.2.0 / FPC 2.6.2 32 bit Lazarus 1.6.2 / FPC 3.0.0 64 bit Lazarus 1.7 trunk / FPC 3.1.1 32 bit 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 {$MODE DELPHI}program project1;uses  fphttpclient; const  url = 'https://webflow.com/discover/popular'; var  page: ansistring;  http: tfphttpclient; begin  http := tfphttpclient.create (nil);  try    try      page := http.get (url);    except      writeln('woops, exception');    end;  finally    http.free;  end;  // writeln (page);  writeln ('no leak. page-length: ', length(page));end. ---------------------------------------------------------------------- Thaddy · 2017-01-24 20:07 @rvk Same here. No leaks after program termination. Code: [Select]no leak. page-length: 402866 Heap dump by heaptrc unit 468 memory blocks allocated : 20291888/20293664 468 memory blocks freed     : 20291888/20293664 0 unfreed memory blocks : 0 True heap size : 425984 True free heap : 425984