Disclosure Statement: This site contains affiliate links, which means that I may receive a commission if you make a purchase using these links. As an eBay Partner, I earn from qualifying purchases.
If you find these projects useful please consider becoming a sponsor with Patreon, GitHub or Liberapay.

Chromium Critical error

Post Reply
User avatar
Uefi1
Posts: 43
Joined: Tue Aug 24, 2021 1:58 pm

Chromium Critical error

Post by Uefi1 »

Hello, I'm getting an сritical error when closing and destroying the browser in my module:

Code: Select all

//Module
unit MyProject;

interface

uses
  Windows, Messages, Controls, SysUtils, uCEFChromium,
  uCEFInterfaces, uCEFTypes, uCEFConstants;
type
 TMyproject= class
 private
 chromium1:Tchromium;
 protected
 FOnGetAdr:Get;
 public
 published
    constructor Create;
    destructor Destroy; override;
    procedure Chromium1Close(Sender: TObject;
  const browser: ICefBrowser; var aAction : TCefCloseBrowserAction);
    
  procedure Chromium1BeforeClose(Sender: TObject;
  const browser: ICefBrowser);
    procedure Chromium1LoadEnd(Sender: TObject; const browser: ICefBrowser;
  end;
procedure  TMyproject.Chromium1LoadEnd(Sender: TObject;
  const browser: ICefBrowser; const frame: ICefFrame;
  httpStatusCode: Integer);
begin
chromium1.ExecuteJavaScript('document.querySelector("body > mybody").click();',chromium1.DefaultUrl,0);
end;

procedure  TMyproject.Chromium1Close(Sender: TObject;
  const browser: ICefBrowser; var aAction : TCefCloseBrowserAction);

begin
  PostMessage(Hwnd(TMyproject), CEF_DESTROY, 0, 0);
  aAction := cbaDelay;
end;

procedure  TMyproject.Chromium1BeforeClose(Sender: TObject;
  const browser: ICefBrowser);
begin
  PostMessage(Hwnd(TMyproject), WM_CLOSE, 0, 0);
end;

constructor  TMyproject.Create;
begin
NUM:=-1;
chromium1:=TChromium.Create(nil);
Chromium1.Options.ImageLoading:=STATE_DISABLED;
Chromium1.Options.Plugins:= STATE_DISABLED;
Chromium1.Options.ImageShrinkStandaloneToFit:= STATE_DISABLED;
Chromium1.Options.FileAccessFromFileUrls:= STATE_DISABLED;
Chromium1.Options.JavascriptCloseWindows:= STATE_DISABLED;
Chromium1.Options.UniversalAccessFromFileUrls:= STATE_DISABLED;
Chromium1.Options.Webgl:= STATE_DISABLED;
Chromium1.Options.TabToLinks:= STATE_DISABLED;
Chromium1.Options.ApplicationCache:= STATE_DISABLED;
chromium1.OnLoadEnd:=Chromium1LoadEnd;
chromium1.OnBeforeClose:=Chromium1BeforeClose;
chromium1.OnClose:=Chromium1Close;
chromium1.DefaultUrl:=(extractfilepath(paramstr(0))+'myhtm.html');
chromium1.CreateBrowser(nil);
PostMessage(Hwnd(TMyproject), CEF_AFTERCREATED, 0, 0);
end;

destructor  TMyproject.Destroy;
begin
inherited;
Chromium1.CloseBrowser(True); //Critical error triggered here
Chromium1.Free;
end;

//Form1

procedure TForm1.Button1Click(Sender: TObject);
var
mymodule:TMyproject;
begin
mymodule:=TMyproject.Create;
mymodule.Free; //Critical error triggered here
end;
User avatar
salvadordf
Posts: 4580
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: Chromium Critical error

Post by salvadordf »

Hi,

TChromiumWindow requires several steps to destroy a browser and it's not possible to call TChromiumWindow.Free until all of the steps are completed.

However, I would recommend that you use a normal TChromium component inside a custom TFrame. See the TabbedBrowser2 demo for all the details.
User avatar
Uefi1
Posts: 43
Joined: Tue Aug 24, 2021 1:58 pm

Re: Chromium Critical error

Post by Uefi1 »

salvadordf wrote: Mon Jun 20, 2022 9:09 am Hi,

TChromiumWindow requires several steps to destroy a browser and it's not possible to call TChromiumWindow.Free until all of the steps are completed.

However, I would recommend that you use a normal TChromium component inside a custom TFrame. See the TabbedBrowser2 demo for all the details.
Dude, I've been waiting for your answer for the whole day, so that you write such nonsense to me
Chromium1.Free no matter with or without it

Code: Select all

destructor  TMyproject.Destroy;
begin
inherited;
Chromium1.CloseBrowser(True); //Critical error triggered here
end;
User avatar
salvadordf
Posts: 4580
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: Chromium Critical error

Post by salvadordf »

Sorry, I thought you were using a TChromiumWindow component.

That code is creating an instance of TMyproject which also creates a browser with the chromium1.CreateBrowser(nil); call.

If you use the default GlobalCEFApp properties then GlobalCEFApp.MultiThreadedMessageLoop is true and the chromium1.CreateBrowser(nil); will be asynchronous.

It will return before creating the browser and the problem comes when the form tries to destroy the TMyproject instance while the browser is still being created.

Once you call TChromium.CreateBrowser you need to wait until the TChromium.OnAfterCreated event is triggered before trying to destroy the browser.
User avatar
Uefi1
Posts: 43
Joined: Tue Aug 24, 2021 1:58 pm

Re: Chromium Critical error

Post by Uefi1 »

salvadordf wrote: Mon Jun 20, 2022 7:27 pm Sorry, I thought you were using a TChromiumWindow component.

That code is creating an instance of TMyproject which also creates a browser with the chromium1.CreateBrowser(nil); call.

If you use the default GlobalCEFApp properties then GlobalCEFApp.MultiThreadedMessageLoop is true and the chromium1.CreateBrowser(nil); will be asynchronous.

It will return before creating the browser and the problem comes when the form tries to destroy the TMyproject instance while the browser is still being created.

Once you call TChromium.CreateBrowser you need to wait until the TChromium.OnAfterCreated event is triggered before trying to destroy the browser.
If it's not difficult for you, please write a part of the code, at least briefly
User avatar
salvadordf
Posts: 4580
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: Chromium Critical error

Post by salvadordf »

All the demos implement that event like this :
https://github.com/salvadordf/CEF4Delphi/blob/09d8ec866959c76e5ae1fb3340a1fbea3e6c9b6b/demos/Delphi_VCL/SimpleBrowser2/uSimpleBrowser2.pas#L155
User avatar
Uefi1
Posts: 43
Joined: Tue Aug 24, 2021 1:58 pm

Re: Chromium Critical error

Post by Uefi1 »

salvadordf wrote: Mon Jun 20, 2022 7:40 pm All the demos implement that event like this :
https://github.com/salvadordf/CEF4Delphi/blob/09d8ec866959c76e5ae1fb3340a1fbea3e6c9b6b/demos/Delphi_VCL/SimpleBrowser2/uSimpleBrowser2.pas#L155
So it works now without errors, but it seems that the software is not released, it may be incorrect Handle ?

Code: Select all

PostMessage(Hwnd(TMyproject), CEF_DESTROY, 0, 0);
User avatar
Uefi1
Posts: 43
Joined: Tue Aug 24, 2021 1:58 pm

Re: Chromium Critical error

Post by Uefi1 »

salvadordf wrote: Mon Jun 20, 2022 7:40 pm All the demos implement that event like this :
https://github.com/salvadordf/CEF4Delphi/blob/09d8ec866959c76e5ae1fb3340a1fbea3e6c9b6b/demos/Delphi_VCL/SimpleBrowser2/uSimpleBrowser2.pas#L155
Chromium1.BrowserHandle this is right Handle ?)
PostMessage(chromium1.BrowserHandle, WM_CLOSE, 0, 0);
it's just that chromium is still not freed from memory and i have memory leaks

