Debugging error on using TAdvWebbrowser TCoreWebView2HistoryChangedEventHandler

Hi, when using an TAdvWebbrowser in combination with TCoreWebView2HistoryChangedEventHandler the Delphi debugger gives us a Critical error message.

It seems to be related to the ExecuteJavascript function we have in our code:

procedure TFormITREPOS_Protserver_Inloggen.FormCreate(Sender: TObject);
var
w: ICoreWebView2;
c: ICoreWebView2Controller;
e: EventRegistrationToken;
begin
c := ICoreWebView2Controller(AdvWebBrowserLogin.NativeBrowser);
if c.get_CoreWebView2(w) = S_OK then
begin
w.add_HistoryChanged(TCoreWebView2HistoryChangedEventHandler.Create, @e);
end;
end;

{ TCoreWebView2HistoryChangedEventHandler }

function TCoreWebView2HistoryChangedEventHandler.Invoke(sender: ICoreWebView2;
args: IInterface): HRESULT;
var
s: string;
cText: string;
begin
Result := S_OK;
if sender.get_Source(@s) = S_OK then
begin
cText := PChar(s);
FormITREPOS_Protserver_Inloggen.AdvWebBrowserLogin.ExecuteJavaScript('window.sessionStorage.getItem("default_oauth")',
procedure(const AValue: string)
begin
if AValue<>'null' then
begin
FormITREPOS_Protserver_Inloggen.InlogInfo.DefaultToken := AValue;
end;
end);
end;
end;

After this ExecuteJavaScript has been executed the debugger (sometimes) gives an error. When closing the form or application. (Only when debugging)
Debug Output:
Crirical error detected c0000374

Any idea what is causing this error and how we can resolve this?

This is the first time we encounter this kind of issue. Can you try to call the code inside the Invoke from a separate thread instead?

function TCoreWebView2HistoryChangedEventHandler.Invoke(Sender: ICoreWebView2;
  args: IInterface): HRESULT;
var
  s: string;
  cText: string;
  aTask: ITask;
begin
  Result := S_OK;
  if Sender.get_Source(@s) = S_OK then
  begin
    cText := PChar(s);
    aTask := TTask.Create(
      procedure
      begin
        FormITREPOS_Protserver_Inloggen.AdvWebBrowserLogin.ExecuteJavaScript
          ('window.sessionStorage.getItem("default_oauth")',
          procedure(const AValue: string)
          begin
            if AValue <> 'null' then
            begin
              FormITREPOS_Protserver_Inloggen.InlogInfo.DefaultToken := AValue;
            end;
          end);
      end);
    aTask.Start;
  end;
end;

Hi @Pieter_Scheldeman, unfortunately your suggestion doesn't resolve the issue.

In addition: when just running the application (without debugging), we experience random crashes.

Very strange issue. Can you throw together a small sample that we can try to reproduce?