Fix memory leaks in Mac OS X CoreWLAN code. b=548796 r=smichaud a=beltzner
authorJosh Aas <joshmoz@gmail.com>
Wed, 10 Mar 2010 15:48:33 -0500
changeset 26838 bccec907649a908f567853ace33352da7f2d6b9f
parent 26837 a0091c234b2b02955061bfe9042c5dbba95ca700
child 26839 0d8bde830a1a071e0a8c4484416ab2831a3a6402
push id2311
push userjosh@mozilla.com
push dateWed, 10 Mar 2010 20:49:07 +0000
reviewerssmichaud, beltzner
bugs548796
milestone1.9.1.9pre
Fix memory leaks in Mac OS X CoreWLAN code. b=548796 r=smichaud a=beltzner
netwerk/wifi/src/osx_corewlan.mm
--- a/netwerk/wifi/src/osx_corewlan.mm
+++ b/netwerk/wifi/src/osx_corewlan.mm
@@ -8,17 +8,17 @@
  *
  * Software distributed under the License is distributed on an "AS IS" basis,
  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  * for the specific language governing rights and limitations under the
  * License.
  *
  * The Original Code is Geolocation.
  *
- * The Initial Developer of the Original Code is Mozilla Corporation
+ * The Initial Developer of the Original Code is Mozilla Foundation
  * Portions created by the Initial Developer are Copyright (C) 2009
  * the Initial Developer. All Rights Reserved.
  *
  * Contributor(s):
  *  Doug Turner <dougt@meer.net>  (Original Author)
  *
  * Alternatively, the contents of this file may be used under the terms of
  * either the GNU General Public License Version 2 or later (the "GPL"), or
@@ -38,78 +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];
 
-  void *corewlan_library = dlopen("/System/Library/Frameworks/CoreWLAN.framework/CoreWLAN",
-                                  RTLD_LAZY);
-  if (!corewlan_library)
-    return NS_ERROR_NOT_AVAILABLE;
-  
-  accessPoints.Clear();
-  
-  id anObject;
-  NSError *err = nil;
-  NSDictionary *params = nil;
+  @try {
+    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) {
+      [pool release];
+      return NS_ERROR_NOT_AVAILABLE;
+    }
+
+    id scanResult = [[CWI_class interface] scanForNetworksWithParameters:nil error:nil];
+    if (!scanResult) {
+      [pool release];
+      return NS_ERROR_NOT_AVAILABLE;
+    }
 
-  // We do this the hard way because we want to be able to run on pre-10.6.  When we drop
-  // this requirement, this objective-c magic goes away.
+    NSArray* scan = [NSMutableArray arrayWithArray:scanResult];
+    NSEnumerator *enumerator = [scan objectEnumerator];
 
-  // dynamically call: [CWInterface interface];
-  Class CWI_class = objc_getClass("CWInterface");
-  if (!CWI_class) {
-    dlclose(corewlan_library);
+    while (id anObject = [enumerator nextObject]) {
+      nsWifiAccessPoint* ap = new nsWifiAccessPoint();
+      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;
   }
 
-  SEL interfaceSel = sel_registerName("interface");
-  id interface = objc_msgSend(CWI_class, interfaceSel);
-
-  // call [interface scanForNetworksWithParameters:params err:&err]
-  SEL scanSel = sel_registerName("scanForNetworksWithParameters:error:");
-  id scanResult = objc_msgSend(interface, scanSel, params, err);
+  [pool release];
 
-  NSArray* scan = [NSMutableArray arrayWithArray:scanResult];
-  NSEnumerator *enumerator = [scan objectEnumerator];
-  
-  while (anObject = [enumerator nextObject]) {
- 
-    nsWifiAccessPoint* ap = new nsWifiAccessPoint();
-    if (!ap)
-      continue;
- 
+  return NS_OK;
 
-    NSData* data = [anObject bssidData];
-    ap->setMac((unsigned char*)[data bytes]);
-    ap->setSignal([[anObject rssi] intValue]);
-    ap->setSSID([[anObject ssid] UTF8String], 32);
-    
-    accessPoints.AppendObject(ap);
-  }
-
-  dlclose(corewlan_library);
-  return NS_OK;
+  NS_OBJC_END_TRY_ABORT_BLOCK_RETURN(NS_ERROR_NOT_AVAILABLE);
 }