1

下のオブジェクトがどこにあり、それらをクリアする方法がわかりません。

例えば:

public

Alist: TStringlist;

..
procedure TForm1.FormCreate(Sender: TObject);
begin
Alist:=Tstringlist.Create;
end;

procedure TForm1. addinstringlist;
var
i: integer;
begin

for i:=0 to 100000 do 
   begin
   Alist.add(inttostr(i), pointer(i));
   end;
end;

procedure TForm1.clearlist;
begin
Alist.clear;

// inttostr(i) are cleared, right? 

// Where are pointer(i)? Are they also cleared ?
// if they are not cleared, how to clear ?

end;



  procedure TForm1. repeat;   //newly added
   var
   i: integer;
   begin
   For i:=0 to 10000 do
       begin
       addinstringlist;
       clearlist;
       end;
   end;   // No problem?

私は Delphi 7 を使用しています。 Delphi 7.0 のヘルプ ファイルには、次のように書かれています。

AddObject method (TStringList)

Description
Call AddObject to add a string and its associated object to the list. 
AddObject returns the index of the new string and object.
Note:   
The TStringList object does not own the objects you add this way. 
Objects added to the TStringList object still exist 
even if the TStringList instance is destroyed. 
They must be explicitly destroyed by the application.

私のプロシージャ Alist.add(inttostr(i), pointer(i)) では、オブジェクトを作成しませんでした。オブジェクトはありましたか?inttostr(i) と pointer(i) の両方をクリアするにはどうすればよいですか。

前もって感謝します

4

1 に答える 1

6

Pointer(I)ポインタはオブジェクトを参照しないため、クリアする必要はありません。ポインタとして格納されている整数です。

アドバイス:コードがリークするかどうかわからない場合、または簡単なテストを作成して使用しない場合

ReportMemoryLeaksOnShutDown:= True;

コードがリークした場合は、テストアプリケーションを閉じたというレポートが表示されます。


追加したコードはリークしません。チェックしたい場合は、次のようなテストを作成してください。

program Project2;

{$APPTYPE CONSOLE}

uses
  SysUtils, Classes;

var
  List: TStringlist;

procedure addinstringlist;
var
  i: integer;
begin

for i:=0 to 100 do
   begin
   List.addObject(inttostr(i), pointer(i));
   end;
end;

procedure clearlist;
begin
   List.clear;
end;

procedure repeatlist;
var
   i: integer;

   begin
   For i:=0 to 100 do
       begin
       addinstringlist;
       clearlist;
       end;
   end;


begin
  ReportMemoryLeaksOnShutDown:= True;
  try
    List:=TStringList.Create;
    repeatlist;
    List.Free;
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.

行にコメントを付けList.Freeてメモリリークを作成し、何が起こるかを確認してください。

于 2013-02-26T10:25:57.237 に答える