2017-06-16 163 views
0

看來,設置ReportMemoryLeaksOnShutdown := true不會對用Delphi 10.2東東創建的程序有任何影響(我在Windows和Linux程序中試過)。即使有明顯的內存泄漏,也沒有任何報道。「ReportMemoryLeaksOnShutdown」不能在Delphi 10.2東京工作?

有人可以證實這一點嗎?有沒有其他方法來檢查Linux程序中的內存泄漏?在Windows上,我可以使用madExcept。

------------------編輯2 ------------------

在Delphi 10.2 ReportMemoryLeaksOnShutdown := true似乎只適用於未標記爲控制檯應用程序的程序。一旦我註釋掉行{$APPTYPE CONSOLE}我收到所需的錯誤信息(當我在Windows上運行該程序)。

------------------編輯1 ------------------

這是請求的示例:

program WeakRefTest; 

{$APPTYPE CONSOLE} 

{$R *.res} 

uses 
    SysUtils; 

type 
    TParent = class; 

    TChild = class 
     private 
     {$IFDEF AUTOREFCOUNT} [Weak] {$ENDIF} 
     Parent: TParent; 
     public 
     constructor Create (const Parent: TParent); 
     destructor Destroy; override; 
    end; { TChild } 

    TParent = class 
     private 
     Child : TChild; 
     public 
     constructor Create; 
     destructor Destroy; override; 
    end; { TParent } 

constructor TChild.Create(const Parent: TParent); 
begin 
    inherited Create; 

    WriteLn ('TChild.Create'); 
    Self.Parent := Parent; 
end; 

destructor TChild.Destroy; 
begin 
    WriteLn ('TChild.Destroy'); 
    inherited; 
end; 

constructor TParent.Create; 
begin 
    inherited; 

    WriteLn ('TParent.Create'); 
    Child := TChild.Create (Self); 
end; 

destructor TParent.Destroy; 
begin 
    WriteLn ('TParent.Destroy'); 
    inherited; 
end; 

procedure SubRoutine; 

var 
    Parent : TParent; 

begin 
    Parent := TParent.Create; 
    WriteLn ('"SubRoutine" exit'); 
end; { SubRoutine } 

begin { WeakRefTest } 
    ReportMemoryLeaksOnShutdown := true; 

    try 
     SubRoutine; 
     WriteLn ('"WeakRefTest" done'); 

    except 
     on E: Exception do 
      WriteLn (E.ClassName, ': ', E.Message); 
    end; 
end. 

要強制在Linux上註釋掉在的TChild聲明的[Weak]屬性就行了內存泄漏。當編譯Windows時,會有內存泄漏,因爲ARC不受支持。

當我編譯並採用Delphi XE出現一則消息,有內存泄漏運行代碼: Message displayed when compiled with Delphi XE

當我編譯並採用Delphi 10.2沒事的Windows運行出現了。我在TChild的聲明中註釋掉[Weak]屬性後,使用Linux編譯器時也是如此。

+0

提交(在Windows FMX項目)Embarcadero公司 –

+0

我只是測試一個bug報告,它工作正常。你能舉一個例子嗎? – Dsm

+0

您應該用[mcve] –

回答

4

如果您從cmd窗口運行控制檯應用程序,它將顯示有關內存泄漏的相應消息。 內存泄漏報告的行爲已更改,並且窗口應用程序顯示MessageBox,而控制檯應用程序在控制檯中獲取消息。

在Delphi XE2中,ScanForMemoryLeaks過程中有單個MessageBoxA。 在Delphi 10.2中有自定義過程ShowMessage(AMessage,ATitle:_PansiChr);它調用WriteConsoleFile或MessageBoxA。 所以它的設計,而不是錯誤(恕我直言)。

比較討論:Reporting memory leaks on shutdown with a console application

+0

感謝您澄清這一點。從我的角度來看,這並沒有得到適當的考慮,但現在我知道是什麼導致了這種行爲,我總是可以使用[FastMM4](https://github.com/pleriche/FastMM4)或使用'ExitProcessProc'as描述[這裏](https://stackoverflow.com/questions/39579740/reporting-memory-leaks-on-shutdown-with-a-console-application)。 –