Recent

Author Topic: getHTTPFile and memory leaks  (Read 5530 times)

IPguy

  • Sr. Member
  • ****
  • Posts: 385
getHTTPFile and memory leaks
« on: July 19, 2011, 03:51:50 am »
The below code works as long as I comment out the s.Free line in the function, but I have 3 unfreed memory blocks.
When it works, I receive the desired simple string from my web server.

When I enable the s.Free line, I get a SIGSEGV.

And I think I should create and free the variable VerFromServer  in the calling procedure, but when I add the the create / free, I also get a SIGSEGV.

Any thoughts / suggestions / RTFM links?  (ah, that would be "Read the FINE manual!"  grin)

note: I'm not attempting to do a httpS transaction.

Code: [Select]
Function GetHTTPfile(const url: string) : tstringlist;
// gets a file from the named url and returns the file contents
// I'm using this to get the current version of the application from the web server
var
  s : tstringlist;
  gotResponse : Boolean;
begin
  s := tstringlist.create;
  gotResponse := HttpGetText(url,s);
  // showmessage(s.text);
  if gotResponse = true
    then GetHTTPFile := s;
  // s.Free;
end;  // of GetHTTPfile
                                         


Procedure CheckforPgmUpdate();
var
  VerFromServer : tStringList;
  VerServer     : String;
begin
  VerFromServer :=   GetHTTPfile(VerFile);  // VerFile is a string containing the desired URL...
  VerServer := VerFromServer[0];

  If VerFromServer[0] > ver1  // ie: a newer version is available ...
    then ShowMessage('A newer version of the progarm is available.'
                        + SLineBreak + 'Visit the web site to download it.');

end;  // for CheckforPgmUpdate 
« Last Edit: July 19, 2011, 03:54:11 am by IPguy »

IPguy

  • Sr. Member
  • ****
  • Posts: 385
Re: getHTTPFile and memory leaks
« Reply #1 on: July 19, 2011, 04:01:49 am »
http://www.lazarus.freepascal.org/index.php/topic,10551.msg52415.html#msg52415
Looks like Indy might exhibit the same/similar memory leak, based on the comment in Sept 2010.

IPguy

  • Sr. Member
  • ****
  • Posts: 385
Re: getHTTPFile and memory leaks
« Reply #2 on: July 22, 2011, 04:17:40 am »
Bump.  Any thoughts?

Ocye

  • Hero Member
  • *****
  • Posts: 518
    • Scrabble3D
Re: getHTTPFile and memory leaks
« Reply #3 on: July 26, 2011, 04:48:47 pm »
Don't mix local and referenced variables. You assign the function's result to a TStringlist that is created (and freed) within the function itself. To solve the problem, submit a valid object.

Code: [Select]
Function GetHTTPfile(const url: string) : TStrings;
begin
  HttpGetText(url,Result);
  //it makes no sense to put the call in a separet function
end;

Procedure CheckforPgmUpdate();
var
  VerFromServer : TStringList;
begin
  VerFromServer:=TStringList.Create;
  try
    VerFromServer :=   GetHTTPfile(VerFile);
    if VerFromServer.Count>0 then...
  finally
    VerFromServer.Free;
  end;
end;

If the method HttpGetText() is defined as HttpGetText(const aUrl:string; out/var aText:string) you should not use a TStringList.
Lazarus 1.7 (SVN) FPC 3.0.0

IPguy

  • Sr. Member
  • ****
  • Posts: 385
Re: getHTTPFile and memory leaks
« Reply #4 on: July 27, 2011, 05:19:39 am »
Ocye,
Thanks for the help.  That solved my issues.

I have a few places that I check for the version and I was attempting to create a common routine that could be called from a few locations - hence the separation of the function and the procedure.

HttpGetText is part of the Ararat Synapse code and is defined as shown below:
function HttpGetText(const URL: string; const Response: TStrings): Boolean;
                                                                               

 

TinyPortal © 2005-2018