首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > .NET >

DelphiX无缘无故引起的Access Violation有关问题

2012-02-19 
DelphiX无缘无故引起的Access Violation问题!新建unit2,贴上下面代码:unit Unit2interfaceusesForms,DXDr

DelphiX无缘无故引起的Access Violation问题!
新建unit2,贴上下面代码:

unit Unit2;
interface
uses
  Forms,DXDraws;

type TDX=class
 DirectX:TDXDraw;
 constructor Create(Owner:TForm);
 destructor Free();
end;

implementation
constructor TDX.Create(Owner:TForm);
begin
 if DirectX=nil then begin
  DirectX:=TDXDraw.Create(Owner);
  DirectX.Parent:=Owner;
  DirectX.Initialize();
  DirectX.Visible:=true;
 end;
end;

destructor TDX.Free();
begin
 if DirectX<>nil then begin
  DirectX.Free();DirectX:=nil;
 end;
end;
end.


在主unit里加入下面代码(记得在uses里添加DXDraws,unit2):
procedure TForm1.Button1Click(Sender: TObject);
var i:Integer;dx:array [0..1] of TDX;
begin
 for i:=1 to 2 do begin //第2次发生错误
  dx[0]:=TDX.Create(Self);
  dx[1]:=TDX.Create(Self);
  dx[0].Free();dx[0]:=nil;
  dx[1].Free();dx[1]:=nil;
 end;
end;

它有2次循环,第1次没发生错误,第2次循环时发生错误。我试过达到这样的条件时才会有错:1.必须是以类的形式把DelphiX包含在类里,2.必须是数组,例如dx,如果是单个则不会发生错误.

指针释放好像没问题,到底是什么问题?谢谢!

[解决办法]
dx[0]:=TDX.Create(Self); 
dx[0].Free();dx[0]:=nil; 

这样试一下
dx[1]:=TDX.Create(Self); 
dx[1].Free();dx[1]:=nil; 

然后加断点 看下dx[0] 和dx[1]的地址 是否一样 一句一句加断点能调试出来的。

下载一个ExceptionLog 这个能帮助你调试错误,找到出错的代码

热点排行