[yelp/yelp-3-0] [libyelp] Fixing various memory issues



commit 397bc3188e468abf2f9bad638b8471e3e60ecb20
Author: Shaun McCance <shaunm gnome org>
Date:   Thu Sep 24 16:11:14 2009 -0500

    [libyelp] Fixing various memory issues

 libyelp/yelp-document.c        |    7 ++--
 libyelp/yelp-simple-document.c |    5 ++-
 libyelp/yelp-uri.c             |   66 +++++++++++++++++++--------------------
 libyelp/yelp-uri.h             |    6 ++--
 libyelp/yelp-view.c            |   25 +++++++++++++--
 libyelp/yelp-view.h            |    2 +
 tests/test-view.c              |   27 +++++++++++++----
 7 files changed, 87 insertions(+), 51 deletions(-)
---
diff --git a/libyelp/yelp-document.c b/libyelp/yelp-document.c
index 2f9df0d..f4bcb3b 100644
--- a/libyelp/yelp-document.c
+++ b/libyelp/yelp-document.c
@@ -85,7 +85,8 @@ static void           document_finish_read      (YelpDocument         *document,
 static gchar *        document_get_mime_type    (YelpDocument         *document,
 						 const gchar          *mime_type);
 
-static void           request_cancel            (Request              *request);
+static void           request_cancel            (GCancellable         *cancellable,
+						 Request              *request);
 static gboolean       request_idle_contents     (Request              *request);
 static gboolean       request_idle_info         (Request              *request);
 static void           request_try_free          (Request              *request);
@@ -114,7 +115,7 @@ yelp_document_class_init (YelpDocumentClass *klass)
 {
     GObjectClass   *object_class = G_OBJECT_CLASS (klass);
 
-    object_class->dispose  = yelp_document_finalize;
+    object_class->dispose  = yelp_document_dispose;
     object_class->finalize = yelp_document_finalize;
 
     klass->request_page =   document_request_page;
@@ -606,7 +607,7 @@ yelp_document_signal (YelpDocument       *document,
 }
 
 static void
-request_cancel (Request *request)
+request_cancel (GCancellable *cancellable, Request *request)
 {
     GSList *cur;
     YelpDocument *document = request->document;
diff --git a/libyelp/yelp-simple-document.c b/libyelp/yelp-simple-document.c
index 099d456..11b303c 100644
--- a/libyelp/yelp-simple-document.c
+++ b/libyelp/yelp-simple-document.c
@@ -92,7 +92,8 @@ static void           stream_close_cb                  (GInputStream
 							GAsyncResult            *result,
 							YelpSimpleDocument      *document);
 
-static void           request_cancel                   (Request                 *request);
+static void           request_cancel                   (GCancellable            *cancellable,
+							Request                 *request);
 static void           request_try_free                 (Request                 *request);
 static void           request_free                     (Request                 *request);
 
@@ -381,7 +382,7 @@ stream_close_cb (GInputStream       *stream,
 /******************************************************************************/
 
 static void
-request_cancel (Request *request)
+request_cancel (GCancellable *cancellable, Request *request)
 {
     GSList *cur;
     YelpSimpleDocument *document = request->document;
diff --git a/libyelp/yelp-uri.c b/libyelp/yelp-uri.c
index 580e533..d3e6150 100644
--- a/libyelp/yelp-uri.c
+++ b/libyelp/yelp-uri.c
@@ -49,25 +49,25 @@ static void           yelp_uri_dispose      (GObject        *object);
 static void           yelp_uri_finalize     (GObject        *object);
 
 static void           resolve_file_uri      (YelpUri        *ret,
-                                             gchar          *arg);
+                                             const gchar    *arg);
 static void           resolve_file_path     (YelpUri        *ret,
                                              YelpUri        *base,
-                                             gchar          *arg);
+                                             const gchar    *arg);
 static void           resolve_data_dirs     (YelpUri        *ret,
-                                             gchar         **subdirs,
-                                             gchar          *docid,
-                                             gchar          *pageid);
+                                             const gchar   **subdirs,
+                                             const gchar    *docid,
+                                             const gchar    *pageid);
 static void           resolve_ghelp_uri     (YelpUri        *ret,
-                                             gchar          *arg);
+                                             const gchar    *arg);
 static void           resolve_man_uri       (YelpUri        *ret,
-                                             gchar          *arg);
+                                             const gchar    *arg);
 static void           resolve_info_uri      (YelpUri        *ret,
-                                             gchar          *arg);
+                                             const gchar    *arg);
 static void           resolve_page_and_frag (YelpUri        *ret,
-                                             gchar          *arg);
+                                             const gchar    *arg);
 static void           resolve_common        (YelpUri        *ret);
