How can UISearchDisplayController autorelease cause crash in a different view controller?

Posted by Tofrizer on Stack Overflow See other posts from Stack Overflow or by Tofrizer
Published on 2010-05-03T14:04:25Z Indexed on 2010/05/03 14:08 UTC
Read the original article Hit count: 414

Hi,

I have two view controllers A and B. From A, I navigate to view controller B as follows:

// in View Controller A 
// navigateToB method

-(void) navigateToB {

BViewController *bViewController = 
[[BViewController alloc] initWithNibName: @"BView" bundle:nil];

bViewController.bProperty1 = SOME_STRING_CONSTANT;
bViewController.title = @"A TITLE OF A VC's CHOOSING"; 
[self.navigationController pushViewController: bViewController animated:YES];
[bViewController release]; //<----- releasing 0x406c1e0

}

In BViewController, the property bPropery1 is defined with copy as below (note, B also contains UITableView and other properties):

@property (nonatomic, copy) NSString *bProperty1;

Everything appeared to work fine when navigating back and forth between A and B. That is until I added a UISearchDisplayController to the table view contained in BViewController. Now when I navigate out of B, back to A, the app crashes.

Stack trace shows what looks the search display controller being autoreleased at time of crash:

#0 0x009663a7 in ___forwarding___
#1 0x009426c2 in __forwarding_prep_0___
#2 0x018c8539 in -[UISearchDisplayController _destroyManagedTableView]
#3 0x018c8ea4 in -[UISearchDisplayController dealloc]
#4 0x00285ce5 in NSPopAutoreleasePool

NSZombies shows:

-[BViewController respondsToSelector:]: message sent to deallocated instance 0x406c1e0

And malloc history on this points to the bViewController already released in A's navigateToB method above:

    Call [2] [arg=132]: thread_a065e720 |start  ... <snip> 
..._sendActionsForEvents:withEvent:] | -[UIControl sendAction:to:forEvent:] | -
[UIApplication sendAction:to:from:forEvent:] | -[**AViewController navigateToB**] | 
+[NSObject alloc] | +[NSObject allocWithZone:] | _internal_class_createInstance | 
_internal_class_createInstanceFromZone | calloc | malloc_zone_calloc 

Can someone please give me any ideas on what is happening here? In navigateToB method, once the bViewController is released (after pushViewController), that's should be it for bViewController. Nothing else even knows about it as it is local to the navigateToB method block and it has been released.

When navigating from B back to A, nothing is invoked in viewDidLoad, viewWillAppear etc that will re-enter navigateToB.

It looks like somehow search display controller has a reference to something in my AViewController and so as it is autoreleased it is taking this "something" down with it but I cannot understand how this is possible, especially as I'm using copy to pass data between A and B.

I'm going potty over this. I'm sure this is my mistake somewhere and so I turn to you, Stack Overflow legends for any words of wisdom or advice on how to resolve this.

Many Thanks.

© Stack Overflow or respective owner

Related posts about iphone-sdk-3.0

Related posts about objective-c