Bug 556910 – [fam-helper.c:223]: Memory leak: sub

2008-10-24  Matthias Clasen  <mclasen@redhat.com>

        Bug 556910 – [fam-helper.c:223]: Memory leak: sub

        * fam/fam-helper.c: Fix a memory leak and formatting issues.
        Reported by Daniel Marjamäki


svn path=/trunk/; revision=7624
This commit is contained in:
Matthias Clasen 2008-10-24 04:04:42 +00:00 committed by Matthias Clasen
parent 05fb3a25e5
commit b616e892ca
2 changed files with 115 additions and 98 deletions

View File

@ -1,3 +1,10 @@
2008-10-24 Matthias Clasen <mclasen@redhat.com>
Bug 556910 [fam-helper.c:223]: Memory leak: sub
* fam/fam-helper.c: Fix a memory leak and formatting issues.
Reported by Daniel Marjamäki
2008-10-23 Matthias Clasen <mclasen@redhat.com> 2008-10-23 Matthias Clasen <mclasen@redhat.com>
* gdesktopappinfo.c (g_app_info_reset_type_associations): Fix docs. * gdesktopappinfo.c (g_app_info_reset_type_associations): Fix docs.

View File

@ -70,76 +70,80 @@ fam_event_to_file_monitor_event (int code)
static gboolean static gboolean
fam_do_iter_unlocked (void) fam_do_iter_unlocked (void)
{ {
while (fam_connection != NULL && FAMPending (fam_connection)) { while (fam_connection != NULL && FAMPending (fam_connection))
FAMEvent ev; {
fam_sub* sub = NULL; FAMEvent ev;
gboolean cancelled; fam_sub* sub = NULL;
gboolean cancelled;
if (FAMNextEvent (fam_connection, &ev) != 1) { if (FAMNextEvent (fam_connection, &ev) != 1)
FAMClose (fam_connection); {
g_free (fam_connection); FAMClose (fam_connection);
g_source_remove (fam_watch_id); g_free (fam_connection);
fam_watch_id = 0; g_source_remove (fam_watch_id);
fam_connection = NULL; fam_watch_id = 0;
return FALSE; fam_connection = NULL;
} return FALSE;
}
sub = (fam_sub*)ev.userdata; sub = (fam_sub*)ev.userdata;
cancelled = sub->cancelled; cancelled = sub->cancelled;
if (ev.code == FAMAcknowledge && cancelled) if (ev.code == FAMAcknowledge && cancelled)
{ {
_fam_sub_free (sub); _fam_sub_free (sub);
continue;
}
if (cancelled)
continue;
if (sub->directory)
{
GFileMonitor* monitor = G_FILE_MONITOR (sub->user_data);
GFileMonitorEvent eflags = fam_event_to_file_monitor_event (ev.code);
gchar* path = NULL;
GFile *child, *parent;
/* unsupported event */
if (eflags == -1)
continue; continue;
}
if (cancelled)
continue;
if (sub->directory)
{
GFileMonitor* monitor = G_FILE_MONITOR (sub->user_data);
GFileMonitorEvent eflags = fam_event_to_file_monitor_event (ev.code);
gchar* path = NULL;
GFile *child, *parent;
if (ev.filename[0] == '/') /* unsupported event */
path = g_strdup (ev.filename); if (eflags == -1)
else continue;
path = g_strdup_printf ("%s/%s", sub->pathname, ev.filename);
if (ev.filename[0] == '/')
path = g_strdup (ev.filename);
else
path = g_strdup_printf ("%s/%s", sub->pathname, ev.filename);
child = g_file_new_for_path (path); child = g_file_new_for_path (path);
parent = g_file_get_parent (child); parent = g_file_get_parent (child);
g_file_monitor_emit_event (monitor, child, NULL, eflags); g_file_monitor_emit_event (monitor, child, NULL, eflags);
g_free (path); g_free (path);
g_object_unref (child); g_object_unref (child);
g_object_unref (parent); g_object_unref (parent);
} else { }
GFile *child; else
GFileMonitor* monitor = G_FILE_MONITOR (sub->user_data); {
GFileMonitorEvent eflags = fam_event_to_file_monitor_event (ev.code); GFile *child;
gchar* path = NULL; GFileMonitor* monitor = G_FILE_MONITOR (sub->user_data);
GFileMonitorEvent eflags = fam_event_to_file_monitor_event (ev.code);
gchar* path = NULL;
if (eflags == -1) if (eflags == -1)
continue; continue;
path = g_strdup (ev.filename); path = g_strdup (ev.filename);
child = g_file_new_for_path (path); child = g_file_new_for_path (path);
g_file_monitor_emit_event (monitor, child, NULL, eflags); g_file_monitor_emit_event (monitor, child, NULL, eflags);
g_free (path); g_free (path);
g_object_unref (child); g_object_unref (child);
} }
} }
return TRUE; return TRUE;
} }
static gboolean static gboolean
fam_callback (GIOChannel *source, fam_callback (GIOChannel *source,
GIOCondition condition, GIOCondition condition,
gpointer data) gpointer data)
{ {
gboolean res; gboolean res;
G_LOCK (fam_connection); G_LOCK (fam_connection);
@ -157,25 +161,27 @@ _fam_sub_startup (void)
G_LOCK (fam_connection); G_LOCK (fam_connection);
if (fam_connection == NULL) { if (fam_connection == NULL)
fam_connection = g_new0 (FAMConnection, 1); {
if (FAMOpen2 (fam_connection, "gvfs user") != 0) { fam_connection = g_new0 (FAMConnection, 1);
g_warning ("FAMOpen failed, FAMErrno=%d\n", FAMErrno); if (FAMOpen2 (fam_connection, "gvfs user") != 0)
g_free (fam_connection); {
fam_connection = NULL; g_warning ("FAMOpen failed, FAMErrno=%d\n", FAMErrno);
G_UNLOCK (fam_connection); g_free (fam_connection);
return FALSE; fam_connection = NULL;
} G_UNLOCK (fam_connection);
return FALSE;
}
#ifdef HAVE_FAM_NO_EXISTS #ifdef HAVE_FAM_NO_EXISTS
/* This is a gamin extension that avoids sending all the Exists event for dir monitors */ /* This is a gamin extension that avoids sending all the Exists event for dir monitors */
FAMNoExists (fam_connection); FAMNoExists (fam_connection);
#endif #endif
ioc = g_io_channel_unix_new (FAMCONNECTION_GETFD(fam_connection)); ioc = g_io_channel_unix_new (FAMCONNECTION_GETFD(fam_connection));
fam_watch_id = g_io_add_watch (ioc, fam_watch_id = g_io_add_watch (ioc,
G_IO_IN | G_IO_HUP | G_IO_ERR, G_IO_IN | G_IO_HUP | G_IO_ERR,
fam_callback, fam_connection); fam_callback, fam_connection);
g_io_channel_unref (ioc); g_io_channel_unref (ioc);
} }
G_UNLOCK (fam_connection); G_UNLOCK (fam_connection);
@ -187,41 +193,43 @@ _fam_sub_shutdown (void)
{ {
G_LOCK (fam_connection); G_LOCK (fam_connection);
if (fam_connection != NULL) { if (fam_connection != NULL)
FAMClose (fam_connection); {
g_free (fam_connection); FAMClose (fam_connection);
g_source_remove (fam_watch_id); g_free (fam_connection);
fam_watch_id = 0; g_source_remove (fam_watch_id);
fam_connection = NULL; fam_watch_id = 0;
} fam_connection = NULL;
}
G_UNLOCK (fam_connection); G_UNLOCK (fam_connection);
} }
fam_sub* fam_sub*
_fam_sub_add (const gchar* pathname, _fam_sub_add (const gchar *pathname,
gboolean directory, gboolean directory,
gpointer user_data) gpointer user_data)
{ {
fam_sub *sub; fam_sub *sub;
if (!_fam_sub_startup ()) if (!_fam_sub_startup ())
return NULL; return NULL;
sub = g_new0 (fam_sub, 1);
sub->pathname = g_strdup (pathname);
sub->directory = directory;
sub->user_data = user_data;
G_LOCK (fam_connection); G_LOCK (fam_connection);
/* We need to queue up incoming messages to avoid blocking on write /* We need to queue up incoming messages to avoid blocking on write
* if there are many monitors being canceled */ * if there are many monitors being canceled */
fam_do_iter_unlocked (); fam_do_iter_unlocked ();
if (fam_connection == NULL) { if (fam_connection == NULL)
G_UNLOCK (fam_connection); {
return NULL; G_UNLOCK (fam_connection);
} return NULL;
}
sub = g_new0 (fam_sub, 1);
sub->pathname = g_strdup (pathname);
sub->directory = directory;
sub->user_data = user_data;
if (directory) if (directory)
FAMMonitorDirectory (fam_connection, pathname, &sub->request, sub); FAMMonitorDirectory (fam_connection, pathname, &sub->request, sub);
@ -229,6 +237,7 @@ _fam_sub_add (const gchar* pathname,
FAMMonitorFile (fam_connection, pathname, &sub->request, sub); FAMMonitorFile (fam_connection, pathname, &sub->request, sub);
G_UNLOCK (fam_connection); G_UNLOCK (fam_connection);
return sub; return sub;
} }
@ -245,10 +254,11 @@ _fam_sub_cancel (fam_sub* sub)
* if there are many monitors being canceled */ * if there are many monitors being canceled */
fam_do_iter_unlocked (); fam_do_iter_unlocked ();
if (fam_connection == NULL) { if (fam_connection == NULL)
G_UNLOCK (fam_connection); {
return FALSE; G_UNLOCK (fam_connection);
} return FALSE;
}
FAMCancelMonitor (fam_connection, &sub->request); FAMCancelMonitor (fam_connection, &sub->request);