From 3dd874a99b81b6c392f1ab4c40015ba43f7801e6 Mon Sep 17 00:00:00 2001
From: Rye Mutt <rye@alchemyviewer.org>
Date: Fri, 28 Oct 2022 18:47:32 -0400
Subject: [PATCH] Fix memory leaks in lldir objc/mac

---
 indra/llfilesystem/lldir_mac.cpp       | 32 +++++-----
 indra/llfilesystem/lldir_utils_objc.h  | 10 ++--
 indra/llfilesystem/lldir_utils_objc.mm | 82 +++++++++++++-------------
 3 files changed, 60 insertions(+), 64 deletions(-)

diff --git a/indra/llfilesystem/lldir_mac.cpp b/indra/llfilesystem/lldir_mac.cpp
index 3bc4ee844e1..9ad8e274b60 100644
--- a/indra/llfilesystem/lldir_mac.cpp
+++ b/indra/llfilesystem/lldir_mac.cpp
@@ -66,16 +66,16 @@ LLDir_Mac::LLDir_Mac()
 
     const std::string     secondLifeString = "SecondLife";
     
-    std::string *executablepathstr = getSystemExecutableFolder();
+    std::string executablepathstr = getSystemExecutableFolder();
 
     //NOTE:  LLINFOS/LLERRS will not output to log here.  The streams are not initialized.
     
-	if (executablepathstr)
+	if (!executablepathstr.empty())
 	{
 		// mExecutablePathAndName
-		mExecutablePathAndName = *executablepathstr;
+		mExecutablePathAndName = executablepathstr;
         
-        boost::filesystem::path executablepath(*executablepathstr);
+        boost::filesystem::path executablepath(executablepathstr);
         
 # ifndef BOOST_SYSTEM_NO_DEPRECATED
 #endif
@@ -83,8 +83,8 @@ LLDir_Mac::LLDir_Mac()
         mExecutableDir = executablepath.parent_path().string();
 		
 		// mAppRODataDir
-        std::string *resourcepath = getSystemResourceFolder();
-        mAppRODataDir = *resourcepath;
+        std::string resourcepath = getSystemResourceFolder();
+        mAppRODataDir = resourcepath;
 		
 		// *NOTE: When running in a dev tree, use the copy of
 		// skins in indra/newview/ rather than in the application bundle.  This
@@ -110,11 +110,11 @@ LLDir_Mac::LLDir_Mac()
 		}
 		
 		// mOSUserDir
-        std::string *appdir = getSystemApplicationSupportFolder();
+        std::string appdir = getSystemApplicationSupportFolder();
         std::string rootdir;
 
         //Create root directory
-        if (CreateDirectory(*appdir, secondLifeString, &rootdir))
+        if (CreateDirectory(appdir, secondLifeString, &rootdir))
         {
             
             // Save the full path to the folder
@@ -128,12 +128,10 @@ LLDir_Mac::LLDir_Mac()
         }
     
 		//mOSCacheDir
-        std::string *cachedir =  getSystemCacheFolder();
-
-        if (cachedir)
-		
+        std::string cachedir =  getSystemCacheFolder();
+        if (!cachedir.empty())
 		{
-            mOSCacheDir = *cachedir;
+            mOSCacheDir = cachedir;
             //TODO:  This changes from ~/Library/Cache/Secondlife to ~/Library/Cache/com.app.secondlife/Secondlife.  Last dir level could go away.
             CreateDirectory(mOSCacheDir, secondLifeString, NULL);
 		}
@@ -143,12 +141,10 @@ LLDir_Mac::LLDir_Mac()
 		
 		// mTempDir
         //Aura 120920 boost::filesystem::temp_directory_path() not yet implemented on mac. :(
-        std::string *tmpdir = getSystemTempFolder();
-        if (tmpdir)
+        std::string tmpdir = getSystemTempFolder();
+        if (!tmpdir.empty())
         {
-            
-            CreateDirectory(*tmpdir, secondLifeString, &mTempDir);
-            if (tmpdir) delete tmpdir;
+            CreateDirectory(tmpdir, secondLifeString, &mTempDir);
         }
 		
 		mWorkingDir = getCurPath();
diff --git a/indra/llfilesystem/lldir_utils_objc.h b/indra/llfilesystem/lldir_utils_objc.h
index 12019c42845..59dbeb4aec2 100644
--- a/indra/llfilesystem/lldir_utils_objc.h
+++ b/indra/llfilesystem/lldir_utils_objc.h
@@ -33,11 +33,11 @@
 
 #include <iostream>
 
