我有以下的Delphi代码时:访问冲突释放TObjectList
destructor TXX_XXXX.Destroy;
var
i: Integer;
begin
if Assigned(Allocations) then
begin
for i:=0 to (Allocations.Count - 1) do
begin
try
TXX_ALOC(Allocations.Items[i]).Free;
except on Ex:Exception do
begin
OutputDebugString(PChar('Exception Error Message '+ Ex.Message));
end;
end;
end;
// Above code works well - no exception
try
FreeAndNil(Allocations); {Exception Here}
except on E:Exception do
begin
OutputDebugString(PChar('Exception in xxxxxxxxx.pas'+E.Message));
end;
end;
end;
inherited;
end;
访问冲突在模块地址4003AB4 'Vcl50.bpl'。读地址2980BFFC
我知道通常
- 自由一些对象导致访问冲突之前
- 使用一些对象,而无需初始化
但在这里我之前已经被释放做免费的,我检查Allocations分配。如果我放弃异常处理,我的应用程序会抛出一些错误的错误。 Allocations是一个TObjectList,如果它是一个数组 - 我会怀疑我没有给该数组分配一个长度,但它是一个TObjectList。
非常感谢!
“但在这里之前,我做了免费的,我检查分配分配” ......注意,如果你不仅调用'Free'也设置了这不仅有助于明确引用“nil”(或更好地使用FreeAndNil)。 'Free'不会将引用设置为'nil',这就是'Assigned'检查的内容! – jpfollenius