From e36769aad82b457becfee54d84c67eca41a35681 Mon Sep 17 00:00:00 2001
From: Rye Mutt <rye@alchemyviewer.org>
Date: Wed, 11 Nov 2020 04:26:38 -0500
Subject: [PATCH] Modernize objc a bit by replacing NSAutoReplacePool with
 @autoreplacepool

---
 indra/llwindow/llopenglview-objc.mm     | 133 +++++++++++++-----------
 indra/llwindow/llwindowmacosx-objc.mm   | 133 ++++++++++++------------
 indra/newview/llappdelegate-objc.mm     |   2 +-
 indra/newview/llappviewermacosx-objc.mm |  66 ++++++------
 4 files changed, 167 insertions(+), 167 deletions(-)

diff --git a/indra/llwindow/llopenglview-objc.mm b/indra/llwindow/llopenglview-objc.mm
index 015a4ad9c14..5730e702322 100644
--- a/indra/llwindow/llopenglview-objc.mm
+++ b/indra/llwindow/llopenglview-objc.mm
@@ -207,57 +207,61 @@ attributedStringInfo getSegments(NSAttributedString *str)
 
 - (id) initWithFrame:(NSRect)frame withSamples:(NSUInteger)samples andVsync:(BOOL)vsync
 {
-	[self registerForDraggedTypes:[NSArray arrayWithObject:NSURLPboardType]];
-	[self initWithFrame:frame];
-	
-	// Initialize with a default "safe" pixel format that will work with versions dating back to OS X 10.6.
-	// Any specialized pixel formats, i.e. a core profile pixel format, should be initialized through rebuildContextWithFormat.
-	// 10.7 and 10.8 don't really care if we're defining a profile or not.  If we don't explicitly request a core or legacy profile, it'll always assume a legacy profile (for compatibility reasons).
-	NSOpenGLPixelFormatAttribute attrs[] = {
-        NSOpenGLPFANoRecovery,
-		NSOpenGLPFADoubleBuffer,
-		NSOpenGLPFAClosestPolicy,
-		NSOpenGLPFAAccelerated,
-		NSOpenGLPFASampleBuffers, static_cast<NSOpenGLPixelFormatAttribute>(samples > 0 ? 1 : 0),
-		NSOpenGLPFASamples, static_cast<NSOpenGLPixelFormatAttribute>(samples),
-		NSOpenGLPFAStencilSize, 8,
-		NSOpenGLPFADepthSize, 24,
-		NSOpenGLPFAAlphaSize, 8,
-		NSOpenGLPFAColorSize, 24,
-		0
-    };
-	
-	NSOpenGLPixelFormat *pixelFormat = [[[NSOpenGLPixelFormat alloc] initWithAttributes:attrs] autorelease];
-	
-	if (pixelFormat == nil)
-	{
-		NSLog(@"Failed to create pixel format!", nil);
-		return nil;
-	}
-	
-	NSOpenGLContext *glContext = [[NSOpenGLContext alloc] initWithFormat:pixelFormat shareContext:nil];
-	
-	if (glContext == nil)
-	{
-		NSLog(@"Failed to create OpenGL context!", nil);
-		return nil;
-	}
-
-	//for retina support
-	[self setWantsBestResolutionOpenGLSurface:YES];
-
-	[self setPixelFormat:pixelFormat];
+	self = [super initWithFrame:frame];
+	if (!self) { return self; }	// Despite what this may look like, returning nil self is a-ok.
+    @autoreleasepool {
+        [self registerForDraggedTypes:[NSArray arrayWithObject:NSURLPboardType]];
+        
+        // Initialize with a default "safe" pixel format that will work with versions dating back to OS X 10.6.
+        // Any specialized pixel formats, i.e. a core profile pixel format, should be initialized through rebuildContextWithFormat.
+        // 10.7 and 10.8 don't really care if we're defining a profile or not.  If we don't explicitly request a core or legacy profile, it'll always assume a legacy profile (for compatibility reasons).
+        NSOpenGLPixelFormatAttribute attrs[] = {
+            NSOpenGLPFANoRecovery,
+            NSOpenGLPFADoubleBuffer,
+            NSOpenGLPFAClosestPolicy,
+            NSOpenGLPFAAccelerated,
+            NSOpenGLPFAMultisample,
+            NSOpenGLPFASampleBuffers, static_cast<NSOpenGLPixelFormatAttribute>((samples > 0 ? 1 : 0)),
+            NSOpenGLPFASamples, static_cast<NSOpenGLPixelFormatAttribute>(samples),
+            NSOpenGLPFAStencilSize, static_cast<NSOpenGLPixelFormatAttribute>(8),
+            NSOpenGLPFADepthSize, static_cast<NSOpenGLPixelFormatAttribute>(24),
+            NSOpenGLPFAAlphaSize, static_cast<NSOpenGLPixelFormatAttribute>(8),
+            NSOpenGLPFAColorSize, static_cast<NSOpenGLPixelFormatAttribute>(24),
+            0
+        };
+        
+        NSOpenGLPixelFormat *pixelFormat = [[[NSOpenGLPixelFormat alloc] initWithAttributes:attrs] autorelease];
+        
+        if (pixelFormat == nil)
+        {
+            NSLog(@"Failed to create pixel format!", nil);
+            return nil;
+        }
+        
+        NSOpenGLContext *glContext = [[[NSOpenGLContext alloc] initWithFormat:pixelFormat shareContext:nil] autorelease];
+        
+        if (glContext == nil)
+        {
+            NSLog(@"Failed to create OpenGL context!", nil);
+            return nil;
+        }
+        
+        //for retina support
+        [self setWantsBestResolutionOpenGLSurface:YES];
+        
+        [self setPixelFormat:pixelFormat];
+        
+        [self setOpenGLContext:glContext];
+        
+        [glContext setView:self];
+        
+        [glContext makeCurrentContext];
+        
+        GLint glVsync = vsync ? 1 : 0;
+        [glContext setValues:&glVsync forParameter:NSOpenGLCPSwapInterval];
 
-	[self setOpenGLContext:glContext];
-	
-	[glContext setView:self];
-	
-	[glContext makeCurrentContext];
-	
-	GLint glVsync = vsync ? 1 : 0;
-	[glContext setValues:&glVsync forParameter:NSOpenGLCPSwapInterval];
+    }
 
-    
 	return self;
 }
 