Code: Select all

//Module
unit MyProject;

interface

uses
  Windows, Messages, Controls, SysUtils, uCEFChromium,
  uCEFInterfaces, uCEFTypes, uCEFConstants;
type
 TMyproject= class(Tchromium)
 private
 chromium1:Tchromium;
 protected
 FOnGetAdr:Get;
 public
 published
    constructor Create;
    destructor Destroy; override;
    procedure Chromium1Close(Sender: TObject;
  const browser: ICefBrowser; var aAction : TCefCloseBrowserAction);
   procedure Chromium1AfterCreated(Sender: TObject; const browser: ICefBrowser); 
  procedure Chromium1BeforeClose(Sender: TObject;
  const browser: ICefBrowser);
    procedure Chromium1LoadEnd(Sender: TObject; const browser: ICefBrowser;
  end;
procedure  TMyproject.Chromium1LoadEnd(Sender: TObject;
  const browser: ICefBrowser; const frame: ICefFrame;
  httpStatusCode: Integer);
begin
chromium1.ExecuteJavaScript('document.querySelector("body > mybody").click();',chromium1.DefaultUrl,0);
end;



procedure  TMyproject.Chromium1Close(Sender: TObject;
  const browser: ICefBrowser; var aAction : TCefCloseBrowserAction);

begin
  PostMessage(chromium1.BrowserHandle, CEF_DESTROY, 0, 0);
  aAction := cbaDelay;
end;

procedure  TMyproject.Chromium1BeforeClose(Sender: TObject;
  const browser: ICefBrowser);
begin
  PostMessage(chromium1.BrowserHandle, WM_CLOSE, 0, 0);
end;

procedure TMyproject.Chromium1AfterCreated(Sender: TObject;
  const browser: ICefBrowser);
begin
PostMessage(chromium1.BrowserHandle, CEF_AFTERCREATED, 0, 0);
end;

constructor  TMyproject.Create;
begin
NUM:=-1;
chromium1:=TChromium.Create(nil);
Chromium1.Options.ImageLoading:=STATE_DISABLED;
Chromium1.Options.Plugins:= STATE_DISABLED;
Chromium1.Options.ImageShrinkStandaloneToFit:= STATE_DISABLED;
Chromium1.Options.FileAccessFromFileUrls:= STATE_DISABLED;
Chromium1.Options.JavascriptCloseWindows:= STATE_DISABLED;
Chromium1.Options.UniversalAccessFromFileUrls:= STATE_DISABLED;
Chromium1.Options.Webgl:= STATE_DISABLED;
Chromium1.Options.TabToLinks:= STATE_DISABLED;
Chromium1.Options.ApplicationCache:= STATE_DISABLED;
chromium1.OnLoadEnd:=Chromium1LoadEnd;
chromium1.OnBeforeClose:=Chromium1BeforeClose;
chromium1.OnClose:=Chromium1Close;
chromium1.OnAfterCreated:=Chromium1AfterCreated;
chromium1.DefaultUrl:=(extractfilepath(paramstr(0))+'myhtm.html');
chromium1.CreateBrowser(nil);
PostMessage(chromium1.BrowserHandle, CEF_AFTERCREATED, 0, 0);
end;

destructor  TMyproject.Destroy;
begin
inherited;
Chromium1.CloseBrowser(True); //Memory Leaks
Chromium1.Free;
end;

//Form1

procedure TForm1.Button1Click(Sender: TObject);
var
mymodule:TMyproject;
begin
mymodule:=TMyproject.Create;
mymodule.Free; //Memory Leaks
end;
User avatar
salvadordf
Posts: 4580
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: Chromium Critical error

Post by salvadordf »

That browser is using the OSR mode and it should follow the same destruction steps as the SimpleOSRBrowser demo.

Call TChromium.CloseBrowser(True) and wait until the TChromium.OnBeforeClose event is executed before destroying the TMyproject instance.

TChromium.CloseBrowser(True) is asynchronous and it may return before TChromium.OnBeforeClose is triggered.
thefunkyjoint
Posts: 513
Joined: Thu Aug 10, 2017 12:40 pm

Re: Chromium Critical error

Post by thefunkyjoint »

Uefi1 wrote: Mon Jun 20, 2022 12:17 pm Dude, I've been waiting for your answer for the whole day, so that you write such nonsense to me
Chromium1.Free no matter with or without it
Calm down, this is an open source and FREE project. It's a privilege to use it, not a right.
Post Reply