Fix memory leaks in Mac OS X CoreWLAN code. b=548796 r=smichaud
authorJosh Aas <joshmoz@gmail.com>
Fri, 26 Feb 2010 14:18:12 -0600
changeset 38766 1e30b2e41326c982ca849f751dc4c633d30dfc13
parent 38765 3e17491d8e321b73c466a2476f2cf75315e31045
child 38772 9462bbed38326ea35fdf4b9e2cb7e551dd07f5d9
push idunknown
push userunknown
push dateunknown
reviewerssmichaud
bugs548796
milestone1.9.3a2pre
Fix memory leaks in Mac OS X CoreWLAN code. b=548796 r=smichaud
netwerk/wifi/src/osx_corewlan.mm
--- a/netwerk/wifi/src/osx_corewlan.mm
+++ b/netwerk/wifi/src/osx_corewlan.mm
@@ -38,67 +38,81 @@
 
 #include <mach-o/dyld.h>
 #include <dlfcn.h>
 #include <unistd.h>
 
 #include <objc/objc.h>
 #include <objc/objc-runtime.h>
 
+#include "nsObjCExceptions.h"
 #include "nsAutoPtr.h"
 #include "nsCOMArray.h"
 #include "nsWifiMonitor.h"
 #include "nsWifiAccessPoint.h"
 
 BOOL UsingSnowLeopard() {
   static PRInt32 gOSXVersion = 0x0;
   if (gOSXVersion == 0x0) {
     Gestalt(gestaltSystemVersion, (SInt32*)&gOSXVersion);
   }
   return (gOSXVersion >= 0x00001060);
 }
 
 nsresult
 GetAccessPointsFromWLAN(nsCOMArray<nsWifiAccessPoint> &accessPoints)
 {
+  NS_OBJC_BEGIN_TRY_ABORT_BLOCK_RETURN;
+
   if (!UsingSnowLeopard())
     return NS_ERROR_NOT_AVAILABLE;
 
   accessPoints.Clear();
 
   NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
+
   @try {
-
-    NSBundle * bundle = [[NSBundle alloc] initWithPath:@"/System/Library/Frameworks/CoreWLAN.framework"];
-    if (!bundle)
+    NSBundle * bundle = [[[NSBundle alloc] initWithPath:@"/System/Library/Frameworks/CoreWLAN.framework"] autorelease];
+    if (!bundle) {
+      [pool release];
       return NS_ERROR_NOT_AVAILABLE;
+    }
 
     Class CWI_class = [bundle classNamed:@"CWInterface"];
-    if (!CWI_class)
+    if (!CWI_class) {
+      [pool release];
       return NS_ERROR_NOT_AVAILABLE;
+    }
 
-    NSDictionary *params = nil;
-    NSError *err = nil;
-    id scanResult = [[CWI_class interface] scanForNetworksWithParameters: params error: err];
-
-    if (!scanResult)
+    id scanResult = [[CWI_class interface] scanForNetworksWithParameters:nil error:nil];
+    if (!scanResult) {
+      [pool release];
       return NS_ERROR_NOT_AVAILABLE;
+    }
 
     NSArray* scan = [NSMutableArray arrayWithArray:scanResult];
     NSEnumerator *enumerator = [scan objectEnumerator];
 
     while (id anObject = [enumerator nextObject]) {
       nsWifiAccessPoint* ap = new nsWifiAccessPoint();
-      if (!ap)
+      if (!ap) {
+        [pool release];
         return NS_ERROR_OUT_OF_MEMORY;
+      }
       NSData* data = [anObject bssidData];
       ap->setMac((unsigned char*)[data bytes]);
       ap->setSignal([[anObject rssi] intValue]);
       ap->setSSID([[anObject ssid] UTF8String], 32);
 
       accessPoints.AppendObject(ap);
     }
   }
   @catch(NSException *_exn) {
+    [pool release];
     return NS_ERROR_NOT_AVAILABLE;
   }
+
+  [pool release];
+
   return NS_OK;
+
+  NS_OBJC_END_TRY_ABORT_BLOCK_RETURN(NS_ERROR_NOT_AVAILABLE);
 }