1

I have an monotouch application that includes an objective-c static library. The application runs correctly on the simulator, but when I try run the app on my iPhone 3GS, it crashes on startup. These are the steps that I have taken to try get it working:

  • Compiled the static library in Xcode with Device specified and active architecture set at armv6 and at armv7 (I am not sure which is correct, but I tried both and neither worked).
  • Under the project info I set code signing identity to my developer key.
  • In MonoDevelop I have included the static library in the application project options by setting the additional monotouch arguments under iPhone build to have the following value (this is identical to what is set for iPhoneSimulator):

    -v -v -v -gcc_flags "-lstdc++ -I${ProjectDir}/Ultralite/Include -L${ProjectDir}/Ultralite -lUltralite -force_load ${ProjectDir}/Ultralite/libUltralite.a"

When I try run the application, it crashes on startup (so the application screen does not even appear). In MonoDevelop all that I see is the following exception message:

Exception of type 'Mono.Debugger.Soft.VMDisconnectedException' was thrown.

All that I see in the device log in Xcode is the following:

Exception Type:  EXC_BAD_ACCESS (SIGSEGV)
Exception Codes: KERN_PROTECTION_FAILURE at 0x2fd00f24

If I remove the -gcc_flags option from the project options, then the application starts up, but crashes at the first attempt at accessing the static library. So it is definitely something to do with the static library that is causing the application to crash on startup.

I have no idea where to even begin with solving this, and so really need some help on this one. Anyone got any ideas as to what is wrong with the static library that I am including, or know where I can get more information about what is going wrong? The KERN_PROTECTION_FAILURE message in the crash report is really not giving me much to work with.

Update: I have created a simple Hello World application which has one button, which when clicked calls a method sayHello in a static library. Even with this basic example I encountered the same problem; namely that it runs on the simulator but not on the actual device. I have uploaded my helloworld example to github. I would really appreciate it if someone could help me in getting this working. Here is the Hello World sample:

https://github.com/BruceHill/HelloWorld

This includes a folder for the basic application, a folder with the objective-c static library and then finally a folder for the btouch definition. I call btouch with the parameter -outdir=. to build Messaging.g.cs and UltraliteManager.g.cs which I then include in the MonoTouch application.

BruceHill
  • 6,954
  • 8
  • 62
  • 114
  • Extract from the crash report: Thread 0 name: Dispatch queue: com.apple.main-thread Thread 0 Crashed: 0 Mobileforms 0x00fc0c1c mono_aot_get_class_from_name (aot-runtime.c:1497) 1 Mobileforms 0x010225b0 mono_class_from_name (class.c:7092) 2 Mobileforms 0x0103ce80 mono_exception_from_name_domain (exception.c:58) 3 Mobileforms 0x0103ce38 mono_exception_from_name (exception.c:35) 4 Mobileforms 0x0103d418 mono_get_exception_null_reference (exception.c:280) – BruceHill May 09 '11 at 09:40
  • I am now a step closer to solving this problem. I have managed to get my _Hello World_ sample working by changing the _Linker behavior_ setting in the build options from _Don't link_ to _Link SDK assemblies only_. However when I make this change in the actual application, I get an error "mtouch failed with no output" and under the build log I see linker errors stating that symbols are not defined. For example, it says that "_SecRandomCopyBytes" and "_SecKeyGetBlockSize" in libUltralite.a are undefined. – BruceHill May 10 '11 at 08:38

4 Answers4

3

I had to make two changes to get this working correctly on the iphone:

  1. linker behavior in the build options had to have the option Link all assemblies set.
  2. I had to add -framework Security to the gcc_flags.

So additional monotouch arguments under the build options had to have the following value:

-v -v -v -gcc_flags "-framework Security -lstdc++ -I${ProjectDir}/Ultralite/Include -L${ProjectDir}/Ultralite -lUltralite -force_load ${ProjectDir}/Ultralite/libUltralite.a"

The reason I had to add the Security framework is that it seems MonoTouch includes this framework when Don't link is specified in the build options, but does not include it when the other two options are set. I determined this by comparing the build logs for the different options.

BruceHill
  • 6,954
  • 8
  • 62
  • 114
1

Open XCode Organizer. Plug in your device. Then look at the crashes. The data will symbolicate and you'd at least see at what point it failed.

Ian Vink
  • 66,960
  • 104
  • 341
  • 555
  • Thanks, Ivink. I did figure out evenutally that I could see a crash log in XCode organizer. Unfortunately, all the symbols listed there seem to suggest that the crash is happening at mono level, so nothing in the log makes any sense to me. But I have made a discovery regarding this error by doing tests on my Hello World sample. Seems this error has something to do with the _Linker behaviour_ setting in the options. See my latest comment on this question for details. – BruceHill May 10 '11 at 08:04
1

I had a similar problem with MonoTouch linking a 3rd party library through BTouch. Same exception types and codes.

You have to get the source code for that 3rd party library and compile it with THUMB disabled. XCode has this option, just do a search for THUMB in the options. libUltralite.a will end up being a little bit larger in size.

Matt Hill
  • 11
  • 1
  • Thanks, Matt. I had previously encountered the problem with THUMB and that is disabled on my library. But I am a step closer to solving this; see my latest comment on this question. – BruceHill May 10 '11 at 08:17
0

Try adding "-ObjC" to your linker flags.

Update: There seems to be a some issues regarding static libraties. You may also try to use the -all_load flag: What does the -all_load linker flag do?

Community
  • 1
  • 1
Claus Broch
  • 9,212
  • 2
  • 29
  • 38
  • Thanks for replying, Claus. I have tried adding _-ObjC_ and it does not make a difference. Just to point out, though, that my static library is a wrapper around a c++ library, and therefore the inclusion of _-lstdc++_ in the linker flags. What does the inclusion of _ObjC_ to the flags do? – BruceHill May 09 '11 at 09:30
  • There is a thorough explanation of the -ObjC flag in the QA: http://developer.apple.com/library/mac/#qa/qa1490/_index.html – Claus Broch May 09 '11 at 09:54
  • I already tried the -all_load flag and unfortunately it did not help. :( – BruceHill May 09 '11 at 10:59