-static gboolean       is_man_path           (gchar          *uri,
-                                             gchar          *encoding);
+static gboolean       is_man_path           (const gchar    *uri,
+                                             const gchar    *encoding);
 
 static GObjectClass *parent_class;
 
@@ -116,9 +116,6 @@ yelp_uri_dispose (GObject *object)
         g_object_unref (uri->priv->gfile);
         uri->priv->gfile = NULL;
     }
-    g_strfreev (uri->priv->search_path);
-    g_free (uri->priv->page_id);
-    g_free (uri->priv->frag_id);
 
     G_OBJECT_CLASS (yelp_uri_parent_class)->dispose (object);
 }
@@ -138,13 +135,13 @@ yelp_uri_finalize (GObject *object)
 /******************************************************************************/
 
 YelpUri *
-yelp_uri_resolve (gchar *arg)
+yelp_uri_resolve (const gchar *arg)
 {
     return yelp_uri_resolve_relative (NULL, arg);
 }
 
 YelpUri *
-yelp_uri_resolve_relative (YelpUri *base, gchar *arg)
+yelp_uri_resolve_relative (YelpUri *base, const gchar *arg)
 {
     YelpUri *ret;
 
@@ -208,16 +205,15 @@ yelp_uri_get_frag_id (YelpUri *uri)
 /******************************************************************************/
 
 static void
-resolve_file_uri (YelpUri *ret, gchar *arg)
+resolve_file_uri (YelpUri *ret, const gchar *arg)
 {
     gchar *uri;
-    gchar *hash;
+    const gchar *hash = strchr (arg, '#');
 
-    hash = strchr (arg, '#');
     if (hash)
         uri = g_strndup (arg, hash - arg);
     else
-        uri = arg;
+        uri = (gchar *) arg;
 
     ret->priv->gfile = g_file_new_for_uri (uri);
 
@@ -230,16 +226,15 @@ resolve_file_uri (YelpUri *ret, gchar *arg)
 }
 
 static void
-resolve_file_path (YelpUri *ret, YelpUri *base, gchar *arg)
+resolve_file_path (YelpUri *ret, YelpUri *base, const gchar *arg)
 {
     gchar *path;
-    gchar *hash;
+    const gchar *hash = strchr (arg, '#');
 
-    hash = strchr (arg, '#');
     if (hash)
         path = g_strndup (arg, hash - arg);
     else
-        path = arg;
+        path = (gchar *) arg;
 
     if (arg[0] == '/') {
         ret->priv->gfile = g_file_new_for_path (path);
@@ -266,7 +261,10 @@ resolve_file_path (YelpUri *ret, YelpUri *base, gchar *arg)
 }
 
 static void
-resolve_data_dirs (YelpUri *ret, gchar **subdirs, gchar *docid, gchar *pageid)
+resolve_data_dirs (YelpUri      *ret,
+                   const gchar **subdirs,
+                   const gchar  *docid,
+                   const gchar  *pageid)
 {
     const gchar * const *datadirs = g_get_system_data_dirs ();
     const gchar * const *langs = g_get_language_names ();
@@ -296,7 +294,6 @@ resolve_data_dirs (YelpUri *ret, gchar **subdirs, gchar *docid, gchar *pageid)
                 }
                 searchpath[searchi] = helpdir;
                 searchpath[++searchi] = NULL;
-                /* FIXME: append to path */
 
                 if (type != YELP_URI_DOCUMENT_TYPE_UNKNOWN)
                     /* We've already found it.  We're just adding to the search path now. */
@@ -340,14 +337,14 @@ resolve_data_dirs (YelpUri *ret, gchar **subdirs, gchar *docid, gchar *pageid)
 }
 
 static void
-resolve_ghelp_uri (YelpUri *ret, gchar *arg)
+resolve_ghelp_uri (YelpUri *ret, const gchar *arg)
 {
     /* ghelp:/path/to/file
      * ghelp:document
      */
     const gchar const *helpdirs[3] = {"help", "gnome/help", NULL};
     gchar *docid = NULL;
-    gchar *colon, *hash, *slash, *pageid; /* don't free */
+    gchar *colon, *hash, *slash, *pageid; /* do not free */
 
     colon = strchr (arg, ':');
     if (!colon) {
@@ -384,7 +381,7 @@ resolve_ghelp_uri (YelpUri *ret, gchar *arg)
         pageid = docid;
     }
 
-    resolve_data_dirs (ret, (gchar **) helpdirs, docid, pageid);
+    resolve_data_dirs (ret, helpdirs, docid, pageid);
 
     /* Specifying pages and anchors for Mallard documents with ghelp URIs is sort
        hacked on.  This is a touch inconsistent, but it maintains compatibility
@@ -401,7 +398,7 @@ resolve_ghelp_uri (YelpUri *ret, gchar *arg)
 }
 
 static void
-resolve_man_uri (YelpUri *ret, gchar *arg)
+resolve_man_uri (YelpUri *ret, const gchar *arg)
 {
     /* man:/path/to/file
      * man:name(section)
@@ -414,6 +411,7 @@ resolve_man_uri (YelpUri *ret, gchar *arg)
     gchar *section = NULL;
     gchar *name = NULL;
     gchar *fullpath = NULL;
+
     /* not to be freed */
     gchar *colon, *hash;
     gchar *lbrace = NULL;
@@ -441,7 +439,7 @@ resolve_man_uri (YelpUri *ret, gchar *arg)
     if (colon)
         colon++;
     else
-        colon = arg;
+        colon = (gchar *) arg;
 
     hash = strchr (colon, '#');
     if (hash)
@@ -547,13 +545,13 @@ resolve_man_uri (YelpUri *ret, gchar *arg)
 }
 
 static void
-resolve_info_uri (YelpUri *ret, gchar *arg)
+resolve_info_uri (YelpUri *ret, const gchar *arg)
 {
     /* FIXME */
 }
 
 static void
-resolve_page_and_frag (YelpUri *ret, gchar *arg)
+resolve_page_and_frag (YelpUri *ret, const gchar *arg)
 {
     gchar *hash;
 
@@ -665,7 +663,7 @@ resolve_common (YelpUri *ret)
 }
 
 static gboolean
-is_man_path (gchar *path, gchar *encoding)
+is_man_path (const gchar *path, const gchar *encoding)
 {
     gchar **iter = (gchar **) mancats;
 
diff --git a/libyelp/yelp-uri.h b/libyelp/yelp-uri.h
index 9562f50..fc180a4 100644
--- a/libyelp/yelp-uri.h
+++ b/libyelp/yelp-uri.h
@@ -64,9 +64,9 @@ struct _YelpUriClass {
 
 GType             yelp_uri_get_type     (void);
 
-YelpUri *         yelp_uri_resolve           (gchar     *arg);
-YelpUri *         yelp_uri_resolve_relative  (YelpUri   *base,
-                                              gchar     *arg);
+YelpUri *         yelp_uri_resolve           (const gchar *arg);
+YelpUri *         yelp_uri_resolve_relative  (YelpUri     *base,
+                                              const gchar *arg);
 
 YelpUriDocumentType  yelp_uri_get_document_type  (YelpUri   *uri);
 gchar *              yelp_uri_get_base_uri       (YelpUri   *uri);
diff --git a/libyelp/yelp-view.c b/libyelp/yelp-view.c
index 4403388..7153021 100644
--- a/libyelp/yelp-view.c
+++ b/libyelp/yelp-view.c
@@ -52,6 +52,7 @@ G_DEFINE_TYPE (YelpView, yelp_view, WEBKIT_TYPE_WEB_VIEW);
 
 struct _YelpViewPriv {
     YelpUri       *uri;
+    YelpDocument  *document;
     GCancellable  *cancellable;
 };
 
@@ -84,6 +85,11 @@ yelp_view_dispose (GObject *object)
         view->priv->cancellable = NULL;
     }
 
+    if (view->priv->document) {
+        g_object_unref (view->priv->document);
+        view->priv->document = NULL;
+    }
+
     G_OBJECT_CLASS (yelp_view_parent_class)->dispose (object);
 }
 
@@ -123,10 +129,22 @@ yelp_view_new (void)
 }
 
 void