@@ -268,20 +272,23 @@ attributedStringInfo getSegments(NSAttributedString *str)
 
 - (BOOL) rebuildContextWithFormat:(NSOpenGLPixelFormat *)format
 {
-	NSOpenGLContext *ctx = [self openGLContext];
-	
-	[ctx clearDrawable];
-	[ctx initWithFormat:format shareContext:nil];
-	
-	if (ctx == nil)
-	{
-		NSLog(@"Failed to create OpenGL context!", nil);
-		return false;
-	}
-	
-	[self setOpenGLContext:ctx];
-	[ctx setView:self];
-	[ctx makeCurrentContext];
+    @autoreleasepool {
+        NSOpenGLContext *ctx = [self openGLContext];
+        
+        [ctx clearDrawable];
+        ctx = [[[NSOpenGLContext alloc] initWithFormat:format shareContext:nil] autorelease];
+        
+        if (ctx == nil)
+        {
+            NSLog(@"Failed to create OpenGL context!", nil);
+            return false;
+        }
+        
+        [self setOpenGLContext:ctx];
+        [ctx setView:self];
+        [ctx makeCurrentContext];
+    }
+
 	return true;
 }
 
diff --git a/indra/llwindow/llwindowmacosx-objc.mm b/indra/llwindow/llwindowmacosx-objc.mm
index cbbbccdc5bd..61c2ff71bcf 100644
--- a/indra/llwindow/llwindowmacosx-objc.mm
+++ b/indra/llwindow/llwindowmacosx-objc.mm
@@ -49,14 +49,12 @@ void setupCocoa()
 	
 	if(!inited)
 	{
-		NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
-		
-		// 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"];
-		
-		[pool release];
+        @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"];
+        }
 
 		inited = true;
 	}
