由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序

Posted

技术标签:

【中文标题】由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序【英文标题】:Terminating app due to uncaught exception 'NSInternalInconsistencyException' 【发布时间】:2011-08-27 18:58:16 【问题描述】:

是的,我有 5 个视图。其中一个视图称为 RecordViewController。 (RecordViewController 导致错误)我可以很好地切换视图。但是一旦我进入recordviewcontroller,我就可以切换到另一个视图。但是如果我想切换回recordviewcontroller。它把我扔出我的应用程序并给我这个错误:

2011-08-27 19:52:00.358 app[1737:707] *** Assertion failure in +[RecordViewController alloc], /Users/user/Desktop/app/Classes/RecordViewController.m:111
2011-08-27 19:52:00.373 app[1737:707] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Attempted to allocated second singleton  RecordViewController'
*** Call stack at first throw:
(
    0   CoreFoundation                      0x36fd064f __exceptionPreprocess + 114
    1   libobjc.A.dylib                     0x34229c5d objc_exception_throw + 24
    2   CoreFoundation                      0x36fd0491 +[NSException raise:format:arguments:] + 68
    3   Foundation                          0x353d0573 -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 62
    4   app                             0x00014bdd +[RecordViewController alloc] + 212
    5   app                             0x00013fd3 -[EffectsViewController goRecord:] + 34
    6   CoreFoundation                      0x36f40571 -[NSObject(NSObject) performSelector:withObject:withObject:] + 24
    7   UIKit                               0x35dd2ec9 -[UIApplication sendAction:to:from:forEvent:] + 84
    8   UIKit                               0x35dd2e69 -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 32
    9   UIKit                               0x35dd2e3b -[UIControl sendAction:to:forEvent:] + 38
    10  UIKit                               0x35dd2b8d -[UIControl(Internal) _sendActionsForEvents:withEvent:] + 356
    11  UIKit                               0x35dd283b -[UIControl touchesBegan:withEvent:] + 186
    12  UIKit                               0x35dd1bb1 -[UIWindow _sendTouchesForEvent:] + 300
    13  UIKit                               0x35dd156f -[UIWindow sendEvent:] + 262
    14  UIKit                               0x35dba313 -[UIApplication sendEvent:] + 298
    15  UIKit                               0x35db9c53 _UIApplicationHandleEvent + 5090
    16  GraphicsServices                    0x366f4e77 PurpleEventCallback + 666
    17  CoreFoundation                      0x36fa7a97 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 26
    18  CoreFoundation                      0x36fa983f __CFRunLoopDoSource1 + 166
    19  CoreFoundation                      0x36faa60d __CFRunLoopRun + 520
    20  CoreFoundation                      0x36f3aec3 CFRunLoopRunSpecific + 230
    21  CoreFoundation                      0x36f3adcb CFRunLoopRunInMode + 58
    22  GraphicsServices                    0x366f441f GSEventRunModal + 114
    23  GraphicsServices                    0x366f44cb GSEventRun + 62
    24  UIKit                               0x35de4d69 -[UIApplication _run] + 404
    25  UIKit                               0x35de2807 UIApplicationMain + 670
    26  app                                 0x00002e73 main + 70
    27  app                                 0x00002e28 start + 40
)
terminate called after throwing an instance of 'NSException'

这里是recordviewcontroller的代码

-(void)countUp 

    mainInt += 1;
    seconds.text = [NSString stringWithFormat:@"%02d", mainInt];



-(IBAction)goBack:(id)sender; 

    [self dismissModalViewControllerAnimated:YES];



static RecordViewController *sharedInstance = nil;


+(RecordViewController *) sharedInstance 
    @synchronized([RecordViewController class])
    
        if (!sharedInstance)
        
            [[self alloc]init];
        
        return sharedInstance;
    
    return nil;


+(id) alloc

    @synchronized([RecordViewController class])
    
        NSAssert(sharedInstance == nil, @"Attempted to allocated second singleton  RecordViewController");
        sharedInstance = [super alloc];
        return sharedInstance;
    
    return nil;



- (id)autorelease 
    return self;



- (void)viewDidLoad 
    [super viewDidLoad];


    toggle = YES;
    btnPlay.hidden = YES;


    AVAudiosession * audioSession = [AVAudioSession sharedInstance];

    [audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error: &error];

    [audioSession setActive:YES error: &error];







- (IBAction)  start_button_pressed


    if(toggle)
    
        toggle = NO;
        [actSpinner startAnimating];
        [btnStart setImage:[UIImage imageNamed:@"recordstop.png"] forState:UIControlStateNormal];
        mainInt = 0;
        theTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(countUp) userInfo:nil repeats:YES];


        btnPlay.enabled = toggle;
        btnPlay.hidden = !toggle;


        NSMutableDictionary* recordSetting = [[NSMutableDictionary alloc] init];
        [recordSetting setValue :[NSNumber numberWithInt:kAudioFormatAppleIMA4] forKey:AVFormatIDKey];
        [recordSetting setValue:[NSNumber numberWithFloat:44100.0] forKey:AVSampleRateKey]; 
        [recordSetting setValue:[NSNumber numberWithInt: 2] forKey:AVNumberOfChannelsKey];


        recordedTmpFile = [NSURL fileURLWithPath:[NSTemporaryDirectory() stringByAppendingPathComponent: [NSString stringWithFormat: @"%.0f.%@", [NSDate timeIntervalSinceReferenceDate] * 1000.0, @"caf"]]];
        NSLog(@"Using File called: %@",recordedTmpFile);

        recorder = [[ AVAudioRecorder alloc] initWithURL:recordedTmpFile settings:recordSetting error:&error];

        [recorder setDelegate:self];

        [recorder prepareToRecord];

        [recorder record];


    
    else
    
        toggle = YES;
        [actSpinner stopAnimating];
        [btnStart setImage:[UIImage imageNamed:@"recordrecord.png"] forState:UIControlStateNormal];
        btnPlay.enabled = toggle;
        btnPlay.hidden = !toggle;
        [theTimer invalidate];

        NSLog(@"Using File called: %@",recordedTmpFile);

        [recorder stop];
    


- (void)didReceiveMemoryWarning 
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.


-(IBAction) play_button_pressed


    AVAudioPlayer * avPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:recordedTmpFile error:&error];
    [avPlayer prepareToPlay];
    [avPlayer play];



- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight);


- (void)viewDidUnload 
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    //Clean up the temp file.
    NSFileManager * fm = [NSFileManager defaultManager];
    [fm removeItemAtPath:[recordedTmpFile path] error:&error];
    //Call the dealloc on the remaining objects.
    [recorder dealloc];
    recorder = nil;
    recordedTmpFile = nil;


- (void)dealloc 
    [super dealloc];


@end

谢谢

【问题讨论】:

【参考方案1】:

RecordViewController 被声明为单例 (Apple docs on Singletons)。拥有一个单例视图控制器永远不会真正有意义,但我将忽略它并简单地帮助您。

您收到该异常的原因是您调用了两次[RecordViewController alloc]。它应该只被调用一次,因为它是一个单例。

要解决此问题,您需要在每次想要与班级互动时调用[RecordViewController sharedInstance],而不是像您可能习惯的那样调用[[RecordViewController alloc] init]

【讨论】:

Recordviewcontroller,上面有一个录音机 - 我正在使用单例,因为这允许我在录音时切换视图。那么我需要删除或调用我的代码的哪一部分?谢谢 我无法具体说明您需要修改代码的位置,但查看异常我可以看到 [EffectsViewController goRecord] 内部有一个地方只需检查您的代码并替换 [[RecordViewController alloc] init] 与 [RecordViewController sharedInstance] 好的,我已经改变了。但是现在当我尝试继续它时,我得到'程序接收信号:“EXC_BAD_ACCESS”。数据格式化程序暂时不可用,将在“继续”后重试。 (正在调试的程序在从 GDB 调用的函数中发出信号。GDB 已将上下文恢复到调用前的状态。要更改此行为,请使用“set unwindonsignal off” 对包含函数 (gdb_objc_startDebuggerMode) 的表达式的评估将是放弃。)' :( 发生这种情况的原因有很多。我建议尝试解决程序中的所有内存泄漏,而不是专注于让这个类正确。提示是在“产品”菜单项中使用“分析”,该实用程序将检查您的所有代码并通知您可能做错的事情。之后在 Instruments (Product->Profile) 中查看应用程序并使用“Leaks”工具。

以上是关于由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序的主要内容,如果未能解决你的问题,请参考以下文章

目标 C:由于未捕获的异常“NSInvalidArgumentException”而终止应用程序

由于未捕获的异常而终止应用程序 [UIImageView _isResizable]

由于未捕获的异常而终止应用程序,同时加载视图

由于未捕获的异常“NSInternalInconsistencyException”错误而终止应用程序

“由于未捕获的异常而终止应用程序”在推送视图控制器时崩溃

由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,