Application.Terminate 产生访问冲突错误 Delphi 10.3
一旦创建了 TImage
动态数组,Application.Terminate()
过程就会导致访问冲突。
数组(可变长度):
arrImages: array of TImage;
访问冲突:
Access violation at address 00405D39. Write of address 004680EC
由于这种情况仅在创建动态数组后发生,因此我尝试释放数组中的所有元素(onTmrImage
是一个全局变量,包含数组中的第一个元素数组,数组中的元素都是 GUI 上的动态 TImage
组件):
procedure TfrmTank.procPurgeArray;
var
I: integer;
begin
FreeAndNil(onTmrLImage);
for I := 1 to length(arrImages) do
begin
FreeAndNil(arrImages[I]);
end;
FreeAndNil(arrImages);
end;
然后在 onClick
事件中使用 Application.Terminate()
退出按钮:
procedure TfrmTank.btnExitClick(Sender: TObject);
begin
procPurgeArray;
Application.Terminate;
end;
知道为什么吗是否有必要做任何事情来简单地退出应用程序?
The Application.Terminate()
procedure causes an Access Violation once a dynamic array of TImage
has been created.
Array (variable length):
arrImages: array of TImage;
Access violation:
Access violation at address 00405D39. Write of address 004680EC
Since this only occurs after the dynamic array has been created, I have tried to free all elements in the array (onTmrImage
is a global variable containing the first element in the array, the elements in the array are each dynamic TImage
components on the GUI):
procedure TfrmTank.procPurgeArray;
var
I: integer;
begin
FreeAndNil(onTmrLImage);
for I := 1 to length(arrImages) do
begin
FreeAndNil(arrImages[I]);
end;
FreeAndNil(arrImages);
end;
And then used Application.Terminate()
in the onClick
event of the exit button:
procedure TfrmTank.btnExitClick(Sender: TObject);
begin
procPurgeArray;
Application.Terminate;
end;
Any idea as to why it is necessary to even do anything to simply exit the application?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一个错误。您不得对动态数组调用
Free
。您可以对对象实例调用Free
。将此行代码替换为arrImages := nil
。动态数组会自动销毁,因为它们是托管类型。
销毁图像时,您还会得到错误的数组索引。动态数组是从零开始的。您的循环应该是:
也许创建数组的代码中也存在同样的错误。
综上所述,这段代码肯定属于析构函数。有人甚至可能会说,当您即将执行
Application.Terminate
时,这有点毫无意义。This is a bug. You must not call
Free
on a dynamic array. You can callFree
on an object instance. Replace this line of code witharrImages := nil
.Dynamic arrays are destroyed automatically because they are managed types.
You also get the array indices wrong when destroying the images. Dynamic arrays are zero-based. Your loop should be:
Perhaps the same mistake is present in the code that creates the array.
With all this said, surely this code belongs in a destructor. And one could even argue that it's a little pointless when you are about to do
Application.Terminate
.