@@ -102,21 +100,19 @@ unsigned short *copyFromPBoard()
 
 CursorRef createImageCursor(const char *fullpath, int hotspotX, int hotspotY)
 {
-	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
-	
-	// extra retain on the NSCursor since we want it to live for the lifetime of the app.
-	NSCursor *cursor =
-	[[[NSCursor alloc]
-	  initWithImage:
-	  [[[NSImage alloc] initWithContentsOfFile:
-		[NSString stringWithFormat:@"%s", fullpath]
-		]autorelease]
-	  hotSpot:NSMakePoint(hotspotX, hotspotY)
-	  ]retain];
-	
-	[pool release];
-	
-	return (CursorRef)cursor;
+    @autoreleasepool {
+        // extra retain on the NSCursor since we want it to live for the lifetime of the app.
+        NSCursor *cursor =
+        [[[NSCursor alloc]
+          initWithImage:
+          [[[NSImage alloc] initWithContentsOfFile:
+            [NSString stringWithFormat:@"%s", fullpath]
+            ]autorelease]
+          hotSpot:NSMakePoint(hotspotX, hotspotY)
+          ] retain];
+        
+        return (CursorRef)cursor;
+    }
 }
 
 void setArrowCursor()
@@ -176,10 +172,10 @@ OSErr releaseImageCursor(CursorRef ref)
 {
 	if( ref != NULL )
 	{
-		NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
-		NSCursor *cursor = (NSCursor*)ref;
-		[cursor release];
-		[pool release];
+        @autoreleasepool {
+            NSCursor *cursor = (NSCursor*)ref;
+            [cursor release];
+        }
 	}
 	else
 	{
@@ -193,10 +189,10 @@ OSErr setImageCursor(CursorRef ref)
 {
 	if( ref != NULL )
 	{
-		NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
-		NSCursor *cursor = (NSCursor*)ref;
-		[cursor set];
-		[pool release];
+        @autoreleasepool {
+            NSCursor *cursor = (NSCursor*)ref;
+            [cursor set];
+        }
 	}
 	else
 	{
@@ -395,46 +391,47 @@ void requestUserAttention()
 
 long showAlert(std::string text, std::string title, int type)
 {
-    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]]];
-    if (type == 0)
-    {
-        [alert addButtonWithTitle:@"Okay"];
-    } else if (type == 1)
-    {
-        [alert addButtonWithTitle:@"Okay"];
-        [alert addButtonWithTitle:@"Cancel"];
-    } else if (type == 2)
-    {
-        [alert addButtonWithTitle:@"Yes"];
-        [alert addButtonWithTitle:@"No"];
-    }
-    long ret = [alert runModal];
-    [alert dealloc];
-    
-    if (ret == NSAlertFirstButtonReturn)
-    {
-        if (type == 1)
+    @autoreleasepool {
+        NSAlert *alert = [[[NSAlert alloc] init] autorelease];
+        
+        [alert setMessageText:[NSString stringWithCString:title.c_str() encoding:[NSString defaultCStringEncoding]]];
+        [alert setInformativeText:[NSString stringWithCString:text.c_str() encoding:[NSString defaultCStringEncoding]]];
+        if (type == 0)
+        {
+            [alert addButtonWithTitle:@"Okay"];
+        } else if (type == 1)
         {
-            ret = 3;
+            [alert addButtonWithTitle:@"Okay"];
+            [alert addButtonWithTitle:@"Cancel"];
         } else if (type == 2)
         {
-            ret = 0;
+            [alert addButtonWithTitle:@"Yes"];
+            [alert addButtonWithTitle:@"No"];
         }
-    } else if (ret == NSAlertSecondButtonReturn)
-    {
-        if (type == 0 || type == 1)
+        long ret = [alert runModal];
+        
+        if (ret == NSAlertFirstButtonReturn)
         {
-            ret = 2;
-        } else if (type == 2)
+            if (type == 1)
+            {
+                ret = 3;
+            } else if (type == 2)
+            {
+                ret = 0;
+            }
+        } else if (ret == NSAlertSecondButtonReturn)
         {
-            ret = 1;
+            if (type == 0 || type == 1)
+            {
+                ret = 2;
+            } else if (type == 2)
+            {
+                ret = 1;
+            }
         }
+        
+        return ret;
     }
-    
-    return ret;
 }
 
 unsigned int getModifiers()
@@ -444,10 +441,10 @@ unsigned int getModifiers()
 
 void setTitle(const std::string& title)
 {
-	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
-	LLNSWindow *winRef = [(LLAppDelegate*)[[LLApplication sharedApplication] delegate] window];
-	NSString *nsTitle = [NSString stringWithUTF8String:title.c_str()];
-	[winRef setTitle:nsTitle];
-	[pool release];
+    @autoreleasepool {
+        LLNSWindow *winRef = [(LLAppDelegate*)[[LLApplication sharedApplication] delegate] window];
+        NSString *nsTitle = [NSString stringWithUTF8String:title.c_str()];
+        [winRef setTitle:nsTitle];
+    }
 }
 
diff --git a/indra/newview/llappdelegate-objc.mm b/indra/newview/llappdelegate-objc.mm
index 576dd077e07..d88fed859da 100644
--- a/indra/newview/llappdelegate-objc.mm
+++ b/indra/newview/llappdelegate-objc.mm
@@ -358,7 +358,7 @@ struct AttachmentInfo
     }
     else
     {
-	[super sendEvent:event];
+        [super sendEvent:event];
     }
 }
 
