UISearchBar Cancel Button resignFirstResponder message sent to deallocated instance
I have a simple UISearchBar with a cancel button. When I call the following:
- (void)searchBarCancelButtonClicked:(UISearchBar *)sBar {
sBar.text = nil;
[sBar resignFirstResponder];
}
and watch it with breakpoints, I get the following error (which occurs after the [sBar resignFirstResponder] gets called):
*** -[CFString class]: message sent to deallocated instance 0x5ec7270
This ONLY happens once I've performed a search using the following code:
- (void)searchBarSearchButtonClicked:(UISearchBar *)sBar{
[sBar resignFirstResponder];
BrowseViewController *browseViewController = [[BrowseViewController alloc] initWithSearchTerms:sBar.text];
browseViewController.title = @"Search Results";
[self.navigationController pushViewController:browseViewController animated:YES];
[browseViewController release];
}
And go back to the search view.
Any ideas SO?
EDIT: Here's a stack log from info malloc-history:
Allo开发者_StackOverflow中文版c: Block address: 0x0df0dc30 length: 16 Stack - pthread: 0xa0b93500 number of frames: 58 0: 0x987601dc in malloc_zone_malloc 1: 0xf6880d in _CFRuntimeCreateInstance 2: 0xf6a745 in __CFStringCreateImmutableFunnel3 3: 0xf6d108 in CFStringCreateCopy 4: 0x292c4 in -[NSCFString copyWithZone:] 5: 0xfb97ca in -[NSObject(NSObject) copy] 6: 0x3684d0 in -[UITextField setText:] 7: 0x369316 in -[UITextField _endedEditing] 8: 0x3674bb in -[UITextField willDetachFieldEditor:] 9: 0x37915d in -[UIFieldEditor becomeFieldEditorForView:] 10: 0x36964e in -[UITextField _resignFirstResponder] 11: 0x39d21a in -[UIResponder resignFirstResponder] 12: 0x36522f in -[UITextField resignFirstResponder] 13: 0x25b2 in -[SearchViewController searchBarSearchButtonClicked:] at /Users/Createanet/Documents/iPhone Development/FoodFinderSW/Classes/SearchViewController.m:46 14: 0x4d637e in -[UISearchBar(UISearchBarStatic) _searchFieldReturnPressed] 15: 0x35ef9d in -[UIControl(Deprecated) sendAction:toTarget:forEvent:] 16: 0x36187e in -[UIControl(Internal) _sendActionsForEventMask:withEvent:] 17: 0x376b3c in -[UIFieldEditor webView:shouldInsertText:replacingDOMRange:givenAction:] 18: 0xfc967d in __invoking___ 19: 0xfc9551 in -[NSInvocation invoke] 20: 0xff7258 in -[NSInvocation invokeWithTarget:] 21: 0x21ea366 in -[_WebSafeForwarder forwardInvocation:] 22: 0xfca404 in ___forwarding___ 23: 0xfc9f22 in __forwarding_prep_0___ 24: 0x2212434 in _ZN15WebEditorClient16shouldInsertTextERKN7WebCore6StringEPNS0_5RangeENS0_18EditorInsertActionE 25: 0x276f8bd in _ZNK7WebCore6Editor16shouldInsertTextERKNS_6StringEPNS_5RangeENS_18EditorInsertActionE 26: 0x244045f in _ZN7WebCore6Editor24insertParagraphSeparatorEv 27: 0x23f8b19 in _ZN7WebCore12EventHandler28defaultTextInputEventHandlerEPNS_9TextEventE 28: 0x23ddcfa in _ZN7WebCore4Node19defaultEventHandlerEPNS_5EventE 29: 0x23dd7d1 in _ZN7WebCore4Node20dispatchGenericEventEN3WTF10PassRefPtrINS_5EventEEE 30: 0x23dd17b in _ZN7WebCore4Node13dispatchEventEN3WTF10PassRefPtrINS_5EventEEE 31: 0x23f8a38 in _ZN7WebCore11EventTarget13dispatchEventEN3WTF10PassRefPtrINS_5EventEEERi 32: 0x23f85f9 in _ZN7WebCore12EventHandler20handleTextInputEventERKNS_6StringEPNS_5EventEbb 33: 0x23f84f9 in _ZN7WebCore6Editor10insertTextERKNS_6StringEPNS_5EventE 34: 0x223add2 in -[WebHTMLView(WebNSTextInputSupport) insertText:] 35: 0x23f8474 in -[WAKResponder tryToPerform:with:] 36: 0x2252213 in -[WebView(WebViewEditingActions) _performResponderOperation:with:] 37: 0x22501f0 in -[WebView(WebViewEditingActions) insertText:] 38: 0x40ad25 in __-[UIKeyboardImpl addInputString:fromVariantKey:]_block_invoke_1 39: 0x4155eb in -[UIKeyboardImpl addInputString:fromVariantKey:] 40: 0x417c61 in -[UIKeyboardImpl handleKeyEvent:] 41: 0x5403f1 in -[UIKeyboardLayoutStar sendStringAction:forKey:] 42: 0x53dc84 in -[UIKeyboardLayoutStar touchUp:] 43: 0x424152 in -[UIKeyboardLayout touchesEnded:withEvent:] 44: 0x2f50d1 in -[UIWindow _sendTouchesForEvent:] 45: 0x2d637a in -[UIApplication sendEvent:] 46: 0x2db732 in _UIApplicationHandleEvent 47: 0x198ea36 in PurpleEventCallback 48: 0x103a064 in __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ 49: 0xf9a6f7 in __CFRunLoopDoSource1 50: 0xf97983 in __CFRunLoopRun 51: 0xf97240 in CFRunLoopRunSpecific 52: 0xf97161 in CFRunLoopRunInMode 53: 0x198d268 in GSEventRunModal 54: 0x198d32d in GSEventRun 55: 0x2df42e in UIApplicationMain 56: 0x2158 in main at /Users/Createanet/Documents/iPhone Development/FoodFinderSW/main.m:13 57: 0x20e9 in start
Don't nil out the text, instead assign an empty string:
[sBar setText:@""];
Oh wow! I think I fixed it...
I realised that it MUST have had something to do with the fact I was loading another view, and sending sBar.text as the exact object. I changed the line:
BrowseViewController *browseViewController = [[BrowseViewController alloc] initWithSearchTerms:sBar.text];
to
BrowseViewController *browseViewController = [[BrowseViewController alloc] initWithSearchTerms:[sBar.text copy]];
and all is well!
I'm basically sending a copy of sBar.text (as this gets dealloc'd in the BrowseViewController).
精彩评论