2016-12-25 7 views
-3

リーク:デルファイTPngImageメモリは、私はこのようになりレコード有し

TCell = record 
    Marked:  Boolean; 
    ToBeMarked: Boolean; 
    Image:  TPngImage; 
    end; 

var Cells: array of array of TCell 

細胞[n]を.Imageは、特定の手順の間に作成され、後の使用のために記憶されます。プロシージャが呼び出されるたびに、この配列はクリアされます。 しかし、私はまだプログラムを閉じると、メモリリークレポートがあります。

unit Unit1; 

interface 

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

type 
TCell = record 
    Marked:  Boolean; 
    ToBeMarked: Boolean; 
    Image:  TPngImage; 
    end; 
    TForm1 = class(TForm) 
    Button1: TButton; 
    procedure Button1Click(Sender: TObject); 
    procedure TestProcedure1; 
    procedure TestProcedure2(X,Y: Integer); 
    private 
    { Private declarations } 
    public 
    { Public declarations } 
    end; 

var 
    Form1: TForm1; 
    Cells: array of array of TCell; 
    RI_LengthX: Integer; 
    RI_LengthY: Integer; 

implementation 

{$R *.dfm} 

procedure TForm1.Button1Click(Sender: TObject); 
begin 
TestProcedure1; 
end; 

procedure TForm1.TestProcedure1; 
var X,Y: Integer; 
begin 
{ Clearing the array before the new cycle } 
for X:=0 to High(Cells) do for Y:=0 to High(Cells[X]) do  Cells[X,Y].Image.Free; 
SetLength(Cells,0); 
{ Creating new array } 
RI_LengthX:=10; 
RI_LengthY:=10; 
SetLength(Cells,RI_LengthX); 
for X:=0 to High(Cells) do SetLength(Cells[X],RI_LengthY); 
{ Calling the procedure that creates image for every cell } 
for X:=0 to RI_LengthX-1 do for Y:=0 to RI_LengthY-1 do  TestProcedure2(X,Y); 
end; 

procedure TForm1.TestProcedure2(X,Y: Integer); 
var BaseBMP: TBitmap; 
begin 
{ Dynamic creation of an image } 
BaseBMP:=TBitmap.Create; 
BaseBMP.Width:=25; 
BaseBMP.Height:=25; 
{ Saving image inside a record } 
Cells[X,Y].Image:=TPngImage.Create; // Commenting these lines 
Cells[X,Y].Image.Assign(BaseBMP); // prevents the leak 
BaseBMP.Free; 
end; 

initialization 
ReportMemoryLeaksOnShutdown:=True; 

end.` 

enter image description here

このリークを回避する方法はありますか?使用前に

+2

を行う必要があります[MCVE] –

+0

がLoghmanのアドバイスに従って、決済手続きを変更し、必要なコードの最低限を提供示してください。リークはまだ発生します。 –

+0

*「必要最低限​​のコード」*の代わりに[mcve]を指定してください。 –

答えて

7

SetLength(Cells,0);次のコード

for i := 0 to High(Cells) do 
    Cells[i].Image.Free; 
関連する問題