diff --git a/indra/newview/llappviewermacosx-objc.mm b/indra/newview/llappviewermacosx-objc.mm
index 17301847e8b..1d9e8feab56 100644
--- a/indra/newview/llappviewermacosx-objc.mm
+++ b/indra/newview/llappviewermacosx-objc.mm
@@ -35,39 +35,35 @@
 
 void launchApplication(const std::string* app_name, const std::vector<std::string>* args)
 {
-
-    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
-    
-	if (app_name->empty()) return;
-
-	NSMutableString* app_name_ns = [NSMutableString stringWithString:[[NSBundle mainBundle] resourcePath]];	//Path to resource dir
-	[app_name_ns appendFormat:@"/%@", [NSString stringWithCString:app_name->c_str() 
-								encoding:[NSString defaultCStringEncoding]]];
-
-	NSMutableArray *args_ns = nil;
-	args_ns = [[NSMutableArray alloc] init];
-
-	for (int i=0; i < args->size(); ++i)
-	{
-        NSLog(@"Adding string %s", (*args)[i].c_str());
-		[args_ns addObject:
-			[NSString stringWithCString:(*args)[i].c_str()
-						encoding:[NSString defaultCStringEncoding]]];
-	}
-
-    NSTask *task = [[NSTask alloc] init];
-    NSBundle *bundle = [NSBundle bundleWithPath:[[NSWorkspace sharedWorkspace] fullPathForApplication:app_name_ns]];
-    [task setLaunchPath:[bundle executablePath]];
-    [task setArguments:args_ns];
-    [task launch];
-    
-//	NSWorkspace *workspace = [NSWorkspace sharedWorkspace];
-//	NSURL *url = [NSURL fileURLWithPath:[workspace fullPathForApplication:app_name_ns]];
-//
-//	NSError *error = nil;
-//	[workspace launchApplicationAtURL:url options:0 configuration:[NSDictionary dictionaryWithObject:args_ns forKey:NSWorkspaceLaunchConfigurationArguments] error:&error];
-	//TODO Handle error
-    
-    [pool release];
-	return;
+    @autoreleasepool
+    {
+        if (app_name->empty()) return;
+        
+        NSMutableString* app_name_ns = [NSMutableString stringWithString:[[NSBundle mainBundle] resourcePath]];    //Path to resource dir
+        [app_name_ns appendFormat:@"/%@", [NSString stringWithCString:app_name->c_str()
+                                                             encoding:[NSString defaultCStringEncoding]]];
+        
+        NSMutableArray *args_ns = [[[NSMutableArray alloc] init] autorelease];
+        
+        for (int i=0; i < args->size(); ++i)
+        {
+            NSLog(@"Adding string %s", (*args)[i].c_str());
+            [args_ns addObject:
+             [NSString stringWithCString:(*args)[i].c_str()
+                                encoding:[NSString defaultCStringEncoding]]];
+        }
+        
+        NSTask *task = [[[NSTask alloc] init] autorelease];
+        NSBundle *bundle = [NSBundle bundleWithPath:[[NSWorkspace sharedWorkspace] fullPathForApplication:app_name_ns]];
+        [task setLaunchPath:[bundle executablePath]];
+        [task setArguments:args_ns];
+        [task launch];
+        
+        //    NSWorkspace *workspace = [NSWorkspace sharedWorkspace];
+        //    NSURL *url = [NSURL fileURLWithPath:[workspace fullPathForApplication:app_name_ns]];
+        //
+        //    NSError *error = nil;
+        //    [workspace launchApplicationAtURL:url options:0 configuration:[NSDictionary dictionaryWithObject:args_ns forKey:NSWorkspaceLaunchConfigurationArguments] error:&error];
+        //TODO Handle error
+    }
 }
-- 
GitLab