-yelp_view_load (YelpView *view,
-                YelpUri  *uri)
+yelp_view_load (YelpView    *view,
+                const gchar *uri)
+{
+    YelpUri *yuri = yelp_uri_resolve (uri);
+    yelp_view_load_uri (view, yuri);
+    g_object_unref (yuri);
+}
+
+void
+yelp_view_load_uri (YelpView *view,
+                    YelpUri  *uri)
 {
-    /* FIXME: get a document and call load_document */
+    /* FIXME: want to get from a factory, just for testing */
+    YelpDocument *document = yelp_simple_document_new (uri);
+    yelp_view_load_document (view, uri, document);
+    g_object_unref (document);
 }
 
 void
@@ -141,6 +159,7 @@ yelp_view_load_document (YelpView     *view,
     page_id = yelp_uri_get_page_id (uri);
     view->priv->uri = g_object_ref (uri);
     view->priv->cancellable = g_cancellable_new ();
+    view->priv->document = g_object_ref (document);
     yelp_document_request_page (document,
                                 page_id,
                                 view->priv->cancellable,
diff --git a/libyelp/yelp-view.h b/libyelp/yelp-view.h
index a5bafd8..6cdeded 100644
--- a/libyelp/yelp-view.h
+++ b/libyelp/yelp-view.h
@@ -54,6 +54,8 @@ GType            yelp_view_get_type        (void);
 GtkWidget *      yelp_view_new             (void);
 
 void             yelp_view_load            (YelpView     *view,
+					    const gchar  *uri);
+void             yelp_view_load_uri        (YelpView     *view,
 					    YelpUri      *uri);
 void             yelp_view_load_document   (YelpView     *view,
 					    YelpUri      *uri,
diff --git a/tests/test-view.c b/tests/test-view.c
index 99a70f0..e28ebee 100644
--- a/tests/test-view.c
+++ b/tests/test-view.c
@@ -22,14 +22,23 @@
 
 #include <gtk/gtk.h>
 #include <webkit/webkit.h>
+
+#include "yelp-location-entry.h"
 #include "yelp-view.h"
 #include "yelp-uri.h"
 #include "yelp-simple-document.h"
 
+static void
+activate_cb (GtkEntry *entry,
+	     YelpView *view)
+{
+    yelp_view_load (view, gtk_entry_get_text (entry));
+}
+
 int
 main (int argc, char **argv)
 {
-    GtkWidget *window, *scroll, *view;
+    GtkWidget *window, *vbox, *entry, *scroll, *view;
     YelpUri *uri;
     YelpDocument *document;
     GCancellable *cancellable;
@@ -41,19 +50,25 @@ main (int argc, char **argv)
     window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
     gtk_window_set_default_size (GTK_WINDOW (window), 640, 480);
 
+    vbox = gtk_vbox_new (FALSE, 0);
+    gtk_container_add (GTK_CONTAINER (window), vbox);
+
+    entry = gtk_entry_new ();
+    gtk_box_pack_start (GTK_BOX (vbox), entry, FALSE, FALSE, 0);
+
     scroll = gtk_scrolled_window_new (NULL, NULL);
     gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scroll),
 				    GTK_POLICY_AUTOMATIC,
 				    GTK_POLICY_AUTOMATIC);
-    gtk_container_add (GTK_CONTAINER (window), scroll);
+    gtk_box_pack_start (GTK_BOX (vbox), scroll, TRUE, TRUE, 0);
 
     view = yelp_view_new ();
     gtk_container_add (GTK_CONTAINER (scroll), view);
 
-    g_assert (argc >= 2);
-    uri = yelp_uri_resolve (argv[1]);
-    document = yelp_simple_document_new (uri);
-    yelp_view_load_document (view, uri, document);
+    g_signal_connect (entry, "activate", activate_cb, view);
+
+    if (argc >= 2)
+	yelp_view_load (view, argv[1]);
 
     gtk_widget_show_all (GTK_WIDGET (window));
 



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]