Reputation: 3328
I have the following Delphi code:
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;
Access violation at address 4003AB4 in module 'Vcl50.bpl'. Read of address 2980BFFC
I know the access violation usually caused by
But here before I do the free, I checked Allocations is assigned. If I discard the exception handling, my application will throw a something is wrong error. Allocations is a TObjectList, if it is an array - I will doubt I didn't assign a length to the array, but it is a TObjectList.
Thanks a lot!
Upvotes: 4
Views: 7552
Reputation: 22749
Generally, when clearing lists you want to loop from end to start ie
for i := (Allocations.Count - 1) downto 0 do begin
Delete(Allocations.Items[i]);
end
but in case of TObjectList, if the list owns objects (which it does by default) you shouldn't free them before destroying the container as the list will do it for you. In the code above, if the list owns the objects then calling Delete also frees the object.
Upvotes: 7
Reputation: 502
There are 2 options for your ...
1) Create the TObjectList wiht the aOwnsObjects set to true if you want Delphi to automatically free the objects when they get removed from the ObjectList. And in your Destructor simple FreeAndNil the ObjectList itself. This will then remove all objects from the list and free them automatically. Since freeing the ObjectList will automatically free the objects contained in that list, Your code could then look like :
destructor TXX_XXXX.Destroy;
begin
FreeAndNil( Allocations ); //
inherited;
end;
2) Create the TObjectList wiht the aOwnsObjects set to False, in which case you will have to take care of freeing the objects in the list yourself. Your code could then look something like this :
destructor TXX_XXXX.Destroy;
var
i : Integer;
oObject : TObject;
begin
if Assigned(Allocations) then
begin
for i:= Pred( Allocations.Count ) downto 0 do
begin
// Get a reference to the Object
oObject := TXX_ALOC(Allocations.Items[i]);
// Remove the object from the object list
Allocations.Delete( i );
// Free the Object
FreeAndNil( oObject );
end;
end;
FreeAndNil( Allocations );
inherited;
end;
Upvotes: 3
Reputation: 10372
A TObjectList
usually takes care of destroying its content. Don't free your objects in this case. This will lead to an access violation when freeing the TObjectList
because it tries to free the contained objects again.
This behavior of the object list can be controlled in its constructor:
constructor TObjectList.Create(AOwnsObjects: Boolean);
Use this one to specify if you want the list to own its content (means: it takes care of destroying an item when it is removed from the list or the list is destroyed) or not. The constructor without parameter (which you probably used) sets this to true
.
You probably just want a list like TList
but for storing objects. If that's the case then create your list like this:
Allocations:= TObjectList.Create(False);
But if you want the auto-destruction behavior then just remove the for-loop. The object list will destroy your TXX_ALOC
objects.
Upvotes: 18