当我尝试动态加载使用 webkit2gtk 库编译的自定义库 (.so) 时出现段错误

Getting Seg Fault when I try to dynamically load a custom library (.so) which is compiled with webkit2gtk library

我创建了一个共享库,它有一个函数 displaywebview,可以启动 GTK window 并使用 webkit2gtk 将 URL 加载到其中。

现在我正在编写一个 caller 程序,它使用 dlopen 加载这个库,使用 dlsym 获取方法 displaywebview 并调用这个函数。

我在调用 webkit_web_view_new() 时在 displaywebview 中遇到段错误。有人可以帮我解释为什么会这样吗?

webkit_main.so

#include <gtk/gtk.h>
#include <webkit2/webkit2.h>

extern "C"
{
    int displayWebView();
}

int displayWebView()
{

    printf("Entered in displayWebView\n");
    // Initialize GTK+
    gtk_init(NULL, NULL);

    // Create an 800x600 window that will contain the browser instance
    GtkWidget *main_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_default_size(GTK_WINDOW(main_window), 800, 600);

    WebKitWebView *webView = (WebKitWebView*)webkit_web_view_new();

    // webkit_web_view_new();

    // // Put the browser area into the main window
    gtk_container_add(GTK_CONTAINER(main_window), GTK_WIDGET(webView));

    // Set up callbacks so that if either the main window or the browser instance is
    // closed, the program will exit
    g_signal_connect(main_window, "destroy", G_CALLBACK(destroyWindowCb), NULL);
    g_signal_connect(webView, "close", G_CALLBACK(closeWebViewCb), main_window);

    // // Load a web page into the browser instance
    webkit_web_view_load_uri(webView, "http://www.gmail.com");

    // // Make sure that when the browser area becomes visible, it will get mouse
    // // and keyboard events
    gtk_widget_grab_focus(GTK_WIDGET(webView));

    // // Make sure the main window and all its contents are visible
    gtk_widget_show_all(main_window);

    // // Run the main GTK+ event loop
    gtk_main();

    return 0;
}

caller.cpp

#include <unistd.h>
#include <stdlib.h>
#include <dlfcn.h>
#include <stdio.h>

typedef int (*PDISPLAYWEBVIEW)();

int main(){
    void* hnd = dlopen("/home/radix/Desktop/webkit_socket/webkit_main.so", RTLD_LAZY);
    // sleep(10);
    if(hnd!=NULL){

        PDISPLAYWEBVIEW pdisplayWebView = (PDISPLAYWEBVIEW)dlsym(hnd,"displayWebView");
        if(pdisplayWebView == NULL){
            printf("dlsym error %s", dlerror());
        }
        else{
            printf("Everything okay, launch the function\n");

            (*pdisplayWebView)();

        }

        dlclose(hnd);

    }
    else{
        printf("The error is %s", dlerror());
    }
}

BACKTRACE 详细信息:

Thread 1 "caller" received signal SIGSEGV, Segmentation fault.
0x0000000000000000 in ?? ()
(gdb) bt
#0  0x0000000000000000 in ?? ()
#1  0x00007ffff790d165 in std::thread::_M_start_thread(std::unique_ptr<std::thread::_State, std::default_delete<std::thread::_State> >, void (*)()) () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#2  0x00007ffff2009168 in bmalloc::Scavenger::Scavenger(std::lock_guard<bmalloc::Mutex>&) () from /usr/lib/x86_64-linux-gnu/libjavascriptcoregtk-4.0.so.18
#3  0x00007ffff1d12f61 in bmalloc::PerProcess<bmalloc::Scavenger>::getSlowCase() () from /usr/lib/x86_64-linux-gnu/libjavascriptcoregtk-4.0.so.18
#4  0x00007ffff2001fcc in bmalloc::Heap::Heap(bmalloc::HeapKind, std::lock_guard<bmalloc::Mutex>&) () from /usr/lib/x86_64-linux-gnu/libjavascriptcoregtk-4.0.so.18
#5  0x00007ffff1fff1ff in bmalloc::PerProcess<bmalloc::PerHeapKind<bmalloc::Heap> >::getSlowCase() () from /usr/lib/x86_64-linux-gnu/libjavascriptcoregtk-4.0.so.18
#6  0x00007ffff1ffee99 in bmalloc::Cache::Cache(bmalloc::HeapKind) () from /usr/lib/x86_64-linux-gnu/libjavascriptcoregtk-4.0.so.18
#7  0x00007ffff1fff311 in bmalloc::PerThread<bmalloc::PerHeapKind<bmalloc::Cache> >::getSlowCase() () from /usr/lib/x86_64-linux-gnu/libjavascriptcoregtk-4.0.so.18
#8  0x00007ffff1ffef0d in bmalloc::Cache::allocateSlowCaseNullCache(bmalloc::HeapKind, unsigned long) () from /usr/lib/x86_64-linux-gnu/libjavascriptcoregtk-4.0.so.18
#9  0x00007ffff1af22a2 in JSC::ExecutableAllocator::initializeAllocator() () from /usr/lib/x86_64-linux-gnu/libjavascriptcoregtk-4.0.so.18
#10 0x00007ffff1d0bf25 in ?? () from /usr/lib/x86_64-linux-gnu/libjavascriptcoregtk-4.0.so.18
#11 0x00007ffff0926739 in __pthread_once_slow (once_control=0x7ffff22a6ff0, init_routine=0x7ffff790c120 <__once_proxy>) at pthread_once.c:116
#12 0x00007ffff1d0d90d in JSC::initializeThreading() () from /usr/lib/x86_64-linux-gnu/libjavascriptcoregtk-4.0.so.18
#13 0x00007ffff49beb29 in ?? () from /usr/lib/x86_64-linux-gnu/libwebkit2gtk-4.0.so.37
#14 0x00007ffff4aa4add in ?? () from /usr/lib/x86_64-linux-gnu/libwebkit2gtk-4.0.so.37
#15 0x00007ffff4b0eb00 in ?? () from /usr/lib/x86_64-linux-gnu/libwebkit2gtk-4.0.so.37
#16 0x00007ffff0e5e777 in ?? () from /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0
#17 0x00007ffff0e5fc0d in g_object_newv () from /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0
#18 0x00007ffff0e603c4 in g_object_new () from /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0
#19 0x00007ffff4b0ab7d in ?? () from /usr/lib/x86_64-linux-gnu/libwebkit2gtk-4.0.so.37
#20 0x00007ffff0ba74a5 in g_once_impl () from /lib/x86_64-linux-gnu/libglib-2.0.so.0
#21 0x00007ffff4b31cc9 in webkit_web_view_new () from /usr/lib/x86_64-linux-gnu/libwebkit2gtk-4.0.so.37
#22 0x00007ffff6d96f0f in displayWebView () at webkit_main.cpp:81
#23 0x00005555555548df in main () at caller.cpp:20

:

  1. 当我将同一个程序与 webkitgtk-1.0 一起使用时,它运行得非常好。使用 webkit2gtk-4.0 会出现此问题。

  2. 当我用 libwebkit2gtk-4.0 编译 caller.cpp 时,它奇怪地不再给出段错误了。

有人能帮我弄清楚为什么会这样吗?

我正在使用 debian 9 和 webkit2gtk-4.0-37 版本:2.22.2-1~bpo9+1

有些编译器选项必须在每个组件(对象模块、库、共享对象)中都相同,以便它们可以协同工作。 除了 -pthread,我想到了大文件支持(32 位)。

如果组件的不同版本链接在一起,则可能是问题的另一个来源。