2015-04-03 83 views
0

我有奇特的問題與此代碼它crashs 和系統日誌崩潰未知原因指針被釋放

malloc: ** error for object xxxxxxx pointer being freed was not allocated 

這裏說的是,我一直在使用的代碼,並造成我加入的NSLog檢測問題墜毀它在提出警報之後墜毀。

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 
      NSData *data = [[[task standardOutput] fileHandleForReading] readDataToEndOfFile]; 
      NSString *appInfo = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding]; 

      UIAlertController *altC = [UIAlertController alertControllerWithTitle:@"Title" message:appInfo preferredStyle:UIAlertControllerStyleAlert]; 
      UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) { 
       [mWindow setHidden:YES]; 
      }]; 
      NSLog(@"Added Cancel"); 
      UIAlertAction *sendAction = [UIAlertAction actionWithTitle:@"Send" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { 
       ZipArchive *fileArchiveZip = [[ZipArchive alloc] init]; 
       NSLog(@"Start UnZip."); 
       if([fileArchiveZip unzipOpenFile:zipFileName]) { 
        if([fileArchiveZip unzipFileTo:extractPath overWrite:YES] != NO) { 
         //unzip data success 
         NSLog(@"UnZip Successed."); 
         //do something 
         NSLog(@"Remove Start."); 
         [[NSFileManager defaultManager] removeItemAtPath:zipFileName error:NULL]; 
         NSLog(@"Remove Successed."); 
        } 
        NSLog(@"Closing Zip."); 
        [fileArchiveZip unzipCloseFile]; 
        NSLog(@"Closed Zip."); 
       } 
       // dispatch_async(dispatch_get_main_queue(), ^{ 
        NSLog(@"Start Alert."); 
        NSDictionary *infoFilePath = [NSDictionary dictionaryWithContentsOfFile:plistPath]; 
        NSLog(@"Got Info Path"); 
        UIAlertController *altC = [UIAlertController alertControllerWithTitle:@"title" message:@"set file name" preferredStyle:UIAlertControllerStyleAlert]; 
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) { 

        }]; 
        NSLog(@"Added Cancel"); 
        UIAlertAction *sendAction = [UIAlertAction actionWithTitle:@"Send" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { 
         UITextField *alertTextFiled = altC.textFields.firstObject; 
         NSString *userString = alertTextFiled.text; 
         NSInteger textLength = [alertTextFiled.text length]; 
         NSMutableDictionary *defaults = [NSMutableDictionary dictionary]; 
         [defaults addEntriesFromDictionary:infoFilePath]; 
         [defaults setObject:userString forKey:infoFilePath[@"filename"]]; 
         [defaults writeToFile:plistPath atomically:YES]; 

        }]; 
        NSLog(@"Added Action + text"); 
        [altC addAction:cancelAction]; 
        [altC addAction:sendAction]; 
        [altC addTextFieldWithConfigurationHandler:^(UITextField *textField) { 
         textField.placeholder = @"FileName"; 
         textField.text = @"File-Name"; 
        }]; 
        NSLog(@"Added textField"); 
        UIPopoverPresentationController *popover = altC.popoverPresentationController; 
        if (popover) { 
         popover.sourceView = selfRootViewController.view; 
         popover.sourceRect = selfRootViewController.view.bounds; 
         popover.permittedArrowDirections = UIPopoverArrowDirectionAny; 
        } 
        NSLog(@"Presenting alert"); 
        dispatch_async(dispatch_get_main_queue(), ^{ 
         [selfRootViewController presentViewController:altC animated:YES completion:nil]; 
         NSLog(@"Presented alert"); 
        }); 

       [mWindow setHidden:YES]; 

      }]; 
      NSLog(@"Added Action + text"); 
      [altC addAction:cancelAction]; 
      [altC addAction:sendAction]; 
      // [altC addTextFieldWithConfigurationHandler:^(UITextField *textField) { 
      //  textField.placeholder = @"FileName"; 
      // }]; 
      NSLog(@"Added textField"); 
      UIPopoverPresentationController *popover = altC.popoverPresentationController; 
      if (popover) { 
       popover.sourceView = selfRootViewController.view; 
       popover.sourceRect = selfRootViewController.view.bounds; 
       popover.permittedArrowDirections = UIPopoverArrowDirectionAny; 
      } 
      dispatch_async(dispatch_get_main_queue(), ^{ 
       NSLog(@"Presenting alert"); 
       [selfRootViewController presentViewController:altC animated:YES completion:nil]; 
       NSLog(@"Presented alert"); 
      }); 
    }); 

任何想法或建議嗎?

+0

哪條線會崩潰? – NobodyNada 2015-04-03 00:20:21

回答

3

您從根本上濫用dispatch_async。與UI相關的活動(如警報)必須發生在Apps主線程上。它們不是線程安全的。

這就是爲什麼你遇到崩潰。

0

添加中斷點。編輯你的方案,然後按'診斷',選擇'啓用殭屍對象'。然後重新構建您的項目並運行。你會知道原因或在哪裏。

相關問題