崩溃未知原因指针被释放

时间:2015-04-03 00:12:07

标签: ios objective-c iphone

我遇到这个代码崩溃的奇怪问题 并在syslog中说

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");
            });
    });

任何想法或建议?

2 个答案:

答案 0 :(得分:3)

您从根本上滥用了dispatch_async。 UI相关活动(如警报)必须在Apps主线程上发生。它们不是线程安全的。

这就是您遇到崩溃的原因。

答案 1 :(得分:0)

添加断点。编辑您的方案并按“诊断”,然后选择“启用僵尸对象”#39;。然后重新构建您的项目并运行。你会知道原因或在哪里。

相关问题