GNOME Bugzilla – Bug 364868
GDI resource leak in GtkStatusIcon on win32
Last modified: 2008-01-20 17:04:56 UTC
GtkStatusIcon leaks GDI objects when icon image is updated on Windows. If you enable blinking with gtk_status_icon_set_blinking(), the usage of GDI objects keeps increasing. In gtkstatusicon.c::gtk_status_icon_update_image(), priv->nid.hIcon is updated to the new one, but the old one is not deleted. When gdk_win32_pixbuf_to_hicon_libgtk_only() is called, the new icon is created by CreateIconIndirect(), and it must be deleted with DestroyIcon() afterward. The following patch fixes the leaks. --- gtkstatusicon.c.orig Sun Sep 10 15:33:16 2006 +++ gtkstatusicon.c Wed Oct 25 10:59:35 2006 @@ -791,6 +791,8 @@ gtk_status_icon_blank_icon (status_icon)); #endif #ifdef GDK_WINDOWING_WIN32 + if (priv->nid.hIcon) + DestroyIcon (priv->nid.hIcon); priv->nid.hIcon = gdk_win32_pixbuf_to_hicon_libgtk_only (gtk_status_icon_blank_icon (status_icon)); priv->nid.uFlags |= NIF_ICON; if (priv->nid.hWnd != NULL && priv->visible) @@ -832,6 +834,8 @@ gtk_image_set_from_pixbuf (GTK_IMAGE (priv->image), scaled); #endif #ifdef GDK_WINDOWING_WIN32 + if (priv->nid.hIcon) + DestroyIcon (priv->nid.hIcon); priv->nid.hIcon = gdk_win32_pixbuf_to_hicon_libgtk_only (scaled); priv->nid.uFlags |= NIF_ICON; if (priv->nid.hWnd != NULL && priv->visible) @@ -870,6 +874,8 @@ priv->image_data.stock_id, GTK_ICON_SIZE_SMALL_TOOLBAR, NULL); + if (priv->nid.hIcon) + DestroyIcon (priv->nid.hIcon); priv->nid.hIcon = gdk_win32_pixbuf_to_hicon_libgtk_only (pixbuf); priv->nid.uFlags |= NIF_ICON; if (priv->nid.hWnd != NULL && priv->visible) @@ -897,6 +903,8 @@ priv->size, 0, NULL); + if (priv->nid.hIcon) + DestroyIcon (priv->nid.hIcon); priv->nid.hIcon = gdk_win32_pixbuf_to_hicon_libgtk_only (pixbuf); priv->nid.uFlags |= NIF_ICON; if (priv->nid.hWnd != NULL && priv->visible)
Maybe priv->nid.hIcon must also be freed in gtk_status_icon_finalize() after Shell_NotifyIconW (NIM_DELETE, &priv->nid).
Does Shell_NotifyIcon() make a copy of the icon passed to it? If no, isn't it then wrong to call DestroyIcon on the icon that might still be in use?
Shell_NotifyIcon() doesn't make a copy of the icon passed to it. The patch was a quick hack, so it's better to copy the old hIcon to the temporary variable and destroy it after Shell_NotifyIcon().
Created attachment 75817 [details] [review] GtkStatusIcon resource leak fix (revised) DestroyIcon() is called after Shell_NotifyIcon() now. I've also added DestroyIcon() in gtk_status_icon_finalize().
Patch applied to HEAD and gtk-2-10, thanks. 2006-12-28 Tor Lillqvist <tml@novell.com> * gtk/gtkstatusicon.c (gtk_status_icon_finalize) (gtk_status_icon_update_image): Don't leak HICONs on Win32. (#364868, Hiroyuki Yamamoto)
Can anyone have a look at bug 510278 and test if it is still present with GTK+ 2.12?