From a166d446d6cbdbc51490666d65c847f90a6f5309 Mon Sep 17 00:00:00 2001
From: Rye Mutt <rye@alchemyviewer.org>
Date: Mon, 2 Aug 2021 23:36:19 -0400
Subject: [PATCH] Convert to use ObjC ARC

---
 indra/cmake/Variables.cmake              |   1 +
 indra/llplugin/slplugin/slplugin-objc.h  |   2 -
 indra/llplugin/slplugin/slplugin-objc.mm |  49 +++-----
 indra/llplugin/slplugin/slplugin.cpp     |  23 +---
 indra/llwindow/llappdelegate-objc.h      |   6 +-
 indra/llwindow/llopenglview-objc.mm      |  31 +++--
 indra/llwindow/llwindowmacosx-objc.h     |  16 ++-
 indra/llwindow/llwindowmacosx-objc.mm    | 150 +++++++++++------------
 indra/newview/llappdelegate-objc.mm      |  10 +-
 indra/newview/llappviewermacosx-objc.mm  |  12 +-
 indra/newview/llfilepicker_mac.mm        |   2 +-
 11 files changed, 126 insertions(+), 176 deletions(-)

diff --git a/indra/cmake/Variables.cmake b/indra/cmake/Variables.cmake
index 502b6be83cb..0eb7349eda1 100644
--- a/indra/cmake/Variables.cmake
+++ b/indra/cmake/Variables.cmake
@@ -181,6 +181,7 @@ if (${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
   set(CMAKE_XCODE_ATTRIBUTE_CLANG_CXX_LIBRARY "libc++")
 
   # Obj-C
+  set(CMAKE_XCODE_ATTRIBUTE_CLANG_ENABLE_OBJC_ARC YES)
   set(CMAKE_XCODE_ATTRIBUTE_CLANG_ENABLE_OBJC_WEAK YES)
 
   # Disable codesigning, for now it's handled with snake
diff --git a/indra/llplugin/slplugin/slplugin-objc.h b/indra/llplugin/slplugin/slplugin-objc.h
index af0ebe1af2a..0d48739d3b8 100644
--- a/indra/llplugin/slplugin/slplugin-objc.h
+++ b/indra/llplugin/slplugin/slplugin-objc.h
@@ -40,8 +40,6 @@ class LLCocoaPlugin
 public:  
     LLCocoaPlugin();
     void setupCocoa();
-    void createAutoReleasePool();
-    void deleteAutoReleasePool();
     void setupGroup();
     void updateWindows();
     void processEvents();
diff --git a/indra/llplugin/slplugin/slplugin-objc.mm b/indra/llplugin/slplugin/slplugin-objc.mm
index b919edac09e..1c4eb31bdce 100644
--- a/indra/llplugin/slplugin/slplugin-objc.mm
+++ b/indra/llplugin/slplugin/slplugin-objc.mm
@@ -42,46 +42,25 @@ void LLCocoaPlugin::setupCocoa()
 	
 	if(!inited)
 	{
-		createAutoReleasePool();
-		
-		// The following prevents the Cocoa command line parser from trying to open 'unknown' arguements as documents.
-		// ie. running './secondlife -set Language fr' would cause a pop-up saying can't open document 'fr' 
-		// when init'ing the Cocoa App window.		
-		[[NSUserDefaults standardUserDefaults] setObject:@"NO" forKey:@"NSTreatUnknownArgumentsAsOpen"];
-		
-		//	Needed for Carbon based applications which call into Cocoa
-		NSApplicationLoad();
-
-		//	Must first call [[[NSWindow alloc] init] release] to get the NSWindow machinery set up so that NSCursor can use a window to cache the cursor image
-		[[[NSWindow alloc] init] release];
-		
-        mPluginWindow = [NSApp mainWindow];
-        
-		deleteAutoReleasePool();
+        @autoreleasepool {
+            // The following prevents the Cocoa command line parser from trying to open 'unknown' arguements as documents.
+            // ie. running './secondlife -set Language fr' would cause a pop-up saying can't open document 'fr'
+            // when init'ing the Cocoa App window.
+            [[NSUserDefaults standardUserDefaults] setObject:@"NO" forKey:@"NSTreatUnknownArgumentsAsOpen"];
+            
+            //    Needed for Carbon based applications which call into Cocoa
+            NSApplicationLoad();
+            
+            //    Must first call [[[NSWindow alloc] init] release] to get the NSWindow machinery set up so that NSCursor can use a window to cache the cursor image
+            (void)[[NSWindow alloc] init];
+            
+            mPluginWindow = [NSApp mainWindow];
+        }
 		
 		inited = true;
 	}
 }
 
-static NSAutoreleasePool *sPool = NULL;
-
-void LLCocoaPlugin::createAutoReleasePool()
-{
-	if(!sPool)
-	{
-		sPool = [[NSAutoreleasePool alloc] init];
-	}
-}
-
-void LLCocoaPlugin::deleteAutoReleasePool()
-{
-	if(sPool)
-	{
-		[sPool release];
-		sPool = NULL;
-	}
-}
-
 LLCocoaPlugin::LLCocoaPlugin():mHackState(0)
 {
     NSArray* window_list = [NSApp orderedWindows];
diff --git a/indra/llplugin/slplugin/slplugin.cpp b/indra/llplugin/slplugin/slplugin.cpp
index 5c54705c718..259860160d0 100644
--- a/indra/llplugin/slplugin/slplugin.cpp
+++ b/indra/llplugin/slplugin/slplugin.cpp
@@ -206,17 +206,12 @@ int main(int argc, char **argv)
 
     LLCocoaPlugin cocoa_interface;
 	cocoa_interface.setupCocoa();
-	cocoa_interface.createAutoReleasePool();
 #endif //LL_DARWIN
 
 	LLPluginProcessChild *plugin = new LLPluginProcessChild();
 
 	plugin->init(port);
 
-#if LL_DARWIN
-    cocoa_interface.deleteAutoReleasePool();
-#endif
-
 	LLTimer timer;
 	timer.start();
 
@@ -232,9 +227,6 @@ int main(int argc, char **argv)
 #endif
 	while(!plugin->isDone())
 	{
-#if LL_DARWIN
-		cocoa_interface.createAutoReleasePool();
-#endif
 		timer.reset();
 		plugin->idle();
 #if LL_DARWIN
@@ -245,7 +237,7 @@ int main(int argc, char **argv)
 		F64 elapsed = timer.getElapsedTimeF64();
 		F64 remaining = plugin->getSleepTime() - elapsed;
 
-		if(remaining <= 0.0f)
+		if(remaining <= 0.0)
 		{
 			// We've already used our full allotment.
 //			LL_INFOS("slplugin") << "elapsed = " << elapsed * 1000.0f << " ms, remaining = " << remaining * 1000.0f << " ms, not sleeping" << LL_ENDL;
@@ -264,19 +256,6 @@ int main(int argc, char **argv)
 
 //			LL_INFOS("slplugin") << "slept for "<< timer.getElapsedTimeF64() * 1000.0f << " ms" <<  LL_ENDL;
 		}
-        
-        
-#if LL_WINDOWS
-	// More agressive checking of interfering exception handlers.
-	// Doesn't appear to be required so far - even for plugins
-	// that do crash with a single call to the intercept
-	// exception handler such as QuickTime.
-	//checkExceptionHandler();
-#endif
-
-#if LL_DARWIN
-		cocoa_interface.deleteAutoReleasePool();
-#endif
 	}
 	delete plugin;
 
diff --git a/indra/llwindow/llappdelegate-objc.h b/indra/llwindow/llappdelegate-objc.h
index a3f5f1e20e5..e954f0b5ab1 100644
--- a/indra/llwindow/llappdelegate-objc.h
+++ b/indra/llwindow/llappdelegate-objc.h
@@ -31,9 +31,9 @@
 @end
 
 @interface LLAppDelegate : NSObject <NSApplicationDelegate> {
-	LLNSWindow *window;
-	NSWindow *inputWindow;
-	LLNonInlineTextView *inputView;
+	LLNSWindow __unsafe_unretained *window;
+	NSWindow __unsafe_unretained *inputWindow;
+	LLNonInlineTextView __unsafe_unretained *inputView;
 	NSTimer *frameTimer;
 	NSString *currentInputLanguage;
 }
diff --git a/indra/llwindow/llopenglview-objc.mm b/indra/llwindow/llopenglview-objc.mm
index 4b22489b29f..2db78156981 100644
--- a/indra/llwindow/llopenglview-objc.mm
+++ b/indra/llwindow/llopenglview-objc.mm
@@ -27,6 +27,7 @@
 #import "llopenglview-objc.h"
 #import "llwindowmacosx-objc.h"
 #import "llappdelegate-objc.h"
+#import <Carbon/Carbon.h> // for keycodes
 
 extern BOOL gHiDPISupport;
 
@@ -111,7 +112,7 @@ attributedStringInfo getSegments(NSAttributedString *str)
 - (unsigned long)getVramSize
 {
     CGLRendererInfoObj info = 0;
-	GLint vram_megabytes = 0;
+    GLint vram_megabytes = 0;
     int num_renderers = 0;
     CGLError the_err = CGLQueryRendererInfo (CGDisplayIDToOpenGLDisplayMask(kCGDirectMainDisplay), &info, &num_renderers);
     if(0 == the_err)
@@ -124,7 +125,7 @@ attributedStringInfo getSegments(NSAttributedString *str)
         vram_megabytes = 256;
     }
     
-	return (unsigned long)vram_megabytes; // return value is in megabytes.
+    return (unsigned long)vram_megabytes; // return value is in megabytes.
 }
 
 - (void)viewDidMoveToWindow
@@ -150,7 +151,7 @@ attributedStringInfo getSegments(NSAttributedString *str)
     
     NSRect rect = [[self window] frame];
     NSRect scaled_rect = [self convertRectToBacking:rect];
-    if (NSEqualSizes(rect.size, scaled_rect.size))
+    if (!NSEqualSizes(rect.size, scaled_rect.size))
     {
         callResize(scaled_rect.size.width, scaled_rect.size.height);
     }
@@ -189,7 +190,6 @@ attributedStringInfo getSegments(NSAttributedString *str)
 - (void)dealloc
 {
 	[[NSNotificationCenter defaultCenter] removeObserver:self];
-	[super dealloc];
 }
 
 - (id) init
@@ -232,7 +232,7 @@ attributedStringInfo getSegments(NSAttributedString *str)
             0
         };
         
