UISearchBar、UITableViewController 和 UINavigationController 在执行本机代码时收到 SIGSEGV

发布于 2024-12-07 02:42:03 字数 6704 浏览 0 评论 0原文

我有一个简单的测试应用程序,其中有一个扩展 UINavigationController 的 NavigationController

[MonoTouch.Foundation.Register("NavigationController")]
public class NavigationController : UINavigationController
{
    public NavigationController (IntPtr handle) : base (handle)
    {

    }

    [Export ("initWithCoder:")]
    public NavigationController (NSCoder coder) : base (coder)
    {

    }

    public NavigationController (UIViewController rootViewController) : base(rootViewController)
    {

    }

    public override void ViewDidLoad ()
    {
        base.ViewDidLoad ();

        View.BackgroundColor = UIColor.Green;
    }
}

我还有一个扩展 UITableViewController 的 CustomersTableViewController (这里 Source 没有影响)。在 ViewDidLoad 方法中,我创建一个 UISearchBar 并将其添加到 TableHeaderView 中。

public class CustomersTableViewController : UITableViewController
{
    UISearchBar _searchBar;

    public CustomersTableViewController(UITableViewStyle style) : base(style)
    {
    }

    public override void ViewDidLoad ()
    {
        base.ViewDidLoad ();

        _searchBar = CreateSearchBar ();
        _searchBar.SearchButtonClicked += OnSearchBarSearchButtonClicked;

        TableView.TableHeaderView = _searchBar;

    }

    void OnSearchBarSearchButtonClicked (object sender, EventArgs e)
    {

    }

    static UISearchBar CreateSearchBar ()
    {
        UISearchBar search = new UISearchBar ();
        search.Placeholder = "Search Customers";
        search.SizeToFit ();
        search.AutocorrectionType = UITextAutocorrectionType.No;
        search.AutocapitalizationType = UITextAutocapitalizationType.None;
        return search;
    }
}

在 Main.cs 中,我运行以下代码片段。如果我通过代码或通过xib创建navigationController,它是相同的。

// The name AppDelegate is referenced in the MainWindow.xib file.
public partial class AppDelegate : UIApplicationDelegate
{
    // This method is invoked when the application has loaded its UI and its ready to run
    public override bool FinishedLaunching (UIApplication app, NSDictionary options)
    {
        // If you have defined a view, add it here:
        //window.AddSubview (navigationController.View);

        NavigationController navigationController = new NavigationController(new CustomersTableViewController(UITableViewStyle.Plain));
        window.AddSubview (navigationController.View);

        window.MakeKeyAndVisible ();

        return true;
    }
}

图形界面显示正确,但是当我单击搜索栏时,应用程序崩溃并出现以下错误。

Stacktrace:

 at (wrapper managed-to-native) MonoTouch.UIKit.UIApplication.UIApplicationMain (int,string[],intptr,intptr) <IL 0x0009f, 0xffffffff>
 at MonoTouch.UIKit.UIApplication.Main (string[],string,string) [0x00042] in /Developer/MonoTouch/Source/monotouch/src/UIKit/UIApplication.cs:29
 at MonoTouch.UIKit.UIApplication.Main (string[]) [0x00000] in /Developer/MonoTouch/Source/monotouch/src/UIKit/UIApplication.cs:34
 at TestSearchBar.Application.Main (string[]) [0x00000] in /Users/lorenzo/Projects/TestSearchBar/TestSearchBar/Main.cs:13
 at (wrapper runtime-invoke) <Module>.runtime_invoke_void_object (object,intptr,intptr,intptr) <IL 0x00050, 0xffffffff>

Native stacktrace:

