Page 1 of 1

Sharing Memory problem.

Posted: Sun Jul 12, 2020 12:05 am
by yesilcimenahmet
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.

Re: Sharing Memory problem.

Posted: Sun Jul 12, 2020 7:50 am
by salvadordf
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/CEF4Delph ... ss.dpr#L70

Re: Sharing Memory problem.

Posted: Sun Jul 12, 2020 5:23 pm
by yesilcimenahmet
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.

Re: Sharing Memory problem.

Posted: Mon Jul 13, 2020 10:10 am
by salvadordf
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.

Re: Sharing Memory problem.

Posted: Mon Jul 13, 2020 11:42 am
by yesilcimenahmet
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?

Re: Sharing Memory problem.

Posted: Mon Jul 13, 2020 12:34 pm
by salvadordf
The JSExtension and many other demos have process messages.

Read the code comments in that demo for all the details :
https://github.com/salvadordf/CEF4Delph ... n.pas#L122

Those instructions are also here :
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 :
viewtopic.php?f=10&t=1360&p=5655

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