4

I have a web app written in Delphi 12 using TMS Webcore components.

At logout (which happens by user action, or after idle timeout), I want to clear the memory

  • for security reasons,
  • in case there are undiscovered memory leaks,
  • to allow the browser to download a new version if it exists.

I keep getting an exception (shown below) on restarting the application.

Question

How can I:

  • Either stop it form occurring (ideal)?
  • Or capture it silently and attempt another restart?

I have tried a few options, but I keep getting an exception in the constructor of the main form. This indicates that the memory from the previous session has not been cleared.

Code for Log_Out:

procedure Tfrm_Ops_Main.Log_Out (AAuto : Boolean = False);
begin
  TConsole.Log (msg_Logging_Out);
  if AAuto
  then Stop_Warning_User_At_Tab_Close;
  Free_Child_Form;
  Close;
  { Allowing for Form to Close }
//  THtml.Replace_Page;
  THtml.Reload_After_Delay (400);
end;

I have tried the three methods

  • Replace_Page
  • Reload_Page
  • Reload_Page_After_Delay

THtml class code:

class procedure THtml.Replace_Page  (AUrl : string = '');
var
  lUrl  : string;
  lTime : Int64;
begin
  { Add time in ms if same URL }
  if AUrl = ''
  then begin
       LTime := Trunc ((Now - UnixEpoch) * ToMS);
       LUrl := window.location.href + '?t=' + IntToStr (LTime);
  end
  else LUrl := AUrl;
  document.location.replace (LUrl);
end;

class procedure THtml.Reload_Page;
begin
  document.location.reload (True);
end;

class procedure THtml.Reload_After_Delay (ADelayMS : Integer);
var
  LTimer : TWebTimer;
begin
  LTimer          := TWebTimer.Create(nil);
  LTimer.Interval := ADelayMS;
  LTimer.Enabled  := True;
  LTimer.OnTimer  := Do_Reload;
end;

class procedure THtml.Do_Reload (Sender : Tobject);
begin
  TWebTimer (Sender).Free;
  THtml.Reload_Page;
end;

The exception fragment is:

Error: Duplicate component name: "divHeading"   
at Object.Create$1 (https://example.com.au/ops-test/ops_1_0_2109.js:3634:23)   
at Object.CreateFmt (https://example.com.au/ops-test/ops_1_0_2109.js:3641:12)   
at c.$create (https://example.com.au/ops-test/ops_1_0_2109.js:366:19)   
at Object.ValidateRename (https://example.com.au/ops-test/ops_1_0_2109.js:15533:188)   
at Object.SetName (https://example.com.au/ops-test/ops_1_0_2109.js:15513:21)   
at Object.SetName (https://example.com.au/ops-test/ops_1_0_2109.js:33680:38)   
at Object.LoadDFMValues (https://example.com.au/ops-test/ops_1_0_2109.js:127837:25)   
at Object.CreateNewForm (https://example.com.au/ops-test/ops_1_0_2109.js:43375:13)   
at Object.DoFormLoad (https://example.com.au/ops-test/ops_1_0_2109.js:43010:12)   
at XMLHttpRequest.cb (https://example.com.au/ops-test/ops_1_0_2109.js:282:28)

2 Answers 2

0

I experimented with the delay. At 1sec, I couldn't see the issue, but that was way too long. At 400ms, it happens once out of 8 times. So, I have accepted that.

  1. I execute Reload_After_Delay (with 400ms)
  2. I managed to trap the exception successfully, where I check if it is "duplicate component" and I execute Repalce_Page which using a param, forces the browser to reload.

You can see the exception flash for a sub second, before it fixes itself.

Sign up to request clarification or add additional context in comments.

Comments

0

User state really shouldn't be embedded in the webform, it should be its own object. The webform should just reference the object when it renders. If the object is not valid, then navigate to login screen. Clearing the user data should also be simpler and can be localized to the user data object. If you timeout the user, dump the user data object and navigate back to the login screen. Disable the back button navigation and tell the page not to cache to force it to re-download and re-render each time.

Use the principle of least privilege (PoLP) when you lay out your user data object. Only store data that the user needs to do their tasks and don't store data that they don't need. For example, don't store the password in the user object from the login screen once they have been validated.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.