hlwhl / webview_cef

WebView for Flutter Desktop Apps using CEF(Chromium Embedded Framework) [Work in Progress]
Apache License 2.0
176 stars 52 forks source link

应用闪退 #96

Open abandon43 opened 9 months ago

abandon43 commented 9 months ago

我用overlay展示了这个webview,第一个打开没问题,关闭后第二次打开应用闪退 调试模式下报错为断开设备连接,代码会debug到 WebviewHandler::WebviewHandler() { DCHECK(!g_instance); g_instance = this; } FATAL:webview_handler.cc(35)] Check failed: !g_instance. 帮看看,我的老哥

abandon43 commented 9 months ago

是macos下的

SinyimZhi commented 9 months ago

因为现在这个插件还不支持多实例,你在使用overlay的时候第一次关闭的时候是销毁了webviewhandler,但是Webviewhandler本身是单例的。所以这里判空会报错,如果你想支持多实例使用的话,目前这个插件还有一段路要走,我个人的fork上有个试验性的分支,但是依然面临着很多问题,尤其是面对多窗口时。

SinyimZhi commented 9 months ago

尤其是mac上面问题更多,实在是之前没有macos的技术栈,一直做的很慢 /笑哭

alldev commented 8 months ago

Hey, @SinyimZhi, I also faced that "!g_instance" problem and probably found the solution to solve it. In our case it's not about WebView multi instance, but about same instance reinitialization. We have separate screen deep inside our application which contains WebView, the business logic is to open/close it time to time. Also it seems similar problem was already mentioned in prevoius issue which was closed as not reproducible.

I think I figured out the solution. Please chech the webview_plugin.cc file. In Dec 26 2023 commit this file was changed, but not drastically. However, these changes cause error.

I am on the recent (i.e. Jan 15 2024) commit and changed few things.

This part of handler & app initialization:

CefRefPtr<WebviewHandler> handler;
CefRefPtr<WebviewApp> app;

Was changed by me to this one:

CefRefPtr<WebviewHandler> handler(new WebviewHandler());
CefRefPtr<WebviewApp> app(new WebviewApp(handler));

And this part of CEF initialization:

void initCEFProcesses(CefMainArgs args){
    mainArgs = args;
    initCEFProcesses();
}

void initCEFProcesses(){
#ifdef OS_MAC
    CefScopedLibraryLoader loader;
    if(!loader.LoadInMain()) {
        printf("load cef err");
    }
#endif
    handler = new WebviewHandler();
    app = new WebviewApp(handler);
    CefExecuteProcess(mainArgs, app, nullptr);
}

To this one:

void initCEFProcesses(CefMainArgs args){
    mainArgs = args;
    CefExecuteProcess(mainArgs, app, nullptr);
 }

void initCEFProcesses(){
#ifdef OS_MAC
    CefScopedLibraryLoader loader;
    if(!loader.LoadInMain()) {
        printf("load cef err");
    }
#endif
    CefExecuteProcess(mainArgs, app, nullptr);
}

Now everything seems working fine and I do not face any issue.

Hope this stuff can be helpful to be implemented in update and/or will be useful for further understanding.

SinyimZhi commented 8 months ago

@alldev In fact, the CefRefPtr<WebviewHandler> handler(new WebviewHandler()); CefRefPtr<WebviewApp> app(new WebviewApp(handler)); is not quite correct because variables are initialized once on the main thread during program execution, but when initCEFProcesses are executed, they may be initialized again because initCEFProcesses may not be executed on the main thread under different operating systems.That's why i changed the code to initialized WebviewHandler、WebviewApp when initCEFProcesses is called(In fact, it is also to prepare for multiple instantiation). However, this brings another issue, when you dispose WebViewController at dart side. CefShutdown() was called.Due to the intelligent pointer,the singleton WebviewHandler was released. And when you guys called WebViewController::initialize() again, it will try to get WebviewHandler and will not pass null checker. That's why "!g_instance" assert is Thrown out. Above all, there is currently no good way to balance the contradiction between multiple component page instances you want and the existing single instance mode of plugins support. What can i do is try to build correct multi instance mode as fast as i can.

SinyimZhi commented 4 months ago

Hi, every one. I've merged multi instance and multi window support branch to main. There is a lot of problems might have been resloved. Could you guys help us to verify what we did success and what new problem brought in? @alldev @Crush-star @