Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Object was probably modified after being freed

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.

like image 256
Stone Avatar asked Jun 11 '11 16:06

Stone


3 Answers

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.

like image 88
bbum Avatar answered Oct 25 '22 15:10

bbum


Apart from setting a breakpoint in malloc_error_break - press Command-6 in xCode to jump to the breakpoints tab - also enable the malloc aids in your scheme.

Go the the schemes selector, choose "Edit scheme" find the "Run" target and go to the "Diagnostics" tab. Below memory management enable scribble, guard edges, guard malloc and zombie objects.

With a bit of luck xCode will catch you writing outside your allocated memory and corrupting memory.

It's like adult supervision for dealing with memory...

like image 9
Niels Castle Avatar answered Oct 25 '22 14:10

Niels Castle


Understand the error message: it's saying that something continued using (and modifying) the object after you freed it. This code frees it and does not modify it thereafter, but you have to ask what else could possibly continue using it (without knowing it was already freed).

Each time the code in this snippet runs, it releases (frees) any existing checkinViewController, and allocates a new one, and clearly it never touches the old one again. But who else may have a pointer to the old object?

Possibly other code you wrote, and possibly [delegate window], which gets a reference via "[[delegate window] addSubview:checkInViewController.view];"? Hopefully the latter takes its own reference, meaning release won't immediately free it.

But watch out for anywhere you're copying that pointer without adding a reference. If you do this somewhere, and then elsewhere (such as the above snippet) someone calls release on the same pointer, you may now have a pointer to an object that's been freed.

like image 3
metamatt Avatar answered Oct 25 '22 15:10

metamatt