#1 2020-09-23 07:13:15

Encina
Member
Registered: 2020-09-23
Posts: 1

Memory leak using mormot.core.log in DLL

Delphi10.3.3+mormot2
(mormot\SynLog is ok)

////////// 1.1-dll Project1.dpr
library Project1;

uses
  uSynLog2, System.SysUtils, System.Classes;

{$R *.res}

function Test(AoMsg : PAnsiChar) : Integer; StdCall; export;
var
  sMsg : String;
begin
  Result := -1;
  sMsg := FormatDatetime('yyyy-MM-dd hh:nn:ss.ZZZ', Now);
  Log(sMsg);
  StrPCopy(AoMsg, sMsg );
  Result := 0;
end;

exports
  Test;

begin
  ReportMemoryLeaksOnShutdown := True;
  InitLog(16);
end.

////////// 1.2-dll uSynLog2.pas
unit uSynLog2;

interface

uses
  System.SysUtils, mormot.core.base, mormot.core.os, mormot.core.log, mormot.core.zip;

Type
  //提示性信息,警告,一般错误,严重错误
  TLogLevel = (llHint, llWarning, llError, llException);

procedure InitLog(ARotateFileCount : Integer = 16);
procedure Log(AMsg : String; ALevel : TLogLevel = llHint);

implementation

procedure InitLog(ARotateFileCount : Integer);
begin
  try
    with TSynLog.Family do
    begin
      Level := LOG_VERBOSE;
      NoEnvironmentVariable := True;
      CustomFileName := ExeVersion.ProgramName;
      DestinationPath := ExeVersion.ProgramFilePath + 'Log';
      if not DirectoryExists(DestinationPath) then
        ForceDirectories(DestinationPath);
      AutoFlushTimeOut := 10;
      OnArchive := EventArchiveZip;
      LocalTimestamp := True;
      RotateFileCount := ARotateFileCount;
      RotateFileSizeKB := 10240;
      RotateFileDailyAtHour := 0;
      FileExistsAction := acAppend;
    end;
  except
  end;
end;

procedure Log(AMsg : String; ALevel : TLogLevel);
var
  Level : TSynLogInfo;
begin
  case ALevel of
    llWarning : Level := sllCustom2; //警告
    llError : Level := sllCustom3; //一般错误
    llException : Level := sllCustom4; //警告
    else Level := sllCustom1; //提示性信息
  end;
  TSynLog.Add.Log(Level, AMsg);
end;

end.

////////// 2.1-exe unit1.pas
unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Memo1: TMemo;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

function Test(AoMsg : PAnsiChar) : Integer; StdCall; external 'Project1.dll';

{$R *.dfm}

function Test_(out AoMsg : String) : Integer;
var
  Msg : Array[0..1023] of AnsiChar;
begin
  Result := Test(Msg);
  AoMsg := StrPas(Msg);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Memo1.Clear;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  iRet : Integer;
  sMsg : String;
begin
  iRet := Test_(sMsg);
  Memo1.Lines.Add( Format('%d> %s', [iRet, sMsg]) );
end;

end.

////////// 3.1-run
Button1Click
Button1Click   (!important)
close......

Offline

#2 2020-09-23 07:42:56

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

Re: Memory leak using mormot.core.log in DLL

Please the forum rules and don't post huge piece of codes in the forum thread.
Use gist (or other remote service) to put some ready to be compiled example.

It is up to you to debug your own code, using Delphi memory leak reporting and good tools like FastMM4 in FullDebugMode to check where the memory leak appears.
If you use the logging in a separated dll, you may need to properly handle the library loading/releasing in your code.
I doubt ReportMemoryLeaksOnShutdown could be used like this in a Delphi dll.

Offline

Board footer

Powered by FluxBB