#1 2021-12-15 08:54:06

Bsaidus
Member
From: Algeria
Registered: 2018-12-16
Posts: 50

Whats wrong with this code.

Hi,
I use lazarus-2.0.12-fpc322.
I have the following code on the Create constructor of the datamodule followed by the destructor.

constructor TgMainDM.Create(AOwner: TComponent);
var
  sHostPort: String;
  iErr: Integer;
begin
  inherited Create(AOwner);

  iErr        := 0;                           // Error ID = 0
  gfP         := Nil;                         // Connection ID = NULL
  gSQLCache   := TStringList.Create;          // Initialise SQL cache
  //gFormList   := TStringList.Create;        // List of forms displayed
  gId         := -1 ;                         // Global iD for Patients, ... := 0
  gJConfig    := fQueryGetConfig();           // Read config json file
  gBUseCache  := gJConfig.Uc_UseCache;        // Use SQL cach or not


  //gfP         := fQueryHttpSQLConnection( gJConfig.Uc_host,
  //                  gJConfig.Uc_port, gJConfig.Uc_db,
  //                  gJConfig.Uc_user, gJConfig.Uc_pass
  //);                                          // Get Server connection parameter

{ ------------------------------------------------------------------------------- }

  sHostPort := gJConfig.Uc_host + ':' + gJConfig.Uc_port;
  try
    try
      gfP := TSQLDBWinHTTPConnectionProperties.Create( sHostPort , gJConfig.Uc_db,
        gJConfig.Uc_user, gJConfig.Uc_pass );

      if ( gfP<>nil ) then
        gfP.VariantStringAsWideString := true;
    except on E: Exception do
      begin
        iErr := 1;
        MessageDlg('HttpReq', 'Error connecting to Server'+ LineEnding+LineEnding+E.Message, mtWarning, [mbOK], 0);
        //raise Exception.Create('Error connecting to HttpServer: ' + LineEnding + E.Message);
        //gfP := Nil;
        //Application.Terminate;
      end;
    end;
  finally
    if ( iErr = 1 ) then
       FreeAndNil(gfP);
  end;

{ ------------------------------------------------------------------------------- }

end;

destructor TgMainDM.Destroy;
begin

  if gfP<>nil then
    FreeAndNil(gfP);
  FreeAndNil(gSQLCache);
  //FreeAndNil(gFormList);
  inherited Destroy;
end; 

The application has MainForm and DataModule auto created.
the datamodule is created first to instanciate RemoteDB connection to server.
if the server is running, alls goes OK, but if the server is shutdown, the except block executes and error message will appear and the MainForm shown.
But when I close the application, a memory leak is reported by HeapTrc.

Main Project:

program teryal;

{$mode delphi}{$H+}

uses
  {$IFDEF UNIX}{$IFDEF UseCThreads}
  cthreads,
  {$ENDIF}{$ENDIF}
  Interfaces, // this includes the LCL widgetset
  Forms,
  rxnew,
  lazcontrols, datetimectrls, memdslaz, dbflaz,
  umain,
  umaindm
  ;

{$R *.res}

begin
  RequireDerivedFormResource := True;
  Application.Scaled := True;
  Application.Initialize;
  Application.CreateForm(TgMainDM, gMainDM);
  Application.CreateForm(TfrMain, frMain);
  Application.Run;
end. 

What's wrong. ???

Offline

#2 2021-12-15 10:14:53

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,659
Website

Re: Whats wrong with this code.

Sadly, I am not able to find a clear cause of your problems.

Most of the time, I would not initialize the connection within the Create / OnCreate events but within the OnShow event.
It is usually much safer.

Please follow the forum rules and don't post code directly in the thread, but create a minimal reproducible sample ready to be downloaded and debugged.

Offline

#3 2021-12-15 13:28:15

Bsaidus
Member
From: Algeria
Registered: 2018-12-16
Posts: 50

Re: Whats wrong with this code.

Hello ab,
here you can find a simple test:   


test mORMot RemoteDB


PS: Using CMEM in the top of all used units in the project file .lpr, resolve things, But I do not know if it really catches the leak.

Last edited by Bsaidus (2021-12-15 18:48:38)

Offline

#4 2021-12-16 09:15:55

Bsaidus
Member
From: Algeria
Registered: 2018-12-16
Posts: 50

Re: Whats wrong with this code.

I confirme, this is a Bug under Freepascal/lazarus.
You can test your self the code posted in the lazarus forum

https://forum.lazarus.freepascal.org/in … 544.0.html

Offline

#5 2021-12-17 12:20:26

Bsaidus
Member
From: Algeria
Registered: 2018-12-16
Posts: 50

Re: Whats wrong with this code.

Hi,
To contour the problème, I used IdTCPClient to check the availibility of the server before creating the TSQLDBWinHTTPConnectionProperties instance.

Thanks anyway

Offline

Board footer

Powered by FluxBB