有没有人可以解释为什么我的代码在TaskManager上会导致4KB的内存泄漏。Delphi 2005,服务应用:
unit uMain;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, SvcMgr, Dialogs,
ExtCtrls;
type
TgwDebugService_s = class(TService)
Timer1: TTimer;
procedure Timer1Timer(Sender: TObject);
procedure ServiceStop(Sender: TService; var Stopped: Boolean);
procedure ServiceExecute(Sender: TService);
private
{ Private declarations }
public
function GetServiceController: TServiceController; override;
{ Public declarations }
end;
var
gwDebugService_s: TgwDebugService_s;
implementation
{$R *.DFM}
procedure ServiceController(CtrlCode: DWord); stdcall;
begin
gwDebugService_s.Controller(CtrlCode);
end;
function TgwDebugService_s.GetServiceController: TServiceController;
begin
Result := ServiceController;
end;
procedure TgwDebugService_s.ServiceExecute(Sender: TService);
begin
// Service is Fired
while not Terminated do
ServiceThread.ProcessRequests(True);// wait for termination
end;
procedure TgwDebugService_s.ServiceStop(Sender: TService; var Stopped: Boolean);
begin
// Service stops
end;
procedure TgwDebugService_s.Timer1Timer(Sender: TObject);
var
sl : TStringList;
begin
Timer1.Enabled := False;
Sleep(1000);
sl := TStringList.Create;
try
//if FileExists( 'c:\OnlyOnMyPc\test.txt' ) then
sl.LoadFromFile( 'c:\OnlyOnMyPc\test.txt' ); // remove this line and will be find
sl.Add( 'Test @ ' + FormatDateTime( 'dd.mm.yyyy hh:mm:ss.z', Now ) );
sl.SaveToFile( 'c:\OnlyOnMyPc\test.txt' );
finally
sl.Clear;
FreeAndNil( sl );
end;
Timer1.Enabled := True;
end;
end.
感谢您的时间和帮助。
干杯。英镑
发布于 2014-11-18 17:12:35
在寻找代码中的漏洞之前,您首先需要了解您是否真的有漏洞。任务管理器显示了Windows对应用程序的了解,它的数量需要在Windows如何管理内存的上下文中得到理解-然后您必须了解Delphi如何在应用程序中管理内存。
为了学习如何阅读任务经理的数字,我推荐你优秀的Mark Russinovich的博客和视频:
http://channel9.msdn.com/Events/TechEd/NorthAmerica/2011/WCL405
他将向您解释Windows中的内存分配是如何工作的(Delphi内存管理器将其子分配,但它仍然需要使用Windows的内存分配调用),还将解释他的许多工具,这些工具可以更深入地了解Windows应用程序的许多内部工作原理--超出FastMM所能告诉您的范围。
还有一些称为“分析器”的工具,可以跟踪内存分配和释放,并告诉您哪些代码分配了内存,后来没有释放它。
否则你可能会追逐那些实际上并不存在的漏洞...
https://stackoverflow.com/questions/26986776
复制相似问题