[gtk-mac-integration] Fix object release issues found with Clang static analysis



commit b60af782ee3a18d94eb3ba0efb1445ebbb735913
Author: Richard Procter <richard n procter gmail com>
Date:   Tue Feb 14 17:54:59 2012 -0800

    Fix object release issues found with Clang static analysis
    
    With a small change from Richard's submission: Autoreleasing the
    GtkApplicationDelegate in gtkosx_application_init causes a crash.

 src/cocoa_menu_item.c          |   39 ++++++++++++-----------------
 src/gtk-mac-dock.c             |    4 +-
 src/gtk-mac-image-utils.c      |    2 +-
 src/gtk-mac-image-utils.h      |    2 +-
 src/gtkosxapplication.h        |    1 -
 src/gtkosxapplication_quartz.c |   53 +++++++++++++++++++---------------------
 6 files changed, 45 insertions(+), 56 deletions(-)
---
diff --git a/src/cocoa_menu_item.c b/src/cocoa_menu_item.c
index e08ab75..29e315c 100644
--- a/src/cocoa_menu_item.c
+++ b/src/cocoa_menu_item.c
@@ -157,12 +157,10 @@ cocoa_menu_item_update_submenu (GNSMenuItem *cocoa_item,
     cocoa_menu_connect (submenu, cocoa_submenu);
   }
   else { //no submenu anywhere, so create one
-    const gchar *label_text = get_menu_label_text (widget, &label);
-    if (label_text)
-      cocoa_submenu = [ [ NSMenu alloc ] initWithTitle:
-			[[ NSString alloc] initWithUTF8String:label_text]];
-    else
-      cocoa_submenu = [ [ NSMenu alloc ] initWithTitle:@""];
+    const gchar *text = get_menu_label_text (widget, &label);
+    NSString *title = [NSString stringWithUTF8String: text ? text : ""];
+
+    cocoa_submenu = [[[NSMenu alloc] initWithTitle: title] autorelease];
 
     [cocoa_submenu setAutoenablesItems:NO];
     cocoa_menu_connect (submenu, cocoa_submenu);
@@ -182,16 +180,14 @@ static void
 cocoa_menu_item_update_label (GNSMenuItem *cocoa_item,
 			      GtkWidget      *widget)
 {
-  const gchar *label_text;
-
   g_return_if_fail (cocoa_item != NULL);
   g_return_if_fail (widget != NULL);
 
-  label_text = get_menu_label_text (widget, NULL);
-  if (label_text)
-    [cocoa_item setTitle:[ [ NSString alloc] initWithCString:label_text encoding:NSUTF8StringEncoding]];
-  else
-    [cocoa_item setTitle:@""];
+  {
+    const gchar *text = get_menu_label_text (widget, NULL);
+    NSString *title = [NSString stringWithUTF8String: text ? text : ""];
+    [cocoa_item setTitle: title];
+  }
 }
 
 static void
@@ -476,21 +472,18 @@ cocoa_menu_item_add_item (NSMenu* cocoa_menu, GtkWidget* menu_item, int index)
     cocoa_item = (GNSMenuItem*)[GNSMenuItem separatorItem];
     DEBUG ("\ta separator\n");
   } else {
-    const gchar* label_text = get_menu_label_text (menu_item, &label);
+    const gchar* text = get_menu_label_text (menu_item, &label);
+    NSString *title = [NSString stringWithUTF8String:(text ? text : @"")];
+
     GClosure *menu_action =
       g_cclosure_new_object_swap(G_CALLBACK(gtk_menu_item_activate),
 				 G_OBJECT(menu_item));
     g_closure_set_marshal(menu_action, g_cclosure_marshal_VOID__VOID);
 
-    if (label_text)
-      cocoa_item = [ [ GNSMenuItem alloc]
-		     initWithTitle:[ [ NSString alloc]
-				     initWithCString:label_text
-				     encoding:NSUTF8StringEncoding]
-		     aGClosure:menu_action andPointer:NULL];
-    else
-      cocoa_item = [ [ GNSMenuItem alloc] initWithTitle:@""
-		     aGClosure:menu_action andPointer:NULL];
+    cocoa_item = [[GNSMenuItem alloc]
+                   initWithTitle: title
+                   aGClosure:menu_action andPointer:NULL];
+    
     DEBUG ("\tan item\n");
   }
   cocoa_menu_item_connect (menu_item, (GNSMenuItem*) cocoa_item, label);