0   TestSearchBar                       0x000d1e9c mono_handle_native_sigsegv + 343
1   TestSearchBar                       0x000100e0 mono_sigsegv_signal_handler + 322
2   libSystem.B.dylib                   0x9373205b _sigtramp + 43
3   ???                                 0xffffffff 0x0 + 4294967295
4   UIKit                               0x01d73227 -[UITextField canBecomeFirstResponder] + 204
5   UIKit                               0x01da7226 -[UIResponder becomeFirstResponder] + 171
6   UIKit                               0x01f6f961 -[UITextInteractionAssistant setFirstResponderIfNecessary] + 208
7   UIKit                               0x01f725e2 -[UITextInteractionAssistant oneFingerTap:] + 1676
8   UIKit                               0x01f694f2 -[UIGestureRecognizer _updateGestureWithEvent:] + 730
9   UIKit                               0x01f654fe -[UIGestureRecognizer _delayedUpdateGesture] + 47
10  UIKit                               0x01f6bafc _UIGestureRecognizerUpdateObserver + 584
11  UIKit                               0x01f6bce1 _UIGestureRecognizerUpdateGesturesFromSendEvent + 51
12  UIKit                               0x01cff32a -[UIWindow _sendGesturesForEvent:] + 1292
13  UIKit                               0x01cfaca3 -[UIWindow sendEvent:] + 105
14  UIKit                               0x01cddc37 -[UIApplication sendEvent:] + 447
15  UIKit                               0x01ce2f2e _UIApplicationHandleEvent + 7576
16  GraphicsServices                    0x04053992 PurpleEventCallback + 1550
17  CoreFoundation                      0x00ea6944 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 52
18  CoreFoundation                      0x00e06cf7 __CFRunLoopDoSource1 + 215
19  CoreFoundation                      0x00e03f83 __CFRunLoopRun + 979
20  CoreFoundation                      0x00e03840 CFRunLoopRunSpecific + 208
21  CoreFoundation                      0x00e03761 CFRunLoopRunInMode + 97
22  GraphicsServices                    0x040521c4 GSEventRunModal + 217
23  GraphicsServices                    0x04052289 GSEventRun + 115
24  UIKit                               0x01ce6c93 UIApplicationMain + 1160
25  ???                                 0x077f6764 0x0 + 125790052
26  ???                                 0x077f6630 0x0 + 125789744
27  ???                                 0x077f5f60 0x0 + 125788000
28  ???                                 0x077f5eac 0x0 + 125787820
29  ???                                 0x077f5f37 0x0 + 125787959
30  TestSearchBar                       0x0000fe9b mono_jit_runtime_invoke + 1332
31  TestSearchBar                       0x001ee961 mono_runtime_invoke + 137
32  TestSearchBar                       0x001f1048 mono_runtime_exec_main + 669
33  TestSearchBar                       0x001f0432 mono_runtime_run_main + 843
34  TestSearchBar                       0x000a3f9e mono_jit_exec + 200
35  TestSearchBar                       0x002a3d21 main + 3733
36  TestSearchBar                       0x00003179 _start + 208
37  TestSearchBar                       0x000030a8 start + 40

=================================================================
Got a SIGSEGV while executing native code. This usually indicates
a fatal error in the mono runtime or one of the native libraries 
used by your application.
=================================================================

有什么建议吗?这是MT错误吗?我是不是错过了什么?先感谢您。

编辑: 我正在运行 MT 4.2.2 和 Mono 2.10.4。 Mono 2.10.5 中崩溃仍然存在。

I have a simple test application with a NavigationController that extends UINavigationController

[MonoTouch.Foundation.Register("NavigationController")]
public class NavigationController : UINavigationController
{
    public NavigationController (IntPtr handle) : base (handle)
    {

    }

    [Export ("initWithCoder:")]
    public NavigationController (NSCoder coder) : base (coder)
    {

    }

    public NavigationController (UIViewController rootViewController) : base(rootViewController)
    {

    }

    public override void ViewDidLoad ()
    {
        base.ViewDidLoad ();

        View.BackgroundColor = UIColor.Green;
    }
}

I have also a CustomersTableViewController that extends UITableViewController (here Source is not influential). In ViewDidLoad method I create a UISearchBar and I add it to TableHeaderView.

public class CustomersTableViewController : UITableViewController
{
    UISearchBar _searchBar;

    public CustomersTableViewController(UITableViewStyle style) : base(style)
    {
    }

    public override void ViewDidLoad ()
    {
        base.ViewDidLoad ();

        _searchBar = CreateSearchBar ();
        _searchBar.SearchButtonClicked += OnSearchBarSearchButtonClicked;

        TableView.TableHeaderView = _searchBar;

    }

    void OnSearchBarSearchButtonClicked (object sender, EventArgs e)
    {

    }

    static UISearchBar CreateSearchBar ()
    {
        UISearchBar search = new UISearchBar ();
        search.Placeholder = "Search Customers";
        search.SizeToFit ();
        search.AutocorrectionType = UITextAutocorrectionType.No;
        search.AutocapitalizationType = UITextAutocapitalizationType.None;
        return search;
    }
}

In Main.cs, I run the following snippet of code. If I create the navigationController by code or through xib, it is the same.

// The name AppDelegate is referenced in the MainWindow.xib file.
public partial class AppDelegate : UIApplicationDelegate
{
    // This method is invoked when the application has loaded its UI and its ready to run
    public override bool FinishedLaunching (UIApplication app, NSDictionary options)
    {
        // If you have defined a view, add it here:
        //window.AddSubview (navigationController.View);

        NavigationController navigationController = new NavigationController(new CustomersTableViewController(UITableViewStyle.Plain));
        window.AddSubview (navigationController.View);

        window.MakeKeyAndVisible ();

        return true;
    }
}

The graphic interface is displayed correctly, but when I click on the searchBar the application crashes with the following error.

Stacktrace:

 at (wrapper managed-to-native) MonoTouch.UIKit.UIApplication.UIApplicationMain (int,string[],intptr,intptr) <IL 0x0009f, 0xffffffff>
 at MonoTouch.UIKit.UIApplication.Main (string[],string,string) [0x00042] in /Developer/MonoTouch/Source/monotouch/src/UIKit/UIApplication.cs:29
 at MonoTouch.UIKit.UIApplication.Main (string[]) [0x00000] in /Developer/MonoTouch/Source/monotouch/src/UIKit/UIApplication.cs:34
 at TestSearchBar.Application.Main (string[]) [0x00000] in /Users/lorenzo/Projects/TestSearchBar/TestSearchBar/Main.cs:13
 at (wrapper runtime-invoke) <Module>.runtime_invoke_void_object (object,intptr,intptr,intptr) <IL 0x00050, 0xffffffff>

Native stacktrace:

0   TestSearchBar                       0x000d1e9c mono_handle_native_sigsegv + 343
1   TestSearchBar                       0x000100e0 mono_sigsegv_signal_handler + 322
2   libSystem.B.dylib                   0x9373205b _sigtramp + 43
3   ???                                 0xffffffff 0x0 + 4294967295
4   UIKit                               0x01d73227 -[UITextField canBecomeFirstResponder] + 204
5   UIKit                               0x01da7226 -[UIResponder becomeFirstResponder] + 171
6   UIKit                               0x01f6f961 -[UITextInteractionAssistant setFirstResponderIfNecessary] + 208
7   UIKit                               0x01f725e2 -[UITextInteractionAssistant oneFingerTap:] + 1676
8   UIKit                               0x01f694f2 -[UIGestureRecognizer _updateGestureWithEvent:] + 730
9   UIKit                               0x01f654fe -[UIGestureRecognizer _delayedUpdateGesture] + 47
10  UIKit                               0x01f6bafc _UIGestureRecognizerUpdateObserver + 584
11  UIKit                               0x01f6bce1 _UIGestureRecognizerUpdateGesturesFromSendEvent + 51
12  UIKit                               0x01cff32a -[UIWindow _sendGesturesForEvent:] + 1292
13  UIKit                               0x01cfaca3 -[UIWindow sendEvent:] + 105
14  UIKit                               0x01cddc37 -[UIApplication sendEvent:] + 447
15  UIKit                               0x01ce2f2e _UIApplicationHandleEvent + 7576
16  GraphicsServices                    0x04053992 PurpleEventCallback + 1550
17  CoreFoundation                      0x00ea6944 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 52
18  CoreFoundation                      0x00e06cf7 __CFRunLoopDoSource1 + 215
19  CoreFoundation                      0x00e03f83 __CFRunLoopRun + 979
20  CoreFoundation                      0x00e03840 CFRunLoopRunSpecific + 208
21  CoreFoundation                      0x00e03761 CFRunLoopRunInMode + 97
22  GraphicsServices                    0x040521c4 GSEventRunModal + 217
23  GraphicsServices                    0x04052289 GSEventRun + 115
24  UIKit                               0x01ce6c93 UIApplicationMain + 1160
25  ???                                 0x077f6764 0x0 + 125790052
26  ???                                 0x077f6630 0x0 + 125789744
27  ???                                 0x077f5f60 0x0 + 125788000
28  ???                                 0x077f5eac 0x0 + 125787820
29  ???                                 0x077f5f37 0x0 + 125787959
30  TestSearchBar                       0x0000fe9b mono_jit_runtime_invoke + 1332
31  TestSearchBar                       0x001ee961 mono_runtime_invoke + 137
32  TestSearchBar                       0x001f1048 mono_runtime_exec_main + 669
33  TestSearchBar                       0x001f0432 mono_runtime_run_main + 843
34  TestSearchBar                       0x000a3f9e mono_jit_exec + 200
35  TestSearchBar                       0x002a3d21 main + 3733
36  TestSearchBar                       0x00003179 _start + 208
37  TestSearchBar                       0x000030a8 start + 40

=================================================================
Got a SIGSEGV while executing native code. This usually indicates
a fatal error in the mono runtime or one of the native libraries 
used by your application.
=================================================================

Any suggestions? Is this a MT bug? Do I have missed something? Thank you in advance.

EDIT:
I'm running MT 4.2.2 with Mono 2.10.4. The crash still persists also with Mono 2.10.5.

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

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

发布评论

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

评论(1

小傻瓜 2024-12-14 02:42:03

将您的:

   NavigationController navigationController = new NavigationController(new CustomersTableViewController(UITableViewStyle.Plain));

从局部变量(GC 可以在不再使用时立即收集)更改为字段(只要类型的实例存在,该引用就会保持活动状态)。例如,

   private NavigationController navigationController;
   ...

   navigationController = new NavigationController(new CustomersTableViewController(UITableViewStyle.Plain));

请注意:

   window.AddSubview (navigationController.View);

只会保留对 View 的引用,而不是其父级的引用(常见错误)。

Change your:

   NavigationController navigationController = new NavigationController(new CustomersTableViewController(UITableViewStyle.Plain));

from a local variable (which the GC can collect as soon as it's not used anymore) into a field (which will keep the reference alive as long as the type's instance exists). E.g.

   private NavigationController navigationController;
   ...

   navigationController = new NavigationController(new CustomersTableViewController(UITableViewStyle.Plain));

Note that:

   window.AddSubview (navigationController.View);

will only keep a reference to the View, not to it's parent (common mistake).

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