[webkit-gtk] webkit_web_view_run_javascript_finish can't get results on webkit2, crashes

Egor Egorov me at egorfine.com
Thu Sep 19 08:51:45 PDT 2013


This simple reduced test case crashes at JSStringGetMaximumUTF8CStringSize(). Actually I tried a few options to get the string value back from webkit2 instance and googled a lot to no avail. 

It segfaults both on gtk webkit 2.0.4 and 2.1.92, both built by gcc 4.8.1 on Ubuntu 13.04 with all packages updated. 

Any chance to fix it? 

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

void gotInnerHtml(GObject *object, GAsyncResult *result, gpointer data) {
	WebKitJavascriptResult *js_result;
	JSValueRef              value;
	JSGlobalContextRef      context;
	GError                 *error = NULL;

	GtkWidget *webView = (GtkWidget *) data;

	js_result = webkit_web_view_run_javascript_finish(WEBKIT_WEB_VIEW(webView), result, &error);
	if (!js_result) {
		g_warning ("Error running javascript: %s", error->message);
		g_error_free (error);

	context = webkit_javascript_result_get_global_context(js_result);
	value = webkit_javascript_result_get_value(js_result);

	if (JSValueIsString (context, value)) {
		JSStringRef js_str_value;
		gchar      *str_value;
		gsize       str_length;

		js_str_value = JSValueToStringCopy (context, value, NULL);

		str_length = JSStringGetMaximumUTF8CStringSize(js_str_value); // crashes here
		str_value = (gchar *)g_malloc (str_length);

		JSStringGetUTF8CString (js_str_value, str_value, str_length);
		JSStringRelease (js_str_value);

		g_print ("Script result: %s\n", str_value);
		g_free (str_value);
	} else {
		g_warning ("Error running javascript: unexpected return value");

	webkit_javascript_result_unref (js_result);

void getInnerHtml(GtkWidget *webView) {
	webkit_web_view_run_javascript(WEBKIT_WEB_VIEW(webView), "'a little string';", NULL, gotInnerHtml, webView);

gboolean doGetHtml(gpointer data) {
	getInnerHtml((GtkWidget *)data);
	return FALSE;

int main(int argc, char* argv[]) {
	printf("Version %d.%d.%d\n", webkit_get_major_version(), webkit_get_minor_version(), webkit_get_micro_version());
	gtk_init(&argc, &argv);

	GtkWidget *mainWebWindow = gtk_window_new(GTK_WINDOW_TOPLEVEL);
	gtk_window_set_default_size(GTK_WINDOW(mainWebWindow), 800, 600);

	GtkWidget *webView = webkit_web_view_new();

	gtk_container_add(GTK_CONTAINER(mainWebWindow), webView);

	WebKitURIRequest *request = webkit_uri_request_new("http://www.google.com/");
	webkit_web_view_load_request(WEBKIT_WEB_VIEW(webView), request);

	GtkWidget *window1 = gtk_widget_get_toplevel(webView);

	g_timeout_add(2000, doGetHtml, webView);


	return 0;

Egor Egorov

Warning: this email may contain humor, sarcasm, parody, and perhaps even irony. Read at your own risk.

More information about the webkit-gtk mailing list