diff --git a/src/gtk-mac-dock.c b/src/gtk-mac-dock.c
index f64cd2d..8398d53 100644
--- a/src/gtk-mac-dock.c
+++ b/src/gtk-mac-dock.c
@@ -316,7 +316,7 @@ gtk_mac_dock_set_icon_from_pixbuf (GtkMacDock *dock,
     {
       CGImageRef image;
 
-      image = gtk_mac_image_from_pixbuf (pixbuf);
+      image = gtk_create_cgimage_from_pixbuf (pixbuf);
       SetApplicationDockTileImage (image);
       CGImageRelease (image);
     }
@@ -361,7 +361,7 @@ gtk_mac_dock_set_overlay_from_pixbuf (GtkMacDock  *dock,
 
   if (pixbuf)
     {
-      image = gtk_mac_image_from_pixbuf (pixbuf);
+      image = gtk_create_cgimage_from_pixbuf (pixbuf);
       OverlayApplicationDockTileImage (image);
       CGImageRelease (image);
     }
diff --git a/src/gtk-mac-image-utils.c b/src/gtk-mac-image-utils.c
index f88aa63..c3f7b25 100644
--- a/src/gtk-mac-image-utils.c
+++ b/src/gtk-mac-image-utils.c
@@ -26,7 +26,7 @@
 #include "gtk-mac-image-utils.h"
 
 CGImageRef
-gtk_mac_image_from_pixbuf (GdkPixbuf *pixbuf)
+gtk_create_cgimage_from_pixbuf (GdkPixbuf *pixbuf)
 {
   CGColorSpaceRef   colorspace;
   CGDataProviderRef data_provider;
diff --git a/src/gtk-mac-image-utils.h b/src/gtk-mac-image-utils.h
index e92a656..ac0d62f 100644
--- a/src/gtk-mac-image-utils.h
+++ b/src/gtk-mac-image-utils.h
@@ -25,7 +25,7 @@
 
 G_BEGIN_DECLS
 
-CGImageRef gtk_mac_image_from_pixbuf (GdkPixbuf *pixbuf);
+CGImageRef gtk_create_cgimage_from_pixbuf (GdkPixbuf *pixbuf);
 
 G_END_DECLS
 
diff --git a/src/gtkosxapplication.h b/src/gtkosxapplication.h
index 684e68b..ba024f7 100644
--- a/src/gtkosxapplication.h
+++ b/src/gtkosxapplication.h
@@ -63,7 +63,6 @@ GtkosxApplication *gtkosx_application_get (void);
 
 //void gtkosx_application_init (GtkosxApplication *self);
 void gtkosx_application_ready (GtkosxApplication *self);
-void gtkosx_application_cleanup (GtkosxApplication *self);
 
 /*Accelerator functions*/
 
diff --git a/src/gtkosxapplication_quartz.c b/src/gtkosxapplication_quartz.c
index bfde43d..fbf3be0 100644
--- a/src/gtkosxapplication_quartz.c
+++ b/src/gtkosxapplication_quartz.c
@@ -177,9 +177,11 @@ create_apple_menu (GtkosxApplication *self)
 {
   NSMenuItem *menuitem;
   // Create the application (Apple) menu.
-  NSMenu *app_menu = [[NSMenu alloc] initWithTitle: @"Apple Menu"];
-
-  NSMenu *menuServices = [[NSMenu alloc] initWithTitle:  NSLocalizedStringFromTable(@"Services",  @"GtkosxApplication", @"Services Menu title")];
+  NSMenu *app_menu = [[[NSMenu alloc] initWithTitle: @"Apple Menu"] autorelease];
+  NSString *title = NSLocalizedStringFromTable(@"Services",
+                                               @"GtkOSXApplication",
+                                               @"Services Menu title");
+  NSMenu *menuServices = [[[NSMenu alloc] initWithTitle: title] autorelease];
   [NSApp setServicesMenu:menuServices];
 
   [app_menu addItem: [NSMenuItem separatorItem]];
@@ -233,8 +235,11 @@ create_apple_menu (GtkosxApplication *self)
  */
 static GNSMenuItem *
 create_window_menu (GtkosxApplication *self)
-{   
-  NSMenu *window_menu = [[NSMenu alloc] initWithTitle: NSLocalizedStringFromTable(@"Window",  @"GtkosxApplication", @"Window Menu title")];
+{
+  NSString *title = NSLocalizedStringFromTable(@"Window",
+                                               @"GtkOSXApplication",
+                                               @"Window Menu title");
+  NSMenu *window_menu = [[[NSMenu alloc] initWithTitle: title] autorelease];
   GtkMenuBar *menubar = [(GNSMenuBar*)[NSApp mainMenu] menuBar];
   GtkWidget *parent = NULL;
   GdkWindow *win = NULL;
@@ -652,8 +657,8 @@ gtkosx_application_set_menu_bar (GtkosxApplication *self, GtkMenuShell *menu_she
 
   cocoa_menubar = (GNSMenuBar*)cocoa_menu_get(GTK_WIDGET (menu_shell));
   if (!cocoa_menubar) {
-    cocoa_menubar = [[GNSMenuBar alloc] initWithGtkMenuBar: 
-		     GTK_MENU_BAR(menu_shell)];
+    cocoa_menubar = [[[GNSMenuBar alloc] initWithGtkMenuBar: 
+                      GTK_MENU_BAR(menu_shell)] autorelease];
     cocoa_menu_connect(GTK_WIDGET (menu_shell), cocoa_menubar);
   /* turn off auto-enabling for the menu - its silly and slow and
      doesn't really make sense for a Gtk/Cocoa hybrid menu.
@@ -758,7 +763,6 @@ gtkosx_application_set_window_menu(GtkosxApplication *self,
     GtkWidget *parent = NULL;
     GdkWindow *win = NULL;
     NSWindow *nswin = NULL;
-    GtkMenuBar *menubar = [cocoa_menubar menuBar];
     GNSMenuItem *cocoa_item = cocoa_menu_item_get(GTK_WIDGET(menu_item));
      g_return_if_fail(cocoa_item != NULL);
     [cocoa_menubar setWindowsMenu: cocoa_item];
@@ -796,8 +800,9 @@ gtkosx_application_set_help_menu (GtkosxApplication *self,
     [cocoa_menubar setHelpMenu: cocoa_item];
   }
   else {
-    [cocoa_menubar setHelpMenu: [[GNSMenuItem alloc] initWithTitle: @"Help"
-				 action: NULL keyEquivalent: @""]];
+    GNSMenuItem *menuitem = [[[GNSMenuItem alloc] initWithTitle: @"Help"
+                              action: NULL keyEquivalent: @""] autorelease];
+    [cocoa_menubar setHelpMenu: menuitem];
     [cocoa_menubar addItem: [cocoa_menubar helpMenu]];
   }
 }
@@ -862,26 +867,19 @@ static NSImage*
 nsimage_from_resource(const gchar *name, const gchar* type, const gchar* subdir)
 {
   NSString *ns_name, *ns_type, *ns_subdir, *path;
-  NSImage *image;
+  NSImage *image = NULL; 
   g_return_val_if_fail(name != NULL, NULL);
   g_return_val_if_fail(type != NULL, NULL);
   g_return_val_if_fail(subdir != NULL, NULL);
+
   ns_name = [NSString stringWithUTF8String: name];
   ns_type = [NSString stringWithUTF8String: type];
   ns_subdir = [NSString stringWithUTF8String: subdir];
   path = [[NSApp mainBundle] pathForResource: ns_name
 		     ofType: ns_type inDirectory: ns_subdir];
-  if (!path) {
-    [ns_name release];
-    [ns_type release];
-    [ns_subdir release];
-    return NULL;
-  }
+  if (path) 
   image = [[[NSImage alloc] initWithContentsOfFile: path] autorelease];
-  [ns_name release];
-  [ns_type release];
-  [ns_subdir release];
-  [path release];
+
   return image;
 }
 
@@ -903,7 +901,7 @@ nsimage_from_pixbuf(GdkPixbuf *pixbuf)
   NSImage* newImage = nil;
 
   g_return_val_if_fail (pixbuf !=  NULL, NULL);
-  image = gtk_mac_image_from_pixbuf (pixbuf);
+  image = gtk_create_cgimage_from_pixbuf (pixbuf);
   // Get the image dimensions.
   imageRect.size.height = CGImageGetHeight(image);
   imageRect.size.width = CGImageGetWidth(image);
@@ -1096,16 +1094,15 @@ gtkosx_application_get_executable_path(void)
 gchar*
 gtkosx_application_get_bundle_info(const gchar *key)
 {
+  gchar *result = NULL;
   NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
   NSObject *id = [[NSBundle mainBundle] objectForInfoDictionaryKey:
 		  [NSString stringWithUTF8String: key]];
+
   if ([id respondsToSelector: @selector(UTF8String)]) {
-    gchar *str = g_strdup( [(NSString*)id UTF8String]);
-    [id release];
-    [pool release];
-    return str;
+    result = g_strdup( [(NSString*)id UTF8String]);
   }
-  [id release];
+
   [pool release];
-  return NULL;
+  return result;
 }



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