A
andin
Unregistered / Unconfirmed
GUEST, unregistred user!
我写了一个简单的测试程序,用这个程序就可以暴露 Delphi 的Bug:
程序定义了两个单元,一个主界面,放两个按钮,另外一个是线程单元;
主界面单元的程序如下:
unit UnitMain;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,UnitTest;
type
TfrmMain = class(TForm)
btnStart: TButton;
Button1: TButton;
procedure btnStartClick(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
frmMain: TfrmMain;
test : TTest;
implementation
{$R *.dfm}
procedure TfrmMain.btnStartClick(Sender: TObject);
begin
if test = nil then
test := TTest.Create(False);
end;
procedure TfrmMain.Button1Click(Sender: TObject);
begin
FreeAndNil(test);
end;
end.
线程单元的程序如下:
unit UnitTest;
interface
uses
Classes,Graphics,SysUtils;
type
TTest = class(TThread)
private
FCurrentBitmap : TBitmap;
protected
procedure Execute; override;
public
constructor Create(CreateSuspended: Boolean);
destructor Destroy;override;
end;
implementation
constructor TTest.Create(CreateSuspended: Boolean);
begin
FCurrentBitmap := TBitmap.Create;
FCurrentBitmap.LoadFromFile('c:/test.bmp');
Inherited;
end;
destructor TTest.Destroy;
begin
inherited;
FreeAndNil(FCurrentBitmap);
end;
procedure TTest.Execute;
var
number : integer;
begin
number := 0;
while not Terminated do
begin
FCurrentBitmap.Canvas.TextOut(0,0,IntToStr(number));
FCurrentBitmap.SaveToFile('c:/ret.bmp');
Inc(number);
sleep(10);
end;
end;
end.
测试办法:
1. 用Delphi5 或 Delphi6 编译都可以;
2. 在C盘根目录下放一个 test.bmp 文件;
3. 单击“start Test”按钮,工作线程开始不断反复产生 c:/ret.bmp 文件;
Delphi Bug 的表现:
1.以上程序运行一段时间后,就报告奇怪的异常:“当文件存在时,不能创建该文件”
2.如果创建 ret.bmp 文件之前,调用 DeleteFile 函数删除这个文件,运行一段时间后,就会报告奇怪的异常:“Out of system resource”
3.大家测试时,一定要有耐心,这个一段时间,一般是3分钟左右,也可能得等1个小时!
程序定义了两个单元,一个主界面,放两个按钮,另外一个是线程单元;
主界面单元的程序如下:
unit UnitMain;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,UnitTest;
type
TfrmMain = class(TForm)
btnStart: TButton;
Button1: TButton;
procedure btnStartClick(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
frmMain: TfrmMain;
test : TTest;
implementation
{$R *.dfm}
procedure TfrmMain.btnStartClick(Sender: TObject);
begin
if test = nil then
test := TTest.Create(False);
end;
procedure TfrmMain.Button1Click(Sender: TObject);
begin
FreeAndNil(test);
end;
end.
线程单元的程序如下:
unit UnitTest;
interface
uses
Classes,Graphics,SysUtils;
type
TTest = class(TThread)
private
FCurrentBitmap : TBitmap;
protected
procedure Execute; override;
public
constructor Create(CreateSuspended: Boolean);
destructor Destroy;override;
end;
implementation
constructor TTest.Create(CreateSuspended: Boolean);
begin
FCurrentBitmap := TBitmap.Create;
FCurrentBitmap.LoadFromFile('c:/test.bmp');
Inherited;
end;
destructor TTest.Destroy;
begin
inherited;
FreeAndNil(FCurrentBitmap);
end;
procedure TTest.Execute;
var
number : integer;
begin
number := 0;
while not Terminated do
begin
FCurrentBitmap.Canvas.TextOut(0,0,IntToStr(number));
FCurrentBitmap.SaveToFile('c:/ret.bmp');
Inc(number);
sleep(10);
end;
end;
end.
测试办法:
1. 用Delphi5 或 Delphi6 编译都可以;
2. 在C盘根目录下放一个 test.bmp 文件;
3. 单击“start Test”按钮,工作线程开始不断反复产生 c:/ret.bmp 文件;
Delphi Bug 的表现:
1.以上程序运行一段时间后,就报告奇怪的异常:“当文件存在时,不能创建该文件”
2.如果创建 ret.bmp 文件之前,调用 DeleteFile 函数删除这个文件,运行一段时间后,就会报告奇怪的异常:“Out of system resource”
3.大家测试时,一定要有耐心,这个一段时间,一般是3分钟左右,也可能得等1个小时!