Object was probably modified after being freed

Stone picture Stone · Jun 11, 2011 · Viewed 37.1k times · Source

I am working on a project on iPhone. I am now initiating a new UIViewController from another UIViewController, and then switch between them. Here is my code.

iGreenAppDelegate *delegate = [UIApplication sharedApplication].delegate;
if(checkInViewController) {
    [checkInViewController release];
    checkInViewController = nil;
}
checkInViewController = [[CheckInViewController alloc] initWithCheckpoint:checkpoint];

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.8];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:[delegate window] cache:YES];
[[delegate rootTabBarController].view removeFromSuperview];
[[delegate window] addSubview:checkInViewController.view];
[UIView commitAnimations];

The Problem is the second time I initiate the UIViewController, I want to release it to avoid causing memory leak. The Debugger displays

iGreen(916,0x3f60348c) malloc: error for object 0x130350: incorrect checksum for freed object - object was probably modified after being freed. set a breakpoint in malloc_error_break to debug

This is strange because similar codes in other parts don't return such error. Moreover, I tried autorelease, but the program will immediately crash and the Debugger says I am modifying finalized layers.

I've been working on the problem for a whole night, and still confused about it.

Answer

bbum picture bbum · Jun 11, 2011

Set a breakpoint in malloc_error_break to debug.

Do that and post the backtrace.

Usually, this means that you corrupted memory, but it may also mean that you have an over-released object. Try Build and Analyze, too.