-        NSOpenGLPixelFormat *pixelFormat = [[[NSOpenGLPixelFormat alloc] initWithAttributes:attrs] autorelease];
+        NSOpenGLPixelFormat *pixelFormat = [[NSOpenGLPixelFormat alloc] initWithAttributes:attrs];
         
         if (pixelFormat == nil)
         {
@@ -240,7 +240,7 @@ attributedStringInfo getSegments(NSAttributedString *str)
             return nil;
         }
         
-        NSOpenGLContext *glContext = [[[NSOpenGLContext alloc] initWithFormat:pixelFormat shareContext:nil] autorelease];
+        NSOpenGLContext *glContext = [[NSOpenGLContext alloc] initWithFormat:pixelFormat shareContext:nil];
         
         if (glContext == nil)
         {
@@ -262,10 +262,8 @@ attributedStringInfo getSegments(NSAttributedString *str)
         
         GLint glVsync = vsync ? 1 : 0;
         [glContext setValues:&glVsync forParameter:NSOpenGLCPSwapInterval];
-
-    }
-
-	return self;
+    } // @autoreleasepool
+    return self;
 }
 
 - (BOOL) rebuildContext
@@ -279,7 +277,7 @@ attributedStringInfo getSegments(NSAttributedString *str)
         NSOpenGLContext *ctx = [self openGLContext];
         
         [ctx clearDrawable];
