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.

CloseBrowser(True) gives Access Violation

Post Reply
yesilcimenahmet
Posts: 10
Joined: Wed Jul 08, 2020 1:25 pm

CloseBrowser(True) gives Access Violation

Post by yesilcimenahmet »

Code: Select all

.....
type
TCanvasDrawing = class(TCustomChromiumWindow)
protected
procedure DoClose;override;
end;

procedure TCanvasDrawing.DoClose;
begin
  Self.DestroyChildWindow; //falls into access violation after this method call. not this method! it returns True
end;
....

type
TCustomChromiumWindow = class(TCEFWinControl)
protected
      procedure   WebBrowser_OnClose(Sender: TObject; const browser: ICefBrowser; var aAction : TCefCloseBrowserAction);
      procedure DoClose;virtual;abstract;
end;


procedure TCustomChromiumWindow.WebBrowser_OnClose(Sender: TObject; const browser: ICefBrowser; var aAction : TCefCloseBrowserAction);
begin
  aAction := cbaClose;
  DoClose;
end;

There is no error when the program ends. But when I call the CloseBrowser (True) method while the program is running, Access Violation occurs.

I don't want to use Windows messages. Like below;

Code: Select all

procedure   OnCloseMsg(var aMessage : TMessage); message CEF_DOONCLOSE; 
Access Violation does not occur when I use Windows messages. Because "aAction: = cbaDelay;" and by destroying the message with PostMessage, the DestroyChildWindow method runs on the Main Thread and returns the value False.

However, calling the DestroyChildWindow method from the same Thread as the WebBrowser_OnClose method causes problems.

How can I fix this?
User avatar
salvadordf
Posts: 4016
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: CloseBrowser(True) gives Access Violation

Post by salvadordf »

Hi,

Sorry for moving this conversation to this forum but it's much easier to read the code here.

As you probably have seen, using CEF to embed a Chromium browser is not as easy as TWebBrowser because Chromium uses multiple processes and threads to render web pages.

Most of the TChromium events are executed in a secondary thread and some of the GlobalCEFApp events are executed in a different process.

The VCL is not thread safe and you shouldn't modify, create or destroy Windows controls inside TChromium events. Some demos are oversimplified to show code that is easier to understand but you should always remember that all the code in the demos that handles any control inside a TChromium event should be moved out of that event.

Sometimes you can use "TThread.synchronize" to do simple modifications to VCL controls inside TChromium events but I would recommend to send a simple Windows message to the main form to handle those controls in the main application thread.

TChromiumWindow is only meant for extremely simple browsers. If your application uses a new class that inherits from it I would recommend to use the code in the SimpleBrowser2 demo, which uses TChromium and TCEFWindowParent.

Please, don't modify the code inside the TChromium.OnClose, TChromium.OnBeforeClose and TForm.OnCloseQuery events.

All applications that use CEF must wait until all browsers are properly closed and those events follow the "destruction steps" described by the CEF documentation.
yesilcimenahmet
Posts: 10
Joined: Wed Jul 08, 2020 1:25 pm

Re: CloseBrowser(True) gives Access Violation

Post by yesilcimenahmet »

I understand, Thanks for your valuable informaion.
Post Reply