程序收到信号:“EXC_BAD_ACCESS”

时间:2011-08-26 23:27:21

标签: iphone ios xcode cocoa-touch

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

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

3 个答案:

答案 0 :(得分:0)

听起来你没有保留你的recordViewController,所以当你推送另一个视图时,它会被释放,当你试图返回时,它就不再存在了。

如果您不使用ARC,请在创建时保留它。

答案 1 :(得分: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;
}

答案 2 :(得分:0)

我曾多次遇到过这个问题,并且总是有某种不定式的递归 - 很可能是在setter中调用了setter。

修改
googleling for it我发现了这个:ERROR: Memory Leak, data formatters temporarily unavailable

由于您的代码也在泄漏,可能是同样的问题。