【发布时间】:2017-06-25 17:04:08
【问题描述】:
以下代码可以正常工作,但不应该!当我单击 Button1 时,首先销毁该对象,然后使用它的值,并且我没有收到任何访问冲突或其他东西......更重要的是,乘法运算给出了正确的结果,证明 Obj1 是不破坏!但是话又说回来,这也不是真的,因为当我关闭程序时,它不会报告任何内存泄漏。我很困惑。
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
MyObj = class(TObject)
Value: Cardinal;
end;
TForm1 = class(TForm)
Button1: TButton;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
public
Obj1:MyObj;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
Obj1.Free;
Obj1.Value:=Obj1.Value * 5;
Caption:=IntToStr(Obj1.Value);
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
ReportMemoryLeaksOnShutdown:=true;
Obj1:=MyObj.Create;
Obj1.Value:=10;
end;
end.
【问题讨论】:
-
因为内存还有效;那时您只是在写入内存位置。那时你可能会在其他物体上乱涂乱画。
标签: delphi runtime-error delphi-2009