Possible Memory leak in TROCustomSessionManager

TROCustomSessionManager has a method that is not threadsafe, with might lead to memory leaks.

Here’s the code that shows the memory-leak:

procedure TTestForm.Button1Click(Sender: TObject);
var
  SessionManager: TROCustomSessionManager;
begin
  ReportMemoryLeaksOnShutdown := DebugHook <> 0;
  SessionManager := TROInMemorySessionManager.Create(nil);
  try
    TTask.Run(
      procedure
      begin
        SessionManager.ClearSessions(True);
      end);
    TTask.Run(
      procedure
      begin
        SessionManager.ClearSessions(True);
      end);
    Sleep(1000);
  finally
    SessionManager.Free;
  end;

It can easily be solved by changing the CreateTimerByRequest procedure in uROSessions as follows:

procedure TROCustomSessionManager.CreateTimerByRequest;
begin
  if not ((csDesigning in ComponentState) or (csDestroying in ComponentState))
    and not Assigned(fTimer) and (fSessionCheckInterval > 0) then
//    fTimer := TROThreadTimer.Create(DoTimerTick, fSessionCheckInterval*60000); //original version not threadsafe, below between begin-end we use the already existing(!) fCritical.
  begin
    fCritical.Enter;
    try
      if not Assigned(fTimer) then
        fTimer := TROThreadTimer.Create(DoTimerTick, fSessionCheckInterval*60000);
    finally
      fCritical.Leave;
    end;
  end;
end;

This applies to several old versions of the SDK, but also to 9.3.105.1351. Would be great if in future versions the above solution is implemented.

1 Like

Thanks, logged as bugs://79284

bugs://79284 got closed with status fixed.