-std::string* getSystemTempFolder();
-std::string* getSystemCacheFolder();
-std::string* getSystemApplicationSupportFolder();
-std::string* getSystemResourceFolder();
-std::string* getSystemExecutableFolder();
+std::string getSystemTempFolder();
+std::string getSystemCacheFolder();
+std::string getSystemApplicationSupportFolder();
+std::string getSystemResourceFolder();
+std::string getSystemExecutableFolder();
 
 
 #endif // LL_LLDIR_UTILS_OBJC_H
diff --git a/indra/llfilesystem/lldir_utils_objc.mm b/indra/llfilesystem/lldir_utils_objc.mm
index da55a2f897a..20540fb93cd 100644
--- a/indra/llfilesystem/lldir_utils_objc.mm
+++ b/indra/llfilesystem/lldir_utils_objc.mm
@@ -30,75 +30,75 @@
 #include "lldir_utils_objc.h"
 #import <Cocoa/Cocoa.h>
 
-std::string* getSystemTempFolder()
+std::string getSystemTempFolder()
 {
-    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
-    NSString * tempDir = NSTemporaryDirectory();
-    if (tempDir == nil)
-        tempDir = @"/tmp";
-    std::string *result = ( new std::string([tempDir UTF8String]) );
-    [pool release];
+    std::string result;
+    @autoreleasepool {
+        NSString * tempDir = NSTemporaryDirectory();
+        if (tempDir == nil)
+            tempDir = @"/tmp";
+        result = std::string([tempDir UTF8String]);
+    }
     
     return result;
 }
 
 //findSystemDirectory scoped exclusively to this file. 
-std::string* findSystemDirectory(NSSearchPathDirectory searchPathDirectory,
+std::string findSystemDirectory(NSSearchPathDirectory searchPathDirectory,
                                    NSSearchPathDomainMask domainMask)
 {
-    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
-    
-    std::string *result = nil;
-    NSString *path = nil;
-    
-    // Search for the path
-    NSArray* paths = NSSearchPathForDirectoriesInDomains(searchPathDirectory,
-                                                         domainMask,
-                                                         YES);
-    if ([paths count])
-    {
-        path = [paths objectAtIndex:0];
-        //HACK:  Always attempt to create directory, ignore errors.
-        NSError *error = nil;
-
-        [[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:&error];
-
+    std::string result;
+    @autoreleasepool {
+        NSString *path = nil;
         
-        result = new std::string([path UTF8String]);        
+        // Search for the path
+        NSArray* paths = NSSearchPathForDirectoriesInDomains(searchPathDirectory,
+                                                             domainMask,
+                                                             YES);
+        if ([paths count])
+        {
+            path = [paths objectAtIndex:0];
+            //HACK:  Always attempt to create directory, ignore errors.
+            NSError *error = nil;
+            
+            [[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:&error];
+            
+            
+            result = std::string([path UTF8String]);
+        }
     }
-    [pool release];
     return result;
 }
 
-std::string* getSystemExecutableFolder()
+std::string getSystemExecutableFolder()
 {
-    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
-
-    NSString *bundlePath = [[NSBundle mainBundle] executablePath];
-    std::string *result = (new std::string([bundlePath UTF8String]));  
-    [pool release];
+    std::string result;
+    @autoreleasepool {
+        NSString *bundlePath = [[NSBundle mainBundle] executablePath];
+        result = std::string([bundlePath UTF8String]);
+    }
 
     return result;
 }
 
-std::string* getSystemResourceFolder()
+std::string getSystemResourceFolder()
 {
-    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
-
-    NSString *bundlePath = [[NSBundle mainBundle] resourcePath];
-    std::string *result = (new std::string([bundlePath UTF8String]));
-    [pool release];
+    std::string result;
+    @autoreleasepool {
+        NSString *bundlePath = [[NSBundle mainBundle] resourcePath];
+        result = std::string([bundlePath UTF8String]);
+    }
     
     return result;
 }
 
-std::string* getSystemCacheFolder()
+std::string getSystemCacheFolder()
 {
     return findSystemDirectory (NSCachesDirectory,
                                 NSUserDomainMask);
 }
 
-std::string* getSystemApplicationSupportFolder()
+std::string getSystemApplicationSupportFolder()
 {
     return findSystemDirectory (NSApplicationSupportDirectory,
                                 NSUserDomainMask);
-- 
GitLab