troubleshooting Question

Freepascal memory leak Pt2

Avatar of lloydie-t
lloydie-t asked on
PascalDelphi
23 Comments1 Solution1178 ViewsLast Modified:
I had a memory leak in an app I wrote which I thought I had resolved.  unfortunately I have completely resolved the problem. I manage to reoslve the bigger leaks but I have one remaining, which I think I have tracked down but not sure how to solve it.

It seems as though old threads are not being destroyed, so that everytime a new connection is made memory increases.

code follows
unit main;
 
{$mode objfpc}{$H+}
 
interface
 
uses
  cmem,cthreads,Classes, SysUtils, passqlite, BlckSock, synsock, oxounit, BaseUnix;
  Function CallConv (S1: String): String;
 
 var
  LiteDB1 : Tlitedb;
  RegExpStr1,RegExpEnd1,PhoneSysID1,dtimeout1,SysPort1,SysBind1,SiteID1, SysType1 :String;
  Sock:TTCPBlockSocket;
  ClientSock:TSocket;
  par,LCMD,PortNo:String;
  Baud,databits,stopbits:integer;
  parity:char;
  active,hardwarefl,softwarefl:boolean;
  pid  : pid_t;
  secs : longint;
 
  type
  TTCPEchoThrd = class(TThread)
  private
    Sock:TTCPBlockSocket;
    CSock: TSocket;
    S:String;
  public
    Constructor Create (hsock:tSocket);
    procedure Execute; override;
  end;
 
 tmyconverterfunc=function(AData:String):String;
 
implementation
 
var
  myconverterfunc:tmyconverterfunc;
 
 
 Constructor TTCPEchoThrd.Create(Hsock:TSocket);
begin
  Csock := Hsock;
  FreeOnTerminate:=False;
  inherited create(false);
end;
 
 
procedure TTCPEchoThrd.Execute;
begin
  sock:=TTCPBlockSocket.create;
  FreeOnTerminate := True;
  try
    Sock.socket:=CSock;
    sock.GetSins;
    with sock do
      begin
        repeat
          if terminated then break;
          S := RecvString(strToInt(dtimeout1));
          if lastError<>0 then break;
             myconverterfunc:=@CallConv;
             myconverterfunc(S);
          if lastError<>0 then break;
        until false;
      end;
  finally
    Sock.Free;
  end;
end;
 
Function CallConv (S1: String): String;
begin
 oxoparse1.PbxGet(RegExpStr1,S1,SiteID1);
end;
 
 
 
  begin
    { daemonize }
   pid := fpFork;
   Case pid of
      0 : Begin { we are in the child }
         Close(input);  { close standard in }
         Close(output); { close standard out }
         Assign(output,'/dev/null');
         ReWrite(output);
         Close(stderr); { close standard error }
         Assign(stderr,'/dev/null');
         ReWrite(stderr);
      End;
       -1 : secs := 0;     { forking error, so run as non-daemon }
      Else Halt;          { successful fork, so parent dies }
   End;
  LiteDB1 := TliteDB.Create(nil, '/var/lib/vtlogger/collex.sdb3');
  LiteDB1.Query('SELECT * from sys_col where col_active = 1');
  sysType1 := LiteDB1.Results[0][1];
  SiteID1 := LiteDB1.Results[0][3];
  SysPort1 := LiteDB1.Results[0][4];
  SysBind1 := LiteDB1.Results[0][5];
  dtimeout1 := LiteDB1.Results[0][11];
  LiteDB1.Query('SELECT * from sys_par where sys_id = '+sysType1);
  PhoneSysID1 := LiteDB1.Results[0][0];
  RegExpStr1 := LiteDB1.Results[0][2];
  RegExpEnd1 := LiteDB1.Results[0][3];
  LiteDB1.Close;
 
  Sock:=TTCPBlockSocket.create;
  with sock do
    begin
      CreateSocket;
      setLinger(true,10);
      bind(SysBind1,SysPort1);
      listen;
      repeat
        if canread(1000) then
          begin
            ClientSock:=accept;
            if lastError=0 then TTCPEchoThrd.create(ClientSock);
          end;
      until false;
    end;
 
 
//  end;
end.
ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 1 Answer and 23 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 23 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros