gpt4 book ai didi

delphi - TTaskBar内存泄漏

转载 作者:行者123 更新时间:2023-12-03 14:52:08 27 4
gpt4 key购买 nike

Embarcadero 的 TTaskbar 存在内存泄漏。由于我将此控件放在表单上,​​每次关闭应用程序时 FastMM 都会报告泄漏。

我尝试使用以下代码将 FastMM 静音:

procedure TMainForm.FormCreate(Sender: TObject);
begin
fastmm4.RegisterExpectedMemoryLeak(Taskbar);
end;

但它不会工作。如何注册此泄漏?

<小时/>

A memory block has been leaked. The size is: 100

This block was allocated by thread 0xC64, and the stack trace (return addresses) at the time was: 406A52 409A7B 409CAC 4283A0

[System.SysUtils][System][System.SysUtils.FmtStr] 409CC6 40D775 7628A65F
[Unknown function at StretchDIBits] 7731594E
[Unknown function at RtlpNtMakeTemporaryKey] 7731594E
[Unknown function at RtlpNtMakeTemporaryKey] 773168F8
[Unknown function at RtlpNtMakeTemporaryKey] 773168DC
[Unknown function at RtlpNtMakeTemporaryKey]

The block is currently used for an object of class: UnicodeString
The allocation number is: 2209

A memory block has been leaked. The size is: 36

This block was allocated by thread 0xC64, and the stack trace (return addresses) at the time was: 406A52 407D43 40846A 42CD40
[System.SysUtils][System][System.SysUtils.Exception.CreateFmt] 5DEDD7
[System.Win.TaskbarCore][System.Win][System.Win.TaskbarCore.TTaskbarBase.UpdateTab] 610F00
[Vcl.Taskbar][Vcl][Vcl.Taskbar.CheckMDI] 5DF39F
[System.Win.TaskbarCore][System.Win][System.Win.TaskbarCore.TTaskbarBase.ApplyTabsChanges] 610DB8
[Vcl.Taskbar][Vcl][Vcl.Taskbar.TCustomTaskbar.Initialize] 5EB044
[Vcl.Forms][Vcl][Vcl.Forms.TApplication.Run] 62573A
[MinimalTemplate.dpr][MinimalTemplate][MinimalTemplate.MinimalTemplate][26]

The block is currently used for an object of class: ETaskbarException
The allocation number is: 2207

This application has leaked memory. The small block leaks are (excluding expected leaks registered by pointer):

21 - 36 bytes: ETaskbarException x 1
85 - 100 bytes: UnicodeString x 1
[Vcl.Forms][Vcl][Vcl.Forms.TCustomForm.SetVisible] 5F5010

最佳答案

此代码中的内存从 System.Win.TaskbarCore 泄漏:

procedure TTaskbarBase.UpdateTab;
var
LpfIsiconic: LONGBOOL;
LHandle: HWND;
LFlags: Integer;
begin
if FTaskbarIsAvailable then
begin
LHandle := GetFormHandle;
if not FRegistered and TaskBar.RegisterTab(LHandle) then
begin
TaskBar.SetTabOrder(LHandle);
TaskBar.SetTabActive(LHandle);
FRegistered := True;
end
else
ETaskbarException.CreateFmt(SCouldNotRegisterTabException, [TaskBar.LastError]);
....

最后一行创建一个异常,然后不对其执行任何操作。异常及其拥有的字符串被泄漏。据 FastMM 报道。

如果您可以获得这些对象的地址,则可以将这些对象注册为已泄漏。但是,您不能这样做。无法引用此异常对象。

如果您只是必须避免这种错误报告的泄漏,并且这样做是有道理的,那么您需要在项目中包含 System.Win.TaskbarCore 的固定版本。复制该文件,并将其添加到您的项目中。然后修改代码修复故障。我的猜测是,事情会是这样的:

if not FRegistered then
begin
if TaskBar.RegisterTab(LHandle) then
begin
TaskBar.SetTabOrder(LHandle);
TaskBar.SetTabActive(LHandle);
FRegistered := True;
end
else
raise ETaskbarException.CreateFmt(SCouldNotRegisterTabException, [TaskBar.LastError]);
end;

显然这需要向 Embarcadero 报告。我建议您提交错误报告。

<小时/>

解决此问题的另一种方法是尝试完全避免假行的执行。我相信,如果您从 .dfm 文件中删除这一行,您应该避免伪造的行,从而避免泄漏:

Visible = True

只需删除该行即可,它似乎是触发器。

请注意,我通过将项目精简到只剩下骨架来解决这个问题。为了重现该问题,这是所需的最小 dfm 文件:

object Form1: TMainForm
Visible = True
object Taskbar1: TTaskbar
end
end

使用此 dfm 文件不会发生泄漏:

object Form1: TMainForm
object Taskbar1: TTaskbar
end
end

通过将项目削减到最低限度,我找到了触发点。我无法充分强调这种最小化复制技术的值(value)。

<小时/>

感谢Remy找到此故障的QC报告:QC#128865

关于delphi - TTaskBar内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28604631/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com