可能TStringList不适合做全局变量。
我做了个最简单的测试,编译OK,运行时执行到creat出错,内存冲突的错误。
代码如下:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Unit2;
type
TForm1 = class(TForm)
Memo1: TMemo;
Button1: TButton;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
procedure MyTest(var xyz:TStringList);
end;
var
Form1: TForm1;
gtstest: TStringList;
implementation
{$R *.dfm}
{ TForm1 }
procedure TForm1.MyTest(var xyz: TStringList);
var
i:Integer;
s:string;
begin
for i:= 0 to xyz.Count -1 do
begin
s:=xyz.Strings;
memo1.Lines.Add(s);
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
aChild:TForm2;
begin
aChild := TForm2.Create(Self);
aChild.ShowModal;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
ShowMessage('before');
gtstest.Create;
ShowMessage('after');
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
gtstest.Free;
end;
end.
=============
unit Unit2;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm2 = class(TForm)
Memo1: TMemo;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form2: TForm2;
implementation
uses Unit1;
{$R *.dfm}
procedure TForm2.Button1Click(Sender: TObject);
var
lst:TStringList;
i:Integer;
begin
lst:=TStringList.Create;
try
for i:=1 to 10 do
lst.Add('a'+IntToStr(i));
Form1.MyTest(lst);
finally
lst.Free;
end;
end;
end.