pygobject r1060 - in trunk: . gio



Author: paulp
Date: Mon Apr 13 20:28:24 2009
New Revision: 1060
URL: http://svn.gnome.org/viewvc/pygobject?rev=1060&view=rev

Log:
2009-04-13  Paul Pogonyshev  <pogonyshev gmx net>

	Bug 578870 â memory leak in gio.File.copy_async

	* gio/gio.override (PyGIONotify): Add 'slaves' field.
	(pygio_notify_new_slave): New function.
	(pygio_notify_reference_callback): Recurse for slaves, if any.
	(pygio_notify_free): Likewise.

	* gio/gfile.override (file_progress_callback_marshal): Use new
	chaining functionality to avoid leaking the second PyGIONotify
	structure.  Rename 'callback' as 'progress_callback' for clarity.


Modified:
   trunk/ChangeLog
   trunk/gio/gfile.override
   trunk/gio/gio.override

Modified: trunk/gio/gfile.override
==============================================================================
--- trunk/gio/gfile.override	(original)
+++ trunk/gio/gfile.override	Mon Apr 13 20:28:24 2009
@@ -601,10 +601,12 @@
   int io_priority = G_PRIORITY_DEFAULT;
   PyGObject *pycancellable = NULL;
   GCancellable *cancellable;
-  GFileProgressCallback callback = NULL;
+  GFileProgressCallback progress_callback = NULL;
 
+  /* After the creation, referencing/freeing will automatically be
+   * done on the master and the slave. */
   notify = pygio_notify_new();
-  progress_notify = pygio_notify_new();
+  progress_notify = pygio_notify_new_slave(notify);
 
   if (!PyArg_ParseTupleAndKeywords(args, kwargs,
                                    "O!O|OiOOOO:File.copy_async",
@@ -627,7 +629,7 @@
       goto error;
 
   if (pygio_notify_using_optional_callback(progress_notify)) {
-      callback = (GFileProgressCallback)file_progress_callback_marshal;
+      progress_callback = (GFileProgressCallback) file_progress_callback_marshal;
       if (!pygio_notify_callback_is_valid_full(progress_notify, "progress_callback"))
           goto error;
   }
@@ -636,15 +638,13 @@
       goto error;
 
   pygio_notify_reference_callback(notify);
-  pygio_notify_reference_callback(progress_notify);
 
-  /* FIXME: 'progress_notify' is not properly freed up as far as I see. */
   g_file_copy_async(G_FILE(self->obj),
                     G_FILE(destination->obj),
                     flags,
                     io_priority,
                     cancellable,
-                    callback,
+                    progress_callback,
                     progress_notify,
                     (GAsyncReadyCallback)async_result_callback_marshal,
                     notify);
@@ -654,7 +654,6 @@
 
  error:
   pygio_notify_free(notify);
-  pygio_notify_free(progress_notify);
   return NULL;
 }
 %%

Modified: trunk/gio/gio.override
==============================================================================
--- trunk/gio/gio.override	(original)
+++ trunk/gio/gio.override	Mon Apr 13 20:28:24 2009
@@ -33,14 +33,20 @@
 
 #define BUFSIZE 8192
 
-typedef struct {
+typedef struct _PyGIONotify PyGIONotify;
+
+struct _PyGIONotify {
     gboolean  referenced;
     PyObject *callback;
     PyObject *data;
     gboolean  attach_self;
     gpointer  buffer;
     gsize     buffer_size;
-} PyGIONotify;
+
+    /* If a structure has any 'slaves', those will reference their
+     * callbacks and be freed together with the 'master'. */
+    PyGIONotify *slaves;
+};
 
 static GQuark
 pygio_notify_get_internal_quark(void)
@@ -57,6 +63,18 @@
     return g_slice_new0(PyGIONotify);
 }
 
+static PyGIONotify *
+pygio_notify_new_slave(PyGIONotify* master)
+{
+    PyGIONotify *slave = pygio_notify_new();
+
+    while (master->slaves)
+        master = master->slaves;
+    master->slaves = slave;
+
+    return slave;
+}
+
 static gboolean
 pygio_notify_using_optional_callback(PyGIONotify *notify)
 {
@@ -100,6 +118,9 @@
         notify->referenced = TRUE;
         Py_XINCREF(notify->callback);
         Py_XINCREF(notify->data);
+
+        if (notify->slaves)
+            pygio_notify_reference_callback(notify->slaves);
     }
 }
 
@@ -144,6 +165,9 @@
 pygio_notify_free(PyGIONotify *notify)
 {
     if (notify) {
+        if (notify->slaves)
+            pygio_notify_free(notify->slaves);
+
         if (notify->referenced) {
             PyGILState_STATE state;
 



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