UISearchBar取消按钮resignFirstResponder消息发送到已释放的实例

发布于 2024-10-30 17:20:21 字数 4589 浏览 3 评论 0原文

我有一个带有取消按钮的简单 UISearchBar。当我调用以下命令

- (void)searchBarCancelButtonClicked:(UISearchBar *)sBar {
    sBar.text = nil;
    [sBar resignFirstResponder];
}

并使用断点观看它时,我收到以下错误(在调用 [sBar resignFirstResponder] 后发生):

*** -[CFString class]: message sent to deallocated instance 0x5ec7270

在我使用以下命令执行搜索后发生代码:

- (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];
}

并返回到搜索视图。

有什么想法吗?

编辑:这是来自 info malloc-history 的堆栈日志:

Alloc: 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

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:

Alloc: 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

长途伴 2024-11-06 17:20:21

不要将文本清零,而是分配一个空字符串:

[sBar setText:@""];

Don't nil out the text, instead assign an empty string:

[sBar setText:@""];
阳光①夏 2024-11-06 17:20:21

哦哇!我想我修复了它......

我意识到它必须与我正在加载另一个视图并将 sBar.text 作为确切对象发送的事实有关。我改变了台词:

BrowseViewController *browseViewController = [[BrowseViewController alloc] initWithSearchTerms:sBar.text];

一切

BrowseViewController *browseViewController = [[BrowseViewController alloc] initWithSearchTerms:[sBar.text copy]];

都很好!

我基本上发送了 sBar.text 的副本(因为它在 BrowseViewController 中被释放)。

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文