MonoTouch NullReference for every event/Action (SI

2019-05-15 23:42发布

问题:

The problem I'm having is that in my brand new project I have a Window with a button (Outlet and Action defined) and in my C# code I've wired up the partial void implementation of the Action. Then I have a View that I load and do the same thing.

The issue is that the View causes a System.NullReferenceException, the 'Click' code never gets run, instead I end up with the following full trace

Loaded assembly: /Developer/MonoTouch/usr/lib/mono/2.1/monotouch.dll [External]
Loaded assembly: /Developer/MonoTouch/usr/lib/mono/2.1/System.Core.dll [External]
Loaded assembly: /Users/drisszouak/Projects/ZenVelopes/ZenVelopes/bin/iPhoneSimulator/Debug/ZenVelopes.exe
Stacktrace:

  at (wrapper managed-to-native) MonoTouch.UIKit.UIApplication.UIApplicationMain (int,string[],intptr,intptr) <0x00004>
  at (wrapper managed-to-native) MonoTouch.UIKit.UIApplication.UIApplicationMain (int,string[],intptr,intptr) <0x00004>
  at MonoTouch.UIKit.UIApplication.Main (string[],string,string) [0x00038] in /Users/plasma/Source/iphone/monotouch/UIKit/UIApplication.cs:26
  at MonoTouch.UIKit.UIApplication.Main (string[]) [0x00000] in /Users/plasma/Source/iphone/monotouch/UIKit/UIApplication.cs:31
  at ZenVelopes.Application.Main (string[]) [0x00000] in /Users/drisszouak/Projects/ZenVelopes/ZenVelopes/Main.cs:14
  at (wrapper runtime-invoke) <Module>.runtime_invoke_void_object (object,intptr,intptr,intptr) <IL 0x0001d, 0x00090>

Native stacktrace:

    0   ZenVelopes                          0x000be66f mono_handle_native_sigsegv + 343
    1   ZenVelopes                          0x0000e43e mono_sigsegv_signal_handler + 313
    2   libSystem.B.dylib                   0x9118246b _sigtramp + 43
    3   ???                                 0xffffffff 0x0 + 4294967295
    4   UIKit                               0x01def167 -[UIBarButtonItem(UIInternal) _sendAction:withEvent:] + 156
    5   UIKit                               0x01be1a6e -[UIApplication sendAction:to:from:forEvent:] + 119
    6   UIKit                               0x01c701b5 -[UIControl sendAction:to:forEvent:] + 67
    7   UIKit                               0x01c72647 -[UIControl(Internal) _sendActionsForEvents:withEvent:] + 527
    8   UIKit                               0x01c711f4 -[UIControl touchesEnded:withEvent:] + 458
    9   UIKit                               0x01c060d1 -[UIWindow _sendTouchesForEvent:] + 567
    10  UIKit                               0x01be737a -[UIApplication sendEvent:] + 447
    11  UIKit                               0x01bec732 _UIApplicationHandleEvent + 7576
    12  GraphicsServices                    0x03eb7a36 PurpleEventCallback + 1550
    13  CoreFoundation                      0x00df9064 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 52
    14  CoreFoundation                      0x00d596f7 __CFRunLoopDoSource1 + 215
    15  CoreFoundation                      0x00d56983 __CFRunLoopRun + 979
    16  CoreFoundation                      0x00d56240 CFRunLoopRunSpecific + 208
    17  CoreFoundation                      0x00d56161 CFRunLoopRunInMode + 97
    18  GraphicsServices                    0x03eb6268 GSEventRunModal + 217
    19  GraphicsServices                    0x03eb632d GSEventRun + 115
    20  UIKit                               0x01bf042e UIApplicationMain + 1160
    21  ???                                 0x09e39862 0x0 + 165910626
    22  ???                                 0x09e39632 0x0 + 165910066
    23  ???                                 0x09e38c9a 0x0 + 165907610
    24  ???                                 0x09e38aec 0x0 + 165907180
    25  ???                                 0x09e38ba1 0x0 + 165907361
    26  ZenVelopes                          0x0000e202 mono_jit_runtime_invoke + 1360
    27  ZenVelopes                          0x001c92af mono_runtime_invoke + 137
    28  ZenVelopes                          0x001caf6b mono_runtime_exec_main + 714
    29  ZenVelopes                          0x001ca891 mono_runtime_run_main + 812
    30  ZenVelopes                          0x00094fe8 mono_jit_exec + 200
    31  ZenVelopes                          0x0027cf05 main + 3494
    32  ZenVelopes                          0x00002ca1 _start + 208
    33  ZenVelopes                          0x00002bd0 start + 40
    34  ???                                 0x00000002 0x0 + 2

Debug info from gdb:

warning: Could not find object file "/var/folders/Ny/NyElTwhDGD8kZMqIEeLGXE+++TI/-Tmp-//cc6F1tBs.o" - no debug information available for "template.m".

warning: .o file "/Developer/MonoTouch/SDKs/MonoTouch.iphonesimulator4.2.sdk/usr/lib/libmonotouch.a(zlib-helper.x86.42.o)" more recent than executable timestamp in "/Users/drisszouak/Library/Application Support/iPhone Simulator/4.2/Applications/9B8EA00C-09B4-4D08-ABF7-7CF4487F1A28/ZenVelopes.app/ZenVelopes"
warning: Could not open OSO file /Developer/MonoTouch/SDKs/MonoTouch.iphonesimulator4.2.sdk/usr/lib/libmonotouch.a(zlib-helper.x86.42.o) to scan for pubtypes for objfile /Users/drisszouak/Library/Application Support/iPhone Simulator/4.2/Applications/9B8EA00C-09B4-4D08-ABF7-7CF4487F1A28/ZenVelopes.app/ZenVelopes
warning: .o file "/Developer/MonoTouch/SDKs/MonoTouch.iphonesimulator4.2.sdk/usr/lib/libmonotouch.a(monotouch-glue.x86.42.o)" more recent than executable timestamp in "/Users/drisszouak/Library/Application Support/iPhone Simulator/4.2/Applications/9B8EA00C-09B4-4D08-ABF7-7CF4487F1A28/ZenVelopes.app/ZenVelopes"
warning: Could not open OSO file /Developer/MonoTouch/SDKs/MonoTouch.iphonesimulator4.2.sdk/usr/lib/libmonotouch.a(monotouch-glue.x86.42.o) to scan for pubtypes for objfile /Users/drisszouak/Library/Application Support/iPhone Simulator/4.2/Applications/9B8EA00C-09B4-4D08-ABF7-7CF4487F1A28/ZenVelopes.app/ZenVelopes
warning: .o file "/Developer/MonoTouch/SDKs/MonoTouch.iphonesimulator4.2.sdk/usr/lib/libmonotouch.a(gc.x86.42.o)" more recent than executable timestamp in "/Users/drisszouak/Library/Application Support/iPhone Simulator/4.2/Applications/9B8EA00C-09B4-4D08-ABF7-7CF4487F1A28/ZenVelopes.app/ZenVelopes"
warning: Could not open OSO file /Developer/MonoTouch/SDKs/MonoTouch.iphonesimulator4.2.sdk/usr/lib/libmonotouch.a(gc.x86.42.o) to scan for pubtypes for objfile /Users/drisszouak/Library/Application Support/iPhone Simulator/4.2/Applications/9B8EA00C-09B4-4D08-ABF7-7CF4487F1A28/ZenVelopes.app/ZenVelopes
warning: .o file "/Developer/MonoTouch/SDKs/MonoTouch.iphonesimulator4.2.sdk/usr/lib/libmonotouch.a(monotouch-glue.x86.42.o)" more recent than executable timestamp in "/Users/drisszouak/Library/Application Support/iPhone Simulator/4.2/Applications/9B8EA00C-09B4-4D08-ABF7-7CF4487F1A28/ZenVelopes.app/ZenVelopes"
warning: Couldn't open object file '/Developer/MonoTouch/SDKs/MonoTouch.iphonesimulator4.2.sdk/usr/lib/libmonotouch.a(monotouch-glue.x86.42.o)'
Attaching to process 10857.
Reading symbols for shared libraries . done
Reading symbols for shared libraries ...................................................................................................................... done
0x91127459 in read$UNIX2003 ()
  7 "WebThread"                     0x9111c0fa in mach_msg_trap ()
  6                                 0x9114a0a6 in __semwait_signal ()
  5                                 0x911595fe in recvfrom$UNIX2003 ()
  4                                 0x9111c136 in semaphore_wait_trap ()
  3                                 0x9111c0fa in mach_msg_trap ()
  2 "com.apple.libdispatch-manager" 0x91142982 in kevent ()
* 1 "com.apple.main-thread"         0x91127459 in read$UNIX2003 ()

Thread 7 (process 10857):
#0  0x9111c0fa in mach_msg_trap ()
#1  0x9111c867 in mach_msg ()
#2  0x00df94a6 in __CFRunLoopServiceMachPort ()
#3  0x00d56874 in __CFRunLoopRun ()
#4  0x00d56240 in CFRunLoopRunSpecific ()
#5  0x00d56161 in CFRunLoopRunInMode ()
#6  0x04f7c423 in RunWebThread ()
#7  0x9114985d in _pthread_start ()
#8  0x911496e2 in thread_start ()

Thread 6 (process 10857):
#0  0x9114a0a6 in __semwait_signal ()
#1  0x91175ee5 in nanosleep$UNIX2003 ()
#2  0x91175e23 in usleep$UNIX2003 ()
#3  0x0027714c in monotouch_pump_gc ()
#4  0x9114985d in _pthread_start ()
#5  0x911496e2 in thread_start ()

Thread 5 (process 10857):
#0  0x911595fe in recvfrom$UNIX2003 ()
#1  0x911793c7 in recv$UNIX2003 ()
#2  0x000d8cca in recv_length (fd=6, buf=0xb0388ebd, len=11, flags=0) at ../../../../mono/mini/debugger-agent.c:939
#3  0x000e616a in debugger_thread (arg=0x0) at ../../../../mono/mini/debugger-agent.c:6290
#4  0x002448e2 in thread_start_routine (args=0x80339a8) at ../../../../mono/io-layer/wthreads.c:286
#5  0x00274357 in GC_start_routine (arg=0x6ff6f60) at ../../../libgc/pthread_support.c:1390
#6  0x9114985d in _pthread_start ()
#7  0x911496e2 in thread_start ()

Thread 4 (process 10857):
#0  0x9111c136 in semaphore_wait_trap ()
#1  0x0015ae1d in finalizer_thread (unused=0x0) at ../../../../mono/metadata/gc.c:1026
#2  0x002034a3 in start_wrapper (data=0x7b18620) at ../../../../mono/metadata/threads.c:661
#3  0x002448e2 in thread_start_routine (args=0x80338b0) at ../../../../mono/io-layer/wthreads.c:286
#4  0x00274357 in GC_start_routine (arg=0x6ff6f60) at ../../../libgc/pthread_support.c:1390
#5  0x9114985d in _pthread_start ()
#6  0x911496e2 in thread_start ()

Thread 3 (process 10857):
#0  0x9111c0fa in mach_msg_trap ()
#1  0x9111c867 in mach_msg ()
#2  0x0011cc46 in mach_exception_thread (arg=0x0) at ../../../../mono/mini/mini-darwin.c:138
#3  0x9114985d in _pthread_start ()
#4  0x911496e2 in thread_start ()

Thread 2 (process 10857):
#0  0x91142982 in kevent ()
#1  0x9114309c in _dispatch_mgr_invoke ()
#2  0x91142559 in _dispatch_queue_invoke ()
#3  0x911422fe in _dispatch_worker_thread2 ()
#4  0x91141d81 in _pthread_wqthread ()
#5  0x91141bc6 in start_wqthread ()

Thread 1 (process 10857):
#0  0x91127459 in read$UNIX2003 ()
#1  0x000be81f in mono_handle_native_sigsegv (signal=11, ctx=0xbfffd228) at ../../../../mono/mini/mini-exceptions.c:1826
#2  0x0000e43e in mono_sigsegv_signal_handler (_dummy=10, info=0xbfffd1e8, context=0xbfffd228) at ../../../../mono/mini/mini.c:4846
#3  <signal handler called>
#4  0x028d6a63 in objc_msgSend ()
#5  0x07b96550 in ?? ()
#6  0x01def167 in -[UIBarButtonItem(UIInternal) _sendAction:withEvent:] ()
#7  0x01be1a6e in -[UIApplication sendAction:to:from:forEvent:] ()
#8  0x01c701b5 in -[UIControl sendAction:to:forEvent:] ()
#9  0x01c72647 in -[UIControl(Internal) _sendActionsForEvents:withEvent:] ()
#10 0x01c711f4 in -[UIControl touchesEnded:withEvent:] ()
#11 0x01c060d1 in -[UIWindow _sendTouchesForEvent:] ()
#12 0x01be737a in -[UIApplication sendEvent:] ()
#13 0x01bec732 in _UIApplicationHandleEvent ()
#14 0x03eb7a36 in PurpleEventCallback ()
#15 0x00df9064 in __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ ()
#16 0x00d596f7 in __CFRunLoopDoSource1 ()
#17 0x00d56983 in __CFRunLoopRun ()
#18 0x00d56240 in CFRunLoopRunSpecific ()
#19 0x00d56161 in CFRunLoopRunInMode ()
#20 0x03eb6268 in GSEventRunModal ()
#21 0x03eb632d in GSEventRun ()
#22 0x01bf042e in UIApplicationMain ()
#23 0x09e39862 in ?? ()
#24 0x09e39632 in ?? ()
#25 0x09e38c9a in ?? ()
#26 0x09e38aec in ?? ()
#27 0x09e38ba1 in ?? ()
#28 0x0000e202 in mono_jit_runtime_invoke (method=0x8119afc, obj=0x0, params=0xbfffed9c, exc=0x0) at ../../../../mono/mini/mini.c:4733
#29 0x001c92af in mono_runtime_invoke (method=0x8119afc, obj=0x0, params=0xbfffed9c, exc=0x0) at ../../../../mono/metadata/object.c:2615
#30 0x001caf6b in mono_runtime_exec_main (method=0x8119afc, args=0x9e0d1a0, exc=0x0) at ../../../../mono/metadata/object.c:3581
#31 0x001ca891 in mono_runtime_run_main (method=0x8119afc, argc=0, argv=0xbfffeed4, exc=0x0) at ../../../../mono/metadata/object.c:3355
#32 0x00094fe8 in mono_jit_exec (domain=0x6f91e58, assembly=0x7b8ca10, argc=1, argv=0xbfffeed0) at ../../../../mono/mini/driver.c:1094
#33 0x0027cf05 in main ()

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

On the advice of a friend, I uninstalled Mono, MonoTouch, MonoDevelop and the iOS SDK and then redownloaded them, reinstalled them in iOS SDK, Mono, MT (Personal licensed edition), MD order. This didn't change a thing.

The line #14 that is referenced in my Main is this one:

public class Application
    {
        static void Main (string[] args)
        {
    #14 ====>  UIApplication.Main (args);
        }
    }

The Partial void 'click' event that I'm wiring up is this (solely for testing purposes). The code never arrives here, the exception blows up before it.

partial void clickNewEnvelope (UIBarButtonItem sender)
        {
            int x;
            x = 1;
        }

When I create a new project, I'm getting the same thing.

I am seriously stumped, help.

回答1:

It looks like you're dangling an event off a UIBarButtonItem and not keeping a reference to it. Any time you do

foo.SomeEvent += delegate {};

You need to keep a reference to "foo" so that the hidden class which implements the protocol isn't collected, as the UIBarButtonItem in this case is the owner of that hidden class.