我正在考虑来自 Delphi 的New
andDispose
命令,并且想知道我是否可以在我的进程中的其他过程/函数/线程等中使用这些命令。
我想将地址存储到 aTList
但我有点不安全,因为它使用var
可用于“保存”vars 实际地址的引用。我不想要任何访问冲突或任何东西......这是我的代码:
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
MyTList : TList;
public
{ Public declarations }
end;
var
Form1 : TForm1;
type
TMyStruct = record
Int1 : Integer;
Int2 : Integer;
Str1 : String;
Str2 : String;
end;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
P_TMyStruct : ^TMyStruct;
I : Integer;
begin
for I := 1 to 3 do begin
New (P_TMyStruct);
P_TMyStruct^.Int1 := I;
P_TMyStruct^.Int2 := 1337;
P_TMyStruct^.Str1 := inttostr(I);
P_TMyStruct^.Str2 := '1337';
MyTList.Add(P_TMyStruct);
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
var
I : Integer;
begin
for I := 0 to MyTList.Count - 1 do begin
ShowMessage(inttostr(TMyStruct(MyTList.Items[i]^).Int1));
ShowMessage(inttostr(TMyStruct(MyTList.Items[i]^).Int1));
ShowMessage(TMyStruct(MyTList.Items[i]^).Str1);
ShowMessage(TMyStruct(MyTList.Items[i]^).Str2);
Dispose(MyTList.Items[i]);
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
MyTList := TList.Create;
end;
end.
既然我没有将它放在堆栈中,那会安全吗?