Fixed memory management in styles
authorP. F. Chimento <philip.chimento@gmail.com>
Sat, 13 Nov 2010 00:20:21 +0000 (01:20 +0100)
committerP. F. Chimento <philip.chimento@gmail.com>
Sat, 13 Nov 2010 00:20:21 +0000 (01:20 +0100)
Added a free for the StyleSets that were leaked, and fixed a double
unref of text tags that was causing lots of warnings when closing the
widget. Also added a test case.

libchimara/chimara-glk.c
libchimara/style.c
tests/.gitignore
tests/Makefile.am
tests/test-close.c [new file with mode: 0644]

index bbdb614028cb9780ca1df8d4440985e2dbcb5cdd..5cdf63eba1ab7d64a241380d76d86f73cd7cb5dc 100644 (file)
@@ -305,6 +305,8 @@ chimara_glk_finalize(GObject *object)
        g_free(priv->program_name);
        g_free(priv->program_info);
        g_free(priv->story_name);
+       g_free(priv->styles);
+       g_free(priv->glk_styles);
 
        /* Chain up to parent */
     G_OBJECT_CLASS(chimara_glk_parent_class)->finalize(object);
index 9fc6b1e499fe38a907b182f9988c08fce0515221..4785b9086e4d3c1e1f9aa9fcd3775636f6ef1318 100644 (file)
@@ -376,6 +376,7 @@ style_init(ChimaraGlk *glk)
        for(i=0; i<style_NUMSTYLES; i++) {
                tag = gtk_text_tag_new(GLK_TAG_NAMES[i]);
                g_hash_table_insert(glk_text_grid_styles, (gchar*) GLK_TAG_NAMES[i], tag);
+               g_object_ref(tag); /* Add an extra reference since the tag is in two hashtables */
                g_hash_table_insert(glk_text_buffer_styles, (gchar*) GLK_TAG_NAMES[i], tag);
        }
 
index 465ec265a649a013558a89085b59c3e5a7a00d78..8de7f560371913ca25844391a4b6fed53d85c470 100644 (file)
@@ -8,3 +8,4 @@ Makefile.in
 glulxercise
 plugin-loader
 test-multisession
+test-close
index e4ed4d4c1b43642ed5bb44126e8a035ed086dbb7..0300609191e20023fe7ba45c99594dd6fcff52bf 100644 (file)
@@ -10,7 +10,7 @@ TEST_PLUGIN_LIBTOOL_FLAGS = \
        -export-symbols-regex "^glk_main$$" \
        -rpath $(abs_builddir)
 
-noinst_PROGRAMS = test-multisession glulxercise plugin-loader
+noinst_PROGRAMS = test-multisession glulxercise plugin-loader test-close
 
 test_multisession_SOURCES = test-multisession.c
 test_multisession_CFLAGS = @TEST_CFLAGS@ $(AM_CFLAGS)
@@ -25,6 +25,10 @@ plugin_loader_SOURCES = plugin-loader.c
 plugin_loader_CFLAGS = @TEST_CFLAGS@ $(AM_CFLAGS)
 plugin_loader_LDADD = @TEST_LIBS@ $(top_builddir)/libchimara/libchimara.la
 
+test_close_SOURCES = test-close.c
+test_close_CFLAGS = @TEST_CFLAGS@ $(AM_CFLAGS)
+test_close_LDADD = @TEST_LIBS@ $(top_builddir)/libchimara/libchimara.la
+
 noinst_LTLIBRARIES = first.la model.la gridtest.la splittest.la multiwin.la styletest.la
 
 first_la_SOURCES = first.c
diff --git a/tests/test-close.c b/tests/test-close.c
new file mode 100644 (file)
index 0000000..ddc2919
--- /dev/null
@@ -0,0 +1,39 @@
+#include <gtk/gtk.h>
+#include <libchimara/chimara-if.h>
+
+int
+main(int argc, char *argv[])
+{
+    GtkWidget *window, *glk;
+
+    /* Initialize threads and GTK */
+    if(!g_thread_supported())
+        g_thread_init(NULL);
+    gdk_threads_init();
+    gtk_init(&argc, &argv);
+    
+    /* Construct the window and its contents. We quit the GTK main loop
+     * when the window's close button is clicked. */
+    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
+    g_signal_connect(window, "delete-event", G_CALLBACK(gtk_main_quit), NULL);
+    glk = chimara_glk_new();
+    gtk_container_add(GTK_CONTAINER(window), glk);
+    gtk_widget_show_all(window);
+    
+    /* Add a reference to the ChimaraGlk widget, because we want to keep it
+    around after gtk_main() exits */
+    g_object_ref(glk);
+    
+    /* Start the GTK main loop */
+    gdk_threads_enter();
+    gtk_main();
+    gdk_threads_leave();
+
+    /* After the GTK main loop exits, signal the Glk program to shut down if
+     * it is still running, and wait for it to exit. */
+    chimara_glk_stop(CHIMARA_GLK(glk));
+    chimara_glk_wait(CHIMARA_GLK(glk));
+    g_object_unref(glk);
+
+    return 0;
+}