-        ctx = [[[NSOpenGLContext alloc] initWithFormat:format shareContext:nil] autorelease];
+        ctx = [[NSOpenGLContext alloc] initWithFormat:format shareContext:nil];
         
         if (ctx == nil)
         {
@@ -290,8 +288,7 @@ attributedStringInfo getSegments(NSAttributedString *str)
         [self setOpenGLContext:ctx];
         [ctx setView:self];
         [ctx makeCurrentContext];
-    }
-
+    } // @autoreleasepool
 	return true;
 }
 
@@ -477,13 +474,13 @@ attributedStringInfo getSegments(NSAttributedString *str)
     NSInteger mask = 0;
     switch([theEvent keyCode])
     {        
-        case 56:
+        case kVK_Shift:
             mask = NSEventModifierFlagShift;
             break;
-        case 58:
+        case kVK_Option:
             mask = NSEventModifierFlagOption;
             break;
-        case 59:
+        case kVK_Control:
             mask = NSEventModifierFlagControl;
             break;
         default:
@@ -678,7 +675,7 @@ attributedStringInfo getSegments(NSAttributedString *str)
 		// We may never get this point since unmarkText may be called before insertText ever gets called once we submit our text.
 		// But just in case...
 		
-		for (NSInteger i = 0; i < [aString length]; i++)
+		for (NSInteger i = 0; i < [aString length]; ++i)
 		{
 			handleUnicodeCharacter([aString characterAtIndex:i]);
 		}
diff --git a/indra/llwindow/llwindowmacosx-objc.h b/indra/llwindow/llwindowmacosx-objc.h
index d75d5b9c27d..5fd6fe20e8a 100644
--- a/indra/llwindow/llwindowmacosx-objc.h
+++ b/indra/llwindow/llwindowmacosx-objc.h
@@ -29,11 +29,18 @@
 #define LL_LLWINDOWMACOSX_OBJC_H
 
 #include <map>
+#include <string>
 #include <vector>
+#include <OpenGL/CGLTypes.h>
 
 //for CGSize
 #include <CoreGraphics/CGGeometry.h>
 
+// This will actually hold an NSCursor*, but that type is only available in objective C.
+typedef void *CursorRef;
+typedef void *NSWindowRef;
+typedef void *GLViewRef;
+
 typedef std::vector<std::pair<int, bool> > segment_t;
 
 typedef std::vector<int> segment_lengths;
@@ -44,12 +51,6 @@ struct attributedStringInfo {
 	segment_standouts seg_standouts;
 };
 
-// This will actually hold an NSCursor*, but that type is only available in objective C.
-typedef void *CursorRef;
-typedef void *NSWindowRef;
-typedef void *GLViewRef;
-
-
 struct NativeKeyEventData {
     enum EventType {
         KEYUNKNOWN,
@@ -101,8 +102,6 @@ long showAlert(std::string title, std::string text, int type);
 
 NSWindowRef createNSWindow(int x, int y, int width, int height);
 
-#include <OpenGL/OpenGL.h>
-
 GLViewRef createOpenGLView(NSWindowRef window, unsigned int samples, bool vsync);
 void glSwapBuffers(void* context);
 CGLContextObj getCGLContextObj(GLViewRef view);
@@ -153,7 +152,6 @@ void callModifier(unsigned int mask);
 void callQuitHandler();
 void commitCurrentPreedit(GLViewRef glView);
 
-#include <string>
 void callHandleDragEntered(std::string url);
 void callHandleDragExited(std::string url);
 void callHandleDragUpdated(std::string url);
diff --git a/indra/llwindow/llwindowmacosx-objc.mm b/indra/llwindow/llwindowmacosx-objc.mm
index 61c2ff71bcf..e2253dd29c2 100644
--- a/indra/llwindow/llwindowmacosx-objc.mm
+++ b/indra/llwindow/llwindowmacosx-objc.mm
@@ -25,11 +25,12 @@
  * $/LicenseInfo$
  */
 
-#include <AppKit/AppKit.h>
-#include <Cocoa/Cocoa.h>
-#include "llopenglview-objc.h"
-#include "llwindowmacosx-objc.h"
-#include "llappdelegate-objc.h"
+#import "llopenglview-objc.h"
+#import "llwindowmacosx-objc.h"
+#import "llappdelegate-objc.h"
+
+#import <AppKit/AppKit.h>
+#import <Cocoa/Cocoa.h>
 
 /*
  * These functions are broken out into a separate file because the
@@ -54,7 +55,7 @@ void setupCocoa()
             // ie. running './secondlife -set Language fr' would cause a pop-up saying can't open document 'fr'
             // when init'ing the Cocoa App window.
             [[NSUserDefaults standardUserDefaults] setObject:@"NO" forKey:@"NSTreatUnknownArgumentsAsOpen"];
-        }
+        } // @autoreleasepool
 
 		inited = true;
 	}
@@ -66,7 +67,7 @@ bool copyToPBoard(const unsigned short *str, unsigned int len)
         NSPasteboard *pboard = [NSPasteboard generalPasteboard];
         [pboard clearContents];
         
-        NSArray *contentsToPaste = [[[NSArray alloc] initWithObjects:[NSString stringWithCharacters:str length:len], nil] autorelease];
+        NSArray *contentsToPaste = [[NSArray alloc] initWithObjects:[NSString stringWithCharacters:str length:len], nil];
         BOOL ret = [pboard writeObjects:contentsToPaste];
         return ret;
     }
@@ -103,15 +104,15 @@ CursorRef createImageCursor(const char *fullpath, int hotspotX, int hotspotY)
     @autoreleasepool {
         // extra retain on the NSCursor since we want it to live for the lifetime of the app.
         NSCursor *cursor =
-        [[[NSCursor alloc]
+        [[NSCursor alloc]
           initWithImage:
-          [[[NSImage alloc] initWithContentsOfFile:
+          [[NSImage alloc] initWithContentsOfFile:
             [NSString stringWithFormat:@"%s", fullpath]
-            ]autorelease]
+            ]
           hotSpot:NSMakePoint(hotspotX, hotspotY)
-          ] retain];
+          ];
         
-        return (CursorRef)cursor;
+        return (CursorRef)CFBridgingRetain(cursor);
     }
 }
 
@@ -173,9 +174,8 @@ OSErr releaseImageCursor(CursorRef ref)
 	if( ref != NULL )
 	{
         @autoreleasepool {
-            NSCursor *cursor = (NSCursor*)ref;
-            [cursor release];
-        }
+            CFBridgingRelease(ref);
+        } // @autoreleasepool
 	}
 	else
 	{
@@ -189,10 +189,8 @@ OSErr setImageCursor(CursorRef ref)
 {
 	if( ref != NULL )
 	{
-        @autoreleasepool {
-            NSCursor *cursor = (NSCursor*)ref;
-            [cursor set];
-        }
+        NSCursor *cursor = (__bridge NSCursor*)ref;
+        [cursor set];
 	}
 	else
 	{
@@ -211,53 +209,53 @@ NSWindowRef createNSWindow(int x, int y, int width, int height)
                                                       styleMask:NSWindowStyleMaskTitled | NSWindowStyleMaskResizable | NSWindowStyleMaskClosable | NSWindowStyleMaskMiniaturizable | NSWindowStyleMaskTexturedBackground backing:NSBackingStoreBuffered defer:NO];
 	[window makeKeyAndOrderFront:nil];
 	[window setAcceptsMouseMovedEvents:TRUE];
-	return window;
+	return (NSWindowRef)CFBridgingRetain(window);
 }
 
 GLViewRef createOpenGLView(NSWindowRef window, unsigned int samples, bool vsync)
 {
-	LLOpenGLView *glview = [[LLOpenGLView alloc]initWithFrame:[(LLNSWindow*)window frame] withSamples:samples andVsync:vsync];
-	[(LLNSWindow*)window setContentView:glview];
-	return glview;
+	LLOpenGLView *glview = [[LLOpenGLView alloc]initWithFrame:[(__bridge LLNSWindow*)window frame] withSamples:samples andVsync:vsync];
+	[(__bridge LLNSWindow*)window setContentView:glview];
+	return (GLViewRef)CFBridgingRetain(glview);
 }
 
 void glSwapBuffers(void* context)
 {
-	[(NSOpenGLContext*)context flushBuffer];
+	[(__bridge NSOpenGLContext*)context flushBuffer];
 }
 
 CGLContextObj getCGLContextObj(GLViewRef view)
 {
-	return [(LLOpenGLView *)view getCGLContextObj];
+	return [(__bridge LLOpenGLView *)view getCGLContextObj];
 }
 
 CGLPixelFormatObj* getCGLPixelFormatObj(NSWindowRef window)
 {
-	LLOpenGLView *glview = [(LLNSWindow*)window contentView];
+	LLOpenGLView *glview = [(__bridge LLNSWindow*)window contentView];
 	return [glview getCGLPixelFormatObj];
 }
 
 unsigned long getVramSize(GLViewRef view)
 {
-	return [(LLOpenGLView *)view getVramSize];
+	return [(__bridge LLOpenGLView *)view getVramSize];
 }
 
 float getDeviceUnitSize(GLViewRef view)
 {
-	return [(LLOpenGLView*)view convertSizeToBacking:NSMakeSize(1, 1)].width;
+	return [(__bridge LLOpenGLView*)view convertSizeToBacking:NSMakeSize(1, 1)].width;
 }
 
 void getContentViewBounds(NSWindowRef window, float* bounds)
 {
-	bounds[0] = [[(LLNSWindow*)window contentView] bounds].origin.x;
-	bounds[1] = [[(LLNSWindow*)window contentView] bounds].origin.y;
-	bounds[2] = [[(LLNSWindow*)window contentView] bounds].size.width;
-	bounds[3] = [[(LLNSWindow*)window contentView] bounds].size.height;
+	bounds[0] = [[(__bridge LLNSWindow*)window contentView] bounds].origin.x;
+	bounds[1] = [[(__bridge LLNSWindow*)window contentView] bounds].origin.y;
+	bounds[2] = [[(__bridge LLNSWindow*)window contentView] bounds].size.width;
+	bounds[3] = [[(__bridge LLNSWindow*)window contentView] bounds].size.height;
 }
 
 void getScaledContentViewBounds(NSWindowRef window, GLViewRef view, float* bounds)
 {
-    NSRect b = [(NSOpenGLView*)view convertRectToBacking:[[(LLNSWindow*)window contentView] bounds]];
+    NSRect b = [(__bridge NSOpenGLView*)view convertRectToBacking:[[(__bridge LLNSWindow*)window contentView] bounds]];
 	bounds[0] = b.origin.x;
 	bounds[1] = b.origin.y;
 	bounds[2] = b.size.width;
@@ -266,7 +264,7 @@ void getScaledContentViewBounds(NSWindowRef window, GLViewRef view, float* bound
 
 void getWindowSize(NSWindowRef window, float* size)
 {
-	NSRect frame = [(LLNSWindow*)window frame];
+	NSRect frame = [(__bridge LLNSWindow*)window frame];
 	size[0] = frame.origin.x;
 	size[1] = frame.origin.y;
 	size[2] = frame.size.width;
@@ -275,10 +273,10 @@ void getWindowSize(NSWindowRef window, float* size)
 
 void setWindowSize(NSWindowRef window, int width, int height)
 {
-	NSRect frame = [(LLNSWindow*)window frame];
+	NSRect frame = [(__bridge LLNSWindow*)window frame];
 	frame.size.width = width;
 	frame.size.height = height;
-	[(LLNSWindow*)window setFrame:frame display:TRUE];
+	[(__bridge LLNSWindow*)window setFrame:frame display:TRUE];
 }
 
 void setWindowPos(NSWindowRef window, float* pos)
@@ -286,26 +284,26 @@ void setWindowPos(NSWindowRef window, float* pos)
 	NSPoint point;
 	point.x = pos[0];
 	point.y = pos[1];
-	[(LLNSWindow*)window setFrameOrigin:point];
+	[(__bridge LLNSWindow*)window setFrameOrigin:point];
 }
 
 void getCursorPos(NSWindowRef window, float* pos)
 {
 	NSPoint mLoc;
-	mLoc = [(LLNSWindow*)window mouseLocationOutsideOfEventStream];
+	mLoc = [(__bridge LLNSWindow*)window mouseLocationOutsideOfEventStream];
 	pos[0] = mLoc.x;
 	pos[1] = mLoc.y;
 }
 
 void makeWindowOrderFront(NSWindowRef window)
 {
-	[(LLNSWindow*)window makeKeyAndOrderFront:nil];
+	[(__bridge LLNSWindow*)window makeKeyAndOrderFront:nil];
 }
 
 void convertScreenToWindow(NSWindowRef window, float *coord)
 {
 	NSRect point = NSMakeRect(coord[0], coord[1], 0, 0);
-	point = [(LLNSWindow*)window convertRectFromScreen:point];
+	point = [(__bridge LLNSWindow*)window convertRectFromScreen:point];
 	coord[0] = point.origin.x;
 	coord[1] = point.origin.y;
 }
@@ -313,7 +311,7 @@ void convertScreenToWindow(NSWindowRef window, float *coord)
 void convertRectToScreen(NSWindowRef window, float *coord)
 {
 	NSRect point = NSMakeRect(coord[0], coord[1], coord[2], coord[3]);
-	point = [(LLNSWindow*)window convertRectToScreen:point];
+	point = [(__bridge LLNSWindow*)window convertRectToScreen:point];
 	
 	coord[0] = point.origin.x;
 	coord[1] = point.origin.y;
@@ -324,7 +322,7 @@ void convertRectToScreen(NSWindowRef window, float *coord)
 void convertRectFromScreen(NSWindowRef window, float *coord)
 {
 	NSRect point = NSMakeRect(coord[0], coord[1], coord[2], coord[3]);
-	point = [(LLNSWindow*)window convertRectFromScreen:point];
+	point = [(__bridge LLNSWindow*)window convertRectFromScreen:point];
 	
 	coord[0] = point.origin.x;
 	coord[1] = point.origin.y;
@@ -334,7 +332,7 @@ void convertRectFromScreen(NSWindowRef window, float *coord)
 
 void convertWindowToScreen(NSWindowRef window, float *coord)
 {
-	LLNSWindow *nsWindow = (LLNSWindow*)window;
+	LLNSWindow *nsWindow = (__bridge LLNSWindow*)window;
 	NSRect rect = NSMakeRect(coord[0], coord[1], 0, 0);
 	rect = [nsWindow convertRectToScreen:rect];
 	NSRect screenRect = [[nsWindow screen] frame];
@@ -343,32 +341,32 @@ void convertWindowToScreen(NSWindowRef window, float *coord)
 	coord[1] = retPoint.y;
 }
 
-void closeWindow(NSWindowRef window)
+void closeWindow(NSWindowRef windowRef)
 {
-	[(LLNSWindow*)window close];
-	[(LLNSWindow*)window release];
+    LLNSWindow *window = (LLNSWindow*)CFBridgingRelease(windowRef);
+	[window close];
 }
 
-void removeGLView(GLViewRef view)
+void removeGLView(GLViewRef viewRef)
 {
-	[(LLOpenGLView*)view clearGLContext];
-	[(LLOpenGLView*)view removeFromSuperview];
-	[(LLOpenGLView*)view release];
+    LLOpenGLView *view_ctx = (LLOpenGLView*)CFBridgingRelease(viewRef);
+	[(LLOpenGLView*)view_ctx clearGLContext];
+	[(LLOpenGLView*)view_ctx removeFromSuperview];
 }
 
 void setupInputWindow(NSWindowRef window, GLViewRef glview)
 {
-	[[(LLAppDelegate*)[NSApp delegate] inputView] setGLView:(LLOpenGLView*)glview];
+	[[(LLAppDelegate*)[NSApp delegate] inputView] setGLView:(__bridge LLOpenGLView*)glview];
 }
 
 void commitCurrentPreedit(GLViewRef glView)
 {
-	[(LLOpenGLView*)glView commitCurrentPreedit];
+	[(__bridge LLOpenGLView*)glView commitCurrentPreedit];
 }
 
 void allowDirectMarkedTextInput(bool allow, GLViewRef glView)
 {
-    [(LLOpenGLView*)glView allowMarkedTextInput:allow];
+    [(__bridge LLOpenGLView*)glView allowMarkedTextInput:allow];
 }
 
 NSWindowRef getMainAppWindow()
@@ -376,12 +374,12 @@ NSWindowRef getMainAppWindow()
 	LLNSWindow *winRef = [(LLAppDelegate*)[[LLApplication sharedApplication] delegate] window];
 	
 	[winRef setAcceptsMouseMovedEvents:TRUE];
-	return winRef;
+	return (NSWindowRef)CFBridgingRetain(winRef);
 }
 
 void makeFirstResponder(NSWindowRef window, GLViewRef view)
 {
-	[(LLNSWindow*)window makeFirstResponder:(LLOpenGLView*)view];
+	[(__bridge LLNSWindow*)window makeFirstResponder:(__bridge LLOpenGLView*)view];
 }
 
 void requestUserAttention()
@@ -391,8 +389,9 @@ void requestUserAttention()
 
 long showAlert(std::string text, std::string title, int type)
 {
+    NSModalResponse response;
     @autoreleasepool {
-        NSAlert *alert = [[[NSAlert alloc] init] autorelease];
+        NSAlert *alert = [[NSAlert alloc] init];
         
         [alert setMessageText:[NSString stringWithCString:title.c_str() encoding:[NSString defaultCStringEncoding]]];
         [alert setInformativeText:[NSString stringWithCString:text.c_str() encoding:[NSString defaultCStringEncoding]]];
@@ -408,30 +407,29 @@ long showAlert(std::string text, std::string title, int type)
             [alert addButtonWithTitle:@"Yes"];
             [alert addButtonWithTitle:@"No"];
         }
-        long ret = [alert runModal];
-        
-        if (ret == NSAlertFirstButtonReturn)
+        response = [alert runModal];
+    }
+    if (response == NSAlertFirstButtonReturn)
+    {
+        if (type == 1)
         {
-            if (type == 1)
-            {
-                ret = 3;
-            } else if (type == 2)
-            {
-                ret = 0;
-            }
-        } else if (ret == NSAlertSecondButtonReturn)
+            response = 3;
+        } else if (type == 2)
         {
-            if (type == 0 || type == 1)
-            {
-                ret = 2;
-            } else if (type == 2)
-            {
-                ret = 1;
-            }
+            response = 0;
+        }
+    } else if (response == NSAlertSecondButtonReturn)
+    {
+        if (type == 0 || type == 1)
+        {
+            response = 2;
+        } else if (type == 2)
+        {
+            response = 1;
         }
-        
-        return ret;
     }
+    
+    return response;
 }
 
 unsigned int getModifiers()
diff --git a/indra/newview/llappdelegate-objc.mm b/indra/newview/llappdelegate-objc.mm
index 52127cf46bb..ec685b63e0e 100644
--- a/indra/newview/llappdelegate-objc.mm
+++ b/indra/newview/llappdelegate-objc.mm
@@ -44,12 +44,6 @@
 @synthesize inputView;
 @synthesize currentInputLanguage;
 
-- (void)dealloc
-{
-    [currentInputLanguage release];
-    [super dealloc];
-}
-
 - (void) applicationWillFinishLaunching:(NSNotification *)notification
 {
     [[NSAppleEventManager sharedAppleEventManager] setEventHandler:self andSelector:@selector(handleGetURLEvent:withReplyEvent:) forEventClass:kInternetEventClass andEventID:kAEGetURL];
@@ -141,7 +135,7 @@
 		return NSTerminateCancel;
 	} else {
 		// pumpMainLoop() returned true: it's done. Okay, done with frameTimer.
-		[frameTimer release];
+		frameTimer = nil;
 		cleanupViewer();
 		return NSTerminateNow;
 	}
@@ -154,7 +148,7 @@
 	{
 		// Once pumpMainLoop() reports that we're done, cancel frameTimer:
 		// stop the repetitive calls.
-		[frameTimer release];
+        frameTimer = nil;
 		[[LLApplication sharedApplication] terminate:self];
 	}
 }
diff --git a/indra/newview/llappviewermacosx-objc.mm b/indra/newview/llappviewermacosx-objc.mm
index 1d9e8feab56..8ffc978aab9 100644
--- a/indra/newview/llappviewermacosx-objc.mm
+++ b/indra/newview/llappviewermacosx-objc.mm
@@ -43,7 +43,7 @@ void launchApplication(const std::string* app_name, const std::vector<std::strin
         [app_name_ns appendFormat:@"/%@", [NSString stringWithCString:app_name->c_str()
                                                              encoding:[NSString defaultCStringEncoding]]];
         
-        NSMutableArray *args_ns = [[[NSMutableArray alloc] init] autorelease];
+        NSMutableArray *args_ns = [[NSMutableArray alloc] init];
         
         for (int i=0; i < args->size(); ++i)
         {
@@ -53,9 +53,15 @@ void launchApplication(const std::string* app_name, const std::vector<std::strin
                                 encoding:[NSString defaultCStringEncoding]]];
         }
         
-        NSTask *task = [[[NSTask alloc] init] autorelease];
+        NSTask *task = [[NSTask alloc] init];
         NSBundle *bundle = [NSBundle bundleWithPath:[[NSWorkspace sharedWorkspace] fullPathForApplication:app_name_ns]];
-        [task setLaunchPath:[bundle executablePath]];
+	@try {
+        	[task setLaunchPath:[bundle executablePath]];
+	}
+	@catch (NSException *theException) {
+		NSLog(@"Caught a %@ exception, bailing.", theException);
+		return;
+	}
         [task setArguments:args_ns];
         [task launch];
         
diff --git a/indra/newview/llfilepicker_mac.mm b/indra/newview/llfilepicker_mac.mm
index dd0d7386f44..1219e1e277b 100644
--- a/indra/newview/llfilepicker_mac.mm
+++ b/indra/newview/llfilepicker_mac.mm
@@ -46,7 +46,7 @@ std::vector<std::string>* doLoadDialog(const std::vector<std::string>* allowed_t
         
         if (allowed_types && !allowed_types->empty())
         {
-            fileTypes = [[[NSMutableArray alloc] init] autorelease];
+            fileTypes = [[NSMutableArray alloc] init];
             
             for (i=0;i<allowed_types->size();++i)
             {
-- 
GitLab