2015-04-03 4 views
0

у меня странный вопрос с этим кодом она вылетает и говорит в системном журналеКраша по неизвестной указателю причины освобождения

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

ответ

3

Вы принципиально неправильно используете dispatch_async. Деятельность, связанная с пользовательским интерфейсом (например, оповещения), должна выполняться в основном потоке приложений. Они не являются потокобезопасными.

Вот почему вы столкнулись с авариями.

0

Добавить точку останова. Отредактируйте свою схему и нажмите «Диагностика», выберите «Включить объекты зомби». затем повторно создайте проект и запустите его. вы будете знать причину или где.