Преглед изворни кода

Remove a retain cylce in FLEXSystemLogViewController

matrush пре 5 година
родитељ
комит
79e22cf828
1 измењених фајлова са 30 додато и 26 уклоњено
  1. 30 26
      Classes/GlobalStateExplorers/SystemLog/FLEXSystemLogViewController.m

+ 30 - 26
Classes/GlobalStateExplorers/SystemLog/FLEXSystemLogViewController.m

@@ -46,7 +46,7 @@ static BOOL my_os_log_shim_enabled(void *addr) {
     if (!NSUserDefaults.standardUserDefaults.flex_disableOSLog) {
     if (!NSUserDefaults.standardUserDefaults.flex_disableOSLog) {
         return;
         return;
     }
     }
-    
+
     // Thanks to @Ram4096 on GitHub for telling me that
     // Thanks to @Ram4096 on GitHub for telling me that
     // os_log is conditionally enabled by the SDK version
     // os_log is conditionally enabled by the SDK version
     void *addr = __builtin_return_address(0);
     void *addr = __builtin_return_address(0);
@@ -61,23 +61,23 @@ static BOOL my_os_log_shim_enabled(void *addr) {
         (void *)my_os_log_shim_enabled,
         (void *)my_os_log_shim_enabled,
         (void **)&orig_os_log_shim_enabled
         (void **)&orig_os_log_shim_enabled
     }}, 1) == 0;
     }}, 1) == 0;
-    
+
     if (FLEXDidHookNSLog && orig_os_log_shim_enabled != nil) {
     if (FLEXDidHookNSLog && orig_os_log_shim_enabled != nil) {
         // Check if our rebinding worked
         // Check if our rebinding worked
         FLEXNSLogHookWorks = my_os_log_shim_enabled(addr) == NO;
         FLEXNSLogHookWorks = my_os_log_shim_enabled(addr) == NO;
     }
     }
-    
+
     // So, just because we rebind the lazily loaded symbol for
     // So, just because we rebind the lazily loaded symbol for
     // this function doesn't mean it's even going to be used.
     // this function doesn't mean it's even going to be used.
     // While it seems to be sufficient for the simulator, for
     // While it seems to be sufficient for the simulator, for
     // whatever reason it is not sufficient on-device. We need
     // whatever reason it is not sufficient on-device. We need
     // to actually hook the function with something like Substrate.
     // to actually hook the function with something like Substrate.
-    
+
     // Check if we have substrate, and if so use that instead
     // Check if we have substrate, and if so use that instead
     void *handle = dlopen("/usr/lib/libsubstrate.dylib", RTLD_LAZY);
     void *handle = dlopen("/usr/lib/libsubstrate.dylib", RTLD_LAZY);
     if (handle) {
     if (handle) {
         MSHookFunction = dlsym(handle, "MSHookFunction");
         MSHookFunction = dlsym(handle, "MSHookFunction");
-        
+
         if (MSHookFunction) {
         if (MSHookFunction) {
             // Set the hook and check if it worked
             // Set the hook and check if it worked
             void *unused;
             void *unused;
@@ -96,16 +96,16 @@ static BOOL my_os_log_shim_enabled(void *addr) {
 
 
 - (void)viewDidLoad {
 - (void)viewDidLoad {
     [super viewDidLoad];
     [super viewDidLoad];
-    
+
     self.showsSearchBar = YES;
     self.showsSearchBar = YES;
     self.showSearchBarInitially = NO;
     self.showSearchBarInitially = NO;
 
 
     __weak __typeof(self) weakSelf = self;
     __weak __typeof(self) weakSelf = self;
     id logHandler = ^(NSArray<FLEXSystemLogMessage *> *newMessages) {
     id logHandler = ^(NSArray<FLEXSystemLogMessage *> *newMessages) {
-        __strong __typeof(weakSelf) self = weakSelf;
-        [self handleUpdateWithNewMessages:newMessages];
+        __strong __typeof(weakSelf) strongSelf = weakSelf;
+        [strongSelf handleUpdateWithNewMessages:newMessages];
     };
     };
-    
+
     if (FLEXOSLogAvailable() && !FLEXNSLogHookWorks) {
     if (FLEXOSLogAvailable() && !FLEXNSLogHookWorks) {
         _logController = [FLEXOSLogController withUpdateHandler:logHandler];
         _logController = [FLEXOSLogController withUpdateHandler:logHandler];
     } else {
     } else {
@@ -114,9 +114,9 @@ static BOOL my_os_log_shim_enabled(void *addr) {
 
 
     self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
     self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
     self.title = @"Waiting for Logs...";
     self.title = @"Waiting for Logs...";
-    
+
     // Toolbar buttons //
     // Toolbar buttons //
-    
+
     UIBarButtonItem *scrollDown = [UIBarButtonItem
     UIBarButtonItem *scrollDown = [UIBarButtonItem
         itemWithImage:FLEXResources.scrollToBottomIcon
         itemWithImage:FLEXResources.scrollToBottomIcon
         target:self
         target:self
@@ -127,7 +127,7 @@ static BOOL my_os_log_shim_enabled(void *addr) {
         target:self
         target:self
         action:@selector(showLogSettings)
         action:@selector(showLogSettings)
     ];
     ];
-    
+
     [self addToolbarItems:@[scrollDown, settings]];
     [self addToolbarItems:@[scrollDown, settings]];
 }
 }
 
 
@@ -138,26 +138,30 @@ static BOOL my_os_log_shim_enabled(void *addr) {
 }
 }
 
 
 - (NSArray<FLEXTableViewSection *> *)makeSections {
 - (NSArray<FLEXTableViewSection *> *)makeSections {
+    __weak __typeof(self) weakSelf = self;
     _logMessages = [FLEXMutableListSection list:@[]
     _logMessages = [FLEXMutableListSection list:@[]
         cellConfiguration:^(FLEXSystemLogCell *cell, FLEXSystemLogMessage *message, NSInteger row) {
         cellConfiguration:^(FLEXSystemLogCell *cell, FLEXSystemLogMessage *message, NSInteger row) {
-            cell.logMessage = message;
-            cell.highlightedText = self.filterText;
-            
-            if (row % 2 == 0) {
-                cell.backgroundColor = FLEXColor.primaryBackgroundColor;
-            } else {
-                cell.backgroundColor = FLEXColor.secondaryBackgroundColor;
+            __strong __typeof(self) strongSelf = weakSelf;
+            if (strongSelf) {
+                cell.logMessage = message;
+                cell.highlightedText = strongSelf.filterText;
+
+                if (row % 2 == 0) {
+                    cell.backgroundColor = FLEXColor.primaryBackgroundColor;
+                } else {
+                    cell.backgroundColor = FLEXColor.secondaryBackgroundColor;
+                }
             }
             }
         } filterMatcher:^BOOL(NSString *filterText, FLEXSystemLogMessage *message) {
         } filterMatcher:^BOOL(NSString *filterText, FLEXSystemLogMessage *message) {
             NSString *displayedText = [FLEXSystemLogCell displayedTextForLogMessage:message];
             NSString *displayedText = [FLEXSystemLogCell displayedTextForLogMessage:message];
             return [displayedText localizedCaseInsensitiveContainsString:filterText];
             return [displayedText localizedCaseInsensitiveContainsString:filterText];
         }
         }
     ];
     ];
-    
+
     self.logMessages.cellRegistrationMapping = @{
     self.logMessages.cellRegistrationMapping = @{
         kFLEXSystemLogCellIdentifier : [FLEXSystemLogCell class]
         kFLEXSystemLogCellIdentifier : [FLEXSystemLogCell class]
     };
     };
-    
+
     return @[self.logMessages];
     return @[self.logMessages];
 }
 }
 
 
@@ -198,27 +202,27 @@ static BOOL my_os_log_shim_enabled(void *addr) {
 
 
     NSString *aslToggle = disableOSLog ? @"Enable os_log (default)" : @"Disable os_log";
     NSString *aslToggle = disableOSLog ? @"Enable os_log (default)" : @"Disable os_log";
     NSString *persistence = persistent ? @"Disable persistent logging" : @"Enable persistent logging";
     NSString *persistence = persistent ? @"Disable persistent logging" : @"Enable persistent logging";
-    
+
     NSString *title = @"System Log Settings";
     NSString *title = @"System Log Settings";
     NSString *body = @"In iOS 10 and up, ASL has been replaced by os_log. "
     NSString *body = @"In iOS 10 and up, ASL has been replaced by os_log. "
     "The os_log API is much more limited. Below, you can opt-into the old behavior "
     "The os_log API is much more limited. Below, you can opt-into the old behavior "
     "if you want cleaner, more reliable logs within FLEX, but this will break "
     "if you want cleaner, more reliable logs within FLEX, but this will break "
     "anything that expects os_log to be working, such as Console.app. "
     "anything that expects os_log to be working, such as Console.app. "
     "This setting requires the app to restart to take effect. \n\n"
     "This setting requires the app to restart to take effect. \n\n"
-    
+
     "To get as close to the old behavior as possible with os_log enabled, logs must "
     "To get as close to the old behavior as possible with os_log enabled, logs must "
     "be collected manually at launch and stored. This setting has no effect "
     "be collected manually at launch and stored. This setting has no effect "
     "on iOS 9 and below, or if os_log is disabled. "
     "on iOS 9 and below, or if os_log is disabled. "
     "You should only enable persistent logging when you need it.";
     "You should only enable persistent logging when you need it.";
-    
+
     FLEXOSLogController *logController = (FLEXOSLogController *)self.logController;
     FLEXOSLogController *logController = (FLEXOSLogController *)self.logController;
-    
+
     [FLEXAlert makeAlert:^(FLEXAlert *make) {
     [FLEXAlert makeAlert:^(FLEXAlert *make) {
         make.title(title).message(body);
         make.title(title).message(body);
         make.button(aslToggle).destructiveStyle().handler(^(NSArray<NSString *> *strings) {
         make.button(aslToggle).destructiveStyle().handler(^(NSArray<NSString *> *strings) {
             [defaults toggleBoolForKey:kFLEXDefaultsDisableOSLogForceASLKey];
             [defaults toggleBoolForKey:kFLEXDefaultsDisableOSLogForceASLKey];
         });
         });
-        
+
         make.button(persistence).handler(^(NSArray<NSString *> *strings) {
         make.button(persistence).handler(^(NSArray<NSString *> *strings) {
             [defaults toggleBoolForKey:kFLEXDefaultsiOSPersistentOSLogKey];
             [defaults toggleBoolForKey:kFLEXDefaultsiOSPersistentOSLogKey];
             logController.persistent = !persistent;
             logController.persistent = !persistent;