删除 TList 中的 TList
我试图在 onDestroy 事件中释放 Tlist 中的 Tlist,而 FastMM4 引发访问冲突错误。这是代码片段。
procedure TSignalFrm.FormDestroy(Sender: TObject);
var
x,y: integer;
begin
for x := 0 to signalList.Count - 1 do
begin
for y:=0 to TSignal(SignalList.Items[x]).alarmList.Count-1 do
begin
TAlarm(TSignal(SignalList.Items[x]).alarmList.Items[y]).Free;
end;
TSignal(SignalList.Items[x]).AlarmList.Free;
TSignal(SignalList.Items[x]).Free;
end;
SignalList.Free;
end;
我在 TSignal(SignalList.items[x]).Free; 处收到访问冲突错误线。在释放 SignalList 项之前释放 AlarmList 项会引发访问冲突错误,但为什么呢?
更新:我在 Windows XP 上使用 Delphi 7.0。实际的 FastMM4 消息如下。
FastMM 检测到对已释放对象调用虚拟方法的尝试。现在将引发访问冲突以中止当前操作。
释放的对象类:TList
虚拟方法:Destroy
虚拟方法地址:427CF0
分配号为:80055
后面是大量的内存转储。
根据此 FastMM4 错误,如果释放另一个对象中的对象,也会自动释放所有者。我知道这不可能是真的,但如果我错了,请纠正我。
I am trying to free Tlist within a Tlist in a onDestroy event and FastMM4 is raising an access violation error. Here is the code snippet.
procedure TSignalFrm.FormDestroy(Sender: TObject);
var
x,y: integer;
begin
for x := 0 to signalList.Count - 1 do
begin
for y:=0 to TSignal(SignalList.Items[x]).alarmList.Count-1 do
begin
TAlarm(TSignal(SignalList.Items[x]).alarmList.Items[y]).Free;
end;
TSignal(SignalList.Items[x]).AlarmList.Free;
TSignal(SignalList.Items[x]).Free;
end;
SignalList.Free;
end;
I get access violation error at TSignal(SignalList.items[x]).Free; line. Freeing AlarmList items before freeing SignalList items raises the access violation error, but WHY?
Update: I am using Delphi 7.0 on Windows XP. The actual FastMM4 messages is as follows.
FastMM has detected an attempt to call a virtual method on a freed object. An access viloation will now be raised in order to abort the current operation.
Freed Object class: TList
Virtual method: Destroy
Virtual method address:427CF0
The allocation number was: 80055
Followed by a lots of memory dump.
According to this FastMM4 error, if you free an object within an another object, you automatically free the owner as well. I know that can't be true, but correct me if I am wrong.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
TSignal
是否未在其析构函数中释放其AlarmList
成员? (我就是这样做的)。更新:如果删除
TSignal(SignalList.Items[x]).AlarmList.Free;
行,它会起作用吗?第二次更新:
如果每个
TList
的项目包含指向对象的指针,则需要释放它。您的问题是
TSignal
不是TList
。由于它负责释放其成员(例如 Alarmlist),因此不应显式释放该 Alarmlist。Does
TSignal
not free itsAlarmList
member in its destructor? (That’s how I would do this).Update: does it work if you remove the
TSignal(SignalList.Items[x]).AlarmList.Free;
line?Second update:
Each
TList
's items need to be freed, if it contains pointers to objects.Your problem was that
TSignal
is not aTList
. Since it takes care of freeing its members (such as the Alarmlist), that Alarmlist should not be freed explicitly.由于 TAlam 和 TSignal 都是对象(而不是记录),我相信您应该使用 TObjectList 而不是 TList。 TObjectList 有一个称为 OwnsObjects 的特殊属性,允许它在释放其内容时正确释放它。看看http://docwiki.embarcadero.com/VCL/XE/ en/Contnrs.TObjectList.OwnsObjects
建议不要使用 TList,除非您需要存储指针而不是对象。
Since TAlam and TSignal are both objects (not records) I believe you should use TObjectList instead of TList. TObjectList has a special property calld OwnsObjects that allows it to free it's contents properly as it is being freed. Check this out http://docwiki.embarcadero.com/VCL/XE/en/Contnrs.TObjectList.OwnsObjects
As an advice do not use TList unless you need to store pointers not objects.