Skip to content

Commit

Permalink
Tweak TSimbaObjectTracker
Browse files Browse the repository at this point in the history
  • Loading branch information
ollydev committed Sep 27, 2023
1 parent dd52156 commit 71bdfed
Showing 1 changed file with 29 additions and 20 deletions.
49 changes: 29 additions & 20 deletions Source/simba.baseclass.pas
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ TSimbaBaseClass = class
TSimbaObjectTracker = class
protected
FList: TList;
FDestroying: Boolean;
public
procedure Add(Obj: TSimbaBaseClass);
procedure Remove(Obj: TSimbaBaseClass);
Expand All @@ -54,9 +55,9 @@ implementation
procedure TSimbaBaseClass.NotifyUnfreed;
begin
if (Name <> '') then
SimbaDebugLn([EDebugLn.YELLOW], '%s (%s) "%s"'.Format([ClassName, HexStr(Self), Name]))
SimbaDebugLn([EDebugLn.YELLOW], ' %s (%s) "%s"'.Format([ClassName, HexStr(Self), Name]))
else
SimbaDebugLn([EDebugLn.YELLOW], '%s (%s)'.Format([ClassName, HexStr(Self)]));
SimbaDebugLn([EDebugLn.YELLOW], ' %s (%s)'.Format([ClassName, HexStr(Self)]));
end;

function TSimbaBaseClass.GetName: String;
Expand Down Expand Up @@ -97,7 +98,12 @@ procedure TSimbaObjectTracker.Add(Obj: TSimbaBaseClass);

procedure TSimbaObjectTracker.Remove(Obj: TSimbaBaseClass);
begin
FList.Remove(Obj);
if FDestroying then
begin
if (FList.IndexOf(Obj) > -1) then
FList[FList.IndexOf(Obj)] := nil;
end else
FList.Remove(Obj);
end;

constructor TSimbaObjectTracker.Create;
Expand All @@ -109,29 +115,33 @@ constructor TSimbaObjectTracker.Create;

destructor TSimbaObjectTracker.Destroy;
var
I, UnfreedCount: Integer;
I: Integer;
HasUnfreed: Boolean;
begin
FDestroying := True;

if (FList <> nil) then
begin
if (FList.Count > 0) then
begin
UnfreedCount := 0;
for I := 0 to FList.Count - 1 do
if not TSimbaBaseClass(FList[I]).FreeOnTerminate then
Inc(UnfreedCount);

if (UnfreedCount > 0) then
begin
SimbaDebugLn([EDebugLn.YELLOW], 'The following %d objects were not freed:'.Format([UnfreedCount]));
for I := FList.Count - 1 downto 0 do
if (FList[I] <> nil) and TSimbaBaseClass(FList[I]).FreeOnTerminate then
TSimbaBaseClass(FList[I]).Free();

for I := 0 to FList.Count - 1 do
with TSimbaBaseClass(FList[I]) do
HasUnfreed := False;
for I := 0 to FList.Count - 1 do
if (FList[I] <> nil) then
begin
if not HasUnfreed then
begin
if not FreeOnTerminate then
NotifyUnfreed();
Free();
SimbaDebugLn([EDebugLn.YELLOW], 'The following objects were not freed:');

HasUnfreed := True;
end;
end;

TSimbaBaseClass(FList[I]).NotifyUnfreed();
TSimbaBaseClass(FList[I]).Free();
end;
end;

FreeAndNil(FList);
Expand All @@ -144,8 +154,7 @@ initialization
SimbaObjectTracker := TSimbaObjectTracker.Create();

finalization
if Assigned(SimbaObjectTracker) then
FreeAndNil(SimbaObjectTracker);
SimbaObjectTracker.Free();

end.

0 comments on commit 71bdfed

Please sign in to comment.