我正在尝试使用 WebkitGTK。使用一个简单的“hello world”风格的应用程序,它只下载一个网页然后退出,WebKit 在运行程序后报告泄漏的对象:
LEAK: 1 CachedResource
LEAK: 12 WebCoreNode
WebkitGTK 程序是一个简单的 C 程序:
#include <gtk/gtk.h>
#include <webkit/webkit.h>
#include <stdio.h>
static void load_status_callback(GObject* object, GParamSpec* pspec, void* window)
{
WebKitWebView* view = WEBKIT_WEB_VIEW(object);
WebKitLoadStatus status = webkit_web_view_get_load_status(view);
switch (status)
{
case WEBKIT_LOAD_FINISHED:
{
printf("WEBKIT_LOAD_FINISHED\n");
gtk_main_quit();
break;
}
default: g_assert_not_reached();
}
};
int main(int argc, char* argv[])
{
gtk_init(&argc, &argv);
GtkWidget *main_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_default_size(GTK_WINDOW(main_window), 800, 600);
WebKitWebView *webView = WEBKIT_WEB_VIEW(webkit_web_view_new());
GtkWidget *scrolledWindow = gtk_scrolled_window_new(NULL, NULL);
gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledWindow),
GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
gtk_container_add(GTK_CONTAINER(scrolledWindow), GTK_WIDGET(webView));
g_signal_connect(webView, "notify::load-status", G_CALLBACK(load_status_callback), main_window);
gtk_container_add(GTK_CONTAINER(main_window), scrolledWindow);
webkit_web_view_load_uri(webView, "http://127.0.0.1/test.html");
gtk_widget_grab_focus(GTK_WIDGET(webView));
gtk_widget_show_all(main_window);
gtk_main(); // Run main event loop
gtk_widget_destroy(main_window);
return 0;
}
我下载的 HTML 很简单:
<html>
<head>
<script type = "text/javascript"></script>
</head>
<body>
<div id = "foo"></div>
</body>
</html>
经过一些实验,我观察到如果我删除标签就不会发生泄漏<script>
,这让我相信我应该做一些事情来关闭 JavaScriptCore。那么,为什么 WebKit 会报告泄漏?