Sharing Memory problem.

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

Sharing Memory problem.

Post by yesilcimenahmet » Sun Jul 12, 2020 12:05 am

Hi,

I have static class for management GlobalCef operations.

Like;

Code: Select all

  TWebCanvasGlobalCEFManager = class
  private
    class var
      FContextCreatedEvenList: TList<TOnContextCreatedEvent>;
    class var
      FLock: TCriticalSection;
  private
    class procedure Lock; inline;
    class procedure UnLock; inline;
    class function MethodPointersEqual(const MethodPointer1, MethodPointer2): Boolean;
  public
    class procedure RegisterContextCreatedEvent(const Event: TOnContextCreatedEvent);
    class procedure UnRegisterContextCreatedEvent(const Event: TOnContextCreatedEvent);
    class procedure CreateGlobalCEFApp;
    class constructor Create;
    class destructor Destroy;
  end;
I am calling RegisterContextCreatedEvent to put event into FContextCreatedEvenList and I am calling in

Code: Select all

  GlobalCEFApp.OnContextCreated :=
    procedure(const browser: ICefBrowser; const frame: ICefFrame; const context: ICefv8Context)
    begin
    end;
When I use SingleProcess := True Everthing is ok, but when SingleProcess is False, FContextCreatedEvenList count is always Zero (0).

I am calling RegisterContextCreatedEvent method in before my chroimum is created. I know it works different processes. How can i do ?



You can see debug.log file below;
[0712/025012.330:ERROR:CEF4Delphi(1)] Created Chroimum
[0712/025012.384:ERROR:CEF4Delphi(1)] list count 1 (count in chromium)
[0712/025012.682:ERROR:CEF4Delphi(1)] OnContextCreated
[0712/025012.682:ERROR:CEF4Delphi(1)] list count 0
[0712/025012.770:ERROR:CEF4Delphi(1)] OnContextCreated
[0712/025012.770:ERROR:CEF4Delphi(1)] list count 0
[0712/025012.790:ERROR:CEF4Delphi(1)] OnContextCreated
[0712/025012.790:ERROR:CEF4Delphi(1)] list count 0
Thanks.

User avatar
salvadordf
Posts: 2495
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: Sharing Memory problem.

Post by salvadordf » Sun Jul 12, 2020 7:50 am

Hi,

TCefApplicationCore.OnContextCreated is executed in the "render" process only.

I guess that you are creating the TWebCanvasGlobalCEFManager class in the "browser" process and you use a different EXE for the subprocesses.

You would have to set GlobalCEFApp.OnContextCreated in the subprocess project code. The JSWindowBindingSubProcess demo has exactly the code you need :
https://github.com/salvadordf/CEF4Delphi/blob/59ec2022e5c63238375be64d71aa40bd1f5e3b20/demos/Delphi_VCL/JavaScript/JSWindowBindingSubProcess/SubProcess.dpr#L70

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

Re: Sharing Memory problem.

Post by yesilcimenahmet » Sun Jul 12, 2020 5:23 pm

Hi,

Thanks for your reply.

But I did not use a sub process and I do not want to use it.

I use the functions I defined in the OnContextCreated event in Javascript. The OnContextCreated event is a global event. However, I wanted to make all the actions carried out within this event with an event incentive to isolate each browser. In this way, every browser object can define functions or variables that concern it. Thus, an abstraction process takes place.

At this point, I cannot change the data in the List object because it does not run in SingleProcess = false mode and a different process has been created by the CEF for the OnContextCreated event.

I have defined the FContextCreatedEvenList list as a global variable in implementation space. But the result has not changed.

I add to the FContextCreatedEvenList list before OnContextCreated works, but as I mentioned to you, the problem occurs because the event runs in a different process.

I have to solve this problem. Please help me.

User avatar
salvadordf
Posts: 2495
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: Sharing Memory problem.

Post by salvadordf » Mon Jul 13, 2020 10:10 am

Windows protects each process and it's not possible to access the variables, pointers, objects, etc. declared in one process from a different process.

If you need to share information between processes you can use :
  • The process messages available in CEF. They have some size limitations but they work very well.
  • An alternative solution to have IPC. Websockets is usually recommended and you can use the TCEFServerComponent as shown in the SimpleServer demo.
  • You might find useful to use a common data base. It should allow concurrent accesses or you would have to protect it with a named mutex.

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

Re: Sharing Memory problem.

Post by yesilcimenahmet » Mon Jul 13, 2020 11:42 am

Thank you, I thought of using IPC. But I want to use the first option. So Process Messages. Is there a CEF sample? or would you give the unit / class names?

User avatar
salvadordf
Posts: 2495
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: Sharing Memory problem.

Post by salvadordf » Mon Jul 13, 2020 12:34 pm

The JSExtension and many other demos have process messages.

Read the code comments in that demo for all the details :
https://github.com/salvadordf/CEF4Delphi/blob/4be703326c44be831e18f3ac2d6a5d4ec99beae9/demos/Delphi_VCL/JavaScript/JSExtension/uJSExtension.pas#L122

Those instructions are also here :
https://www.briskbard.com/forum/viewtopic.php?f=8&t=918&p=4084

We are using another easier method to send information from the render process to the browser process called the "console trick" that you can see here :
https://www.briskbard.com/forum/viewtopic.php?f=10&t=1360&p=5655

The latest DOMVisitor demo has an example about that communication method.

Post Reply