FastMM4 指出 Quick.Logger 存在内存泄漏,我做错了什么

问题描述 投票:0回答:1

也许我做错了,但我试图让日志刷新,所以我做了一个方法:

...
implementation

uses
  Quick.Logger,
  Quick.Logger.Provider.Files,
  Quick.Logger.ExceptionHook;
...
procedure FinalizeLogger; stdcall;
var
  count: Integer;
begin
  Log('Logger shutdown', etInfo);
  // Force the logger to flush any buffered messages.
  if Assigned(Logger) then
  begin
    count := Logger.QueueCount;
    Log('There are %d log items in queue.', [count], etInfo);
  end;

  if Assigned(GlobalLogFileProvider) then
  begin
    GlobalLogFileProvider.Drain;
    GlobalLogFileProvider.Stop;
  end;
end;

procedure InitializeLogger; stdcall;
begin
  Logger.Providers.Add(GlobalLogFileProvider);
  with GlobalLogFileProvider do
  begin
    FileName := 'D:\Source\Delphi\WalterWrapper\Logs\Native.log';
    // Specify an absolute path for debugging
    DailyRotate := True;
    MaxFileSizeInMB := 20;
    LogLevel := LOG_ALL;
    Enabled := True;
  end;

  Log('Logger initialized', etInfo);
end;

我有一个单元测试,没有任何问题,当我通过 GetIt 添加

Quick.Logger
并集成它时,出现错误。这是我的单元测试,在我的用例中使用日志记录:

[TestFixture]
EncyptionTest = class
  public
    [Setup]
    procedure Setup;
    [TearDown]
    procedure TearDown;
    [Test]
    procedure DeriveKeyFromPasswordTest;
    [Test]
    procedure DeriveSameKeyFromPasswordTest;
    [Test]
    procedure RoundTripTest;
  end;

procedure EncyptionTest.Setup;
begin
  InitializeLogger;
end;

procedure EncyptionTest.TearDown;
begin
  FinalizeLogger;
end;

我的

FinalizeLogger
方法一定有问题,不知道我能做什么。网站上没有我可以找到如何强制刷新日志的文档。

有什么建议吗?

这是日志文件的第一部分,就像它没有关闭一样:

--------------------------------2024/3/14 21:32:16--------------------------------
A memory block has been leaked. The size is: 40

This block was allocated by thread 0x9078, and the stack trace (return addresses) at the time was:
E657E7 [FastMM4.pas][FastMM4][_ZN7Fastmm411DebugGetMemEx][9670]
E1D124 [System][_ZN6System7_GetMemEx]
E22821 [System][_ZN6System7TObject11NewInstanceEv]
E23421 [System][_ZN6System12_ClassCreateEPva]
FB22E1 [System.Classes][_ZN6System7Classes11TFileStreamC3ENS_13UnicodeStringEt]
1225534 [Quick.Logger.Provider.Files.pas][Quick.Logger.Provider.Files][_ZN5Quick6Logger8Provider5Files16TLogFileProvider4InitEv][192]
11FB9E0 [Quick.Logger.pas][Quick.Logger][_ZN5Quick6Logger16TLogProviderBase10SetEnabledEb][1032]
122732B [StringUtils.pas][StringUtils][_ZN11Stringutils16InitializeLoggerEv][49]
128CCD1 [EncryptionTests.pas][EncryptionTests][_ZN15Encryptiontests13EncyptionTest5SetupEv][32]
1124DC1 [DUnitX.TestRunner][_ZN6Dunitx10Testrunner17TDUnitXTestRunner22ExecuteTestSetupMethodEN6System15DelphiInterfaceINS_18Internalinterfaces19ITestExecuteContextEEEjNS3_INS_13Extensibility12ITestFixtureEEENS3_INS7_5ITestEEERNS3_INS_13Testframework11ITestResul
112478D [DUnitX.TestRunner][_ZN6Dunitx10Testrunner17TDUnitXTestRunner12ExecuteTestsEN6System15DelphiInterfaceINS_18Internalinterfaces19ITestExecuteContextEEEjNS3_INS_13Extensibility12ITestFixtureEEENS3_INS_13Testframework14IFixtureResultEEE]

The block is currently used for an object of class: System.Classes.TFileStream

The allocation number is: 11973
delphi logging
1个回答
0
投票

通过将记录器添加到

Logger.Providers
至少两次,它会创建一个循环引用,使其保持活动状态。

但是

Logger.Providers.Remove(GlobalLogFileProvider)
将不起作用,因为
GlobalLogFileProvider
实例将被销毁,因为它的引用计数将降至 0。

这是 Quick.Logger 库中不幸设计的结果,因为它混合了对象和接口引用,以及一些非常幼稚的循环接口引用的使用,这些引用在某些情况下没有得到解决。

© www.soinside.com 2019 - 2024. All rights reserved.