2011-08-26 107 views
-2

好吧,我有5次。其中一个视图叫做RecordViewController。 (RecordViewController导致错误) 我可以很好地切换视图。但是一旦我进入了recordviewcontroller,我可以切换到另一个视图。但是,如果我想切换回recordviewcontroller。它抛出我出我的应用程序,并给了我这个错误:程序接收到的信号:“EXC_BAD_ACCESS”

Program received signal: “EXC_BAD_ACCESS”. 
Data Formatters temporarily unavailable, will re-try after a 'continue'. (The program being debugged was signaled while in a function called from GDB. 
GDB has restored the context to what it was before the call. 
To change this behavior use "set unwindonsignal off" 
Evaluation of the expression containing the function (gdb_objc_startDebuggerMode) will be abandoned.) 

这里是recordviewcontroller代码 - 伊夫删除切换视图代码,因为它没有必要的。

@implementation RecordViewController 
@synthesize actSpinner, btnStart, btnPlay; 



-(void)countUp { 

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

} 



static RecordViewController *sharedInstance = nil; 


+ (RecordViewController*)sharedInstance { 
    if (sharedInstance == nil) { 
     sharedInstance = [[super allocWithZone:NULL] init]; 
    } 

    return sharedInstance; 
} 


+ (id)allocWithZone:(NSZone*)zone { 
    return [[self sharedInstance] retain]; 
} 


- (id)copyWithZone:(NSZone *)zone { 
    return self; 
} 


- (id)retain { 
    return self; 
} 


- (NSUInteger)retainCount { 
    return NSUIntegerMax; 
} 


- (void)release { 

} 


- (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

尝试[启用僵尸](http://stackoverflow.com/questions/2190227/how-do-i-set-nszombieenabled-in-xcode-4)... – jtbandes

+1

你的代码会更容易阅读,如果你有更好的格式(空白行少,模板没有评论) – vikingosegundo

+0

哦,对不起。我会修复 – LA12

回答

0

听起来像是你不留住recordViewController,所以当你把另一种观点认为,它的释放,当您尝试回去,这是不存在了。

如果你不使用ARC,保留它,当你创建它。

+0

ARC没有被使用,哪部分我需要特别保留? – LA12

+0

如果你没有使用弧,那么很多对象都在使用你的代码。检查内存管理指南。 – vikingosegundo

0

拆下实现共下列方法:

+ (id)allocWithZone:(NSZone*)zone 

- (id)copyWithZone:(NSZone *)zone 

- (id)retain 

- (NSUInteger)retainCount 

- (void)release 

并改变sharedInstance执行以下,并且还添加以下所示的alloc的实施。

+(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; 
} 
+0

嗨,我得到这个错误。 ***在+ [RecordViewController ALLOC],类/ RecordViewController.m断言故障:111 ***终止应用程序由于未捕获的异常“NSInternalInconsistencyException”,原因:“已尝试分配第二单RecordViewController” ***调用堆栈在第一次投掷: – LA12

相关问题