Skip to content

Commit c3aa68b

Browse files
committed
fixed potential memory leak due to race condition
1 parent 846c2b1 commit c3aa68b

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

Source/Base/Spring.Events.Base.pas

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ TEventBase = class(TRefCountedObject)
6161
procedure SetOnChanged(const value: TNotifyEvent);
6262
procedure SetUseFreeNotification(const value: Boolean);
6363
{$ENDREGION}
64-
procedure EnsureNotificationHandler; inline;
64+
procedure EnsureNotificationHandler;
6565
procedure HandleNotification(component: TComponent; operation: TOperation);
6666
protected
6767
fInvoke: TMethodPointer;
@@ -166,11 +166,15 @@ destructor TEventBase.Destroy;
166166
end;
167167

168168
procedure TEventBase.EnsureNotificationHandler;
169+
var
170+
notificationHandler: TNotificationHandler;
169171
begin
170172
if fNotificationHandler = nil then
171173
begin
172-
fNotificationHandler := TNotificationHandler.Create(nil);
173-
fNotificationHandler.OnNotification := HandleNotification;
174+
notificationHandler := TNotificationHandler.Create(nil);
175+
notificationHandler.OnNotification := HandleNotification;;
176+
if AtomicCmpExchange(Pointer(fNotificationHandler), Pointer(notificationHandler), nil) <> nil then
177+
notificationHandler.Free;
174178
end;
175179
end;
176180

0 commit comments

Comments
 (0)