iOS native library Xamarin binding - c#

The problem has already been discussed earlier, but I got it after the update in September. Environment: the latest vs studio for mac, all the frameworks are updated, I also tried to switch to the preview channel, the error is the same. Actually the problem: after the update, the error of creating a native object began to appear.
From my renderer to - at Foundation.NSObject.InitializeObject (System.Boolean alloced) [0x0002e] in /Library/Frameworks/Xamarin.iOS.framework/Versions/13.8.2.2/src/Xamarin.iOS/Foundation/NSObject2.cs:196 at Foundation.NSObject..ctor (Foundation.NSObjectFlag x) [0x0000d] in /Library/Frameworks/Xamarin.iOS.framework/Versions/13.8.2.2/src/Xamarin.iOS/Foundation/NSObject2.cs:131 at UIKit.UIResponder..ctor (Foundation.NSObjectFlag t) [0x00000] in /Library/Frameworks/Xamarin.iOS.framework/Versions/13.8.2.2/src/Xamarin.iOS/UIResponder.g.cs:69 at UIKit.UIView..ctor (Foundation.NSObjectFlag t) [0x00000] in /Library/Frameworks/Xamarin.iOS.framework/Versions/13.8.2.2/src/Xamarin.iOS/UIView.g.cs:83
Error message: Could not create an native instance of the type 'xxx': the native class hasn't been loaded. It is possible to ignore this condition by setting ObjCRuntime.Class.ThrowOnInitFailure to false.
if set ObjCRuntime.Class.ThrowOnInitFailure = false; - native constructor returns null;
lib in Binding project is fat, all architectures are supported. i have no its code, only headers. ApiDefinition and Structs generated by sharpie, and works well.
mtouch options: --optimize=-remove-dynamic-registrar
please give some ways to solve, project is on fire..
If exist way to rollback all mono frameworks i can try, but its not good way.

Related

C# mod dependency throws 'TypeLoadException' when instantiated?

Issue
When I try to use a 3rd-party library in my C# project, it almost always throws 'TypeLoadException' at me.
Details
The dependency I'm using in this case is a mongodb driver, though this issue replicates with other libraries such as Json.net and MySQL. I've tested with my own self-created small 'dependency' class and I was able to use this without it throwing an exception at me.
I'm able to build successfully, but when the .dll is loaded as a mod for a game, it logs out this rather unhelpful stack trace and fails to execute anything.
TypeLoadException: A type load exception has occurred.
at Test.Api.GameAwake () [0x00000] in <filename unknown>:0
at ModManager.GameAwake () [0x00000] in <filename unknown>:0
UnityEngine.DebugLogHandler:Internal_LogException(Exception, Object)
UnityEngine.DebugLogHandler:LogException(Exception, Object)
UnityEngine.Logger:LogException(Exception, Object)
UnityEngine.Debug:LogException(Exception)
Logger:masterLogException(Exception)
Logger:Exception(Exception)
Log:Exception(Exception)
ModManager:GameAwake()
GameManager:Awake()
All that resides in 'Test.Api.GameAwake' is a simple connection handler taken from the mongodb documentation. There's nothing else going on in this project.
I can see that the assemblies do seem to load correctly, so I'm really out of ideas as to what to do next.
Could be a specific version dependency problem, or a missing assembly that you're not seeing.
I recommend checking a "dependency spy" program, such as http://ilspy.net/ and trying to load your assembly plugin to see what it's looking for.

HowTo: uPD with unity 5

First post. Let me know if I break any rules!
I would like to experiment with Pure Data and Unity 5. It seems uPD would be the best fit. I have followed the instructions on the github page and loaded the first sample scene. The unity console reports errors with the dll:
Failed to load 'Assets/Magicolo/AudioTools/PureData/Plugins/libpdcsharp.dll', expected 64 bit architecture (IMAGE_FILE_MACHINE_AMD64), but was IMAGE_FILE_MACHINE_I386. You must recompile your plugin for 64 bit architecture.
So I cloned the main libpd repo and used the included batch file to build an x64 dll. I replaced the libpdcsharp.dll in the unity project and the console now reports:
DllNotFoundException: Assets/Magicolo/AudioTools/PureData/Plugins/libpdcsharp.dll
LibPDBinding.LibPD.ProcessArgs[Int32] (System.Int32[] args, System.String& debug) (at Assets/Magicolo/AudioTools/PureData/LibPD/LibPDNativeMessaging.cs:402)
LibPDBinding.LibPD.SendMessage[Int32] (System.String receiver, System.String message, System.Int32[] args) (at Assets/Magicolo/AudioTools/PureData/LibPD/LibPDNativeMessaging.cs:354)
(wrapper synchronized) LibPDBinding.LibPD:SendMessage (string,string,int[])
LibPDBinding.LibPD.ComputeAudio (Boolean state) (at Assets/Magicolo/AudioTools/PureData/LibPD/LibPDNativeMethods.cs:238)
(wrapper synchronized) LibPDBinding.LibPD:ComputeAudio (bool)
LibPDBinding.LibPD.Release () (at Assets/Magicolo/AudioTools/PureData/LibPD/LibPDNativeMethods.cs:207)
(wrapper synchronized) LibPDBinding.LibPD:Release ()
LibPDBinding.LibPD.ReInit () (at Assets/Magicolo/AudioTools/PureData/LibPD/LibPDNativeMethods.cs:79)
(wrapper synchronized) LibPDBinding.LibPD:ReInit ()
LibPDBinding.LibPD..cctor () (at Assets/Magicolo/AudioTools/PureData/LibPD/LibPDNativeMethods.cs:65)
Rethrow as TypeInitializationException: An exception was thrown by the type initializer for LibPDBinding.LibPD
Magicolo.AudioTools.PureDataBridge.SetAudioSettings () (at Assets/Magicolo/AudioTools/PureData/PureDataBridge.cs:46)
Magicolo.AudioTools.PureDataBridge.StartLibPD () (at Assets/Magicolo/AudioTools/PureData/PureDataBridge.cs:25)
Magicolo.AudioTools.PureDataBridge.Start () (at Assets/Magicolo/AudioTools/PureData/PureDataBridge.cs:50)
PureData.StartAll () (at Assets/Magicolo/AudioTools/PureData/PureData.cs:141)
PureData.Initialize () (at Assets/Magicolo/AudioTools/PureData/PureData.cs:58)
PureData.Awake () (at Assets/Magicolo/AudioTools/PureData/PureData.cs:173)
I have also tried copying the libPDBinding.dll file from the main libpd repo to the unity project no avail. I lack the understanding to know where to go next so any help at all would be appreciated!
Further Progress
It was suggested to try NuGet to obtain the binding dll. On this - I get an error about a framework mismatch between my unity project/visual studio solution and the nuget package libPDBinding version 0.10.0. This is to do with unitys profiles as described in an article titled: "Using NuGet in Visual Studio Tools for Unity". I cant post the link here because I am too new.
However, I think I am sucessful in compiling my own libPDBinding.dll. Apologies for dragging this out, just making sure I am doing this right:
After using libPD\mingw64_build_csharp.bat to compile a x64 version of libpdcsharp.dll, I reference this and libpd\libs\mingw64\libwinthread-1.dll in the VS solution and get the resulting LibPDBinding.dll which was built with .NET 3.5. As mentioned libpdcsharp.dll was compiled using the batch/make file, so am I correct to assume this file has been built with the correct settings?
I proceed to copy these 3 dll's the unity assets folder
For reference it's a blank unity project with uPD as the only package, loaded to a uPD example scene.
I still get some errors at run time and some visual studio warnings as follows.
Unity run time error:
EntryPointNotFoundException: libpd_safe_init
LibPDBinding.LibPD.ReInit () (at Assets/Magicolo/AudioTools/PureData/LibPD/LibPDNativeMethods.cs:81)
(wrapper synchronized) LibPDBinding.LibPD:ReInit ()
LibPDBinding.LibPD..cctor () (at Assets/Magicolo/AudioTools/PureData/LibPD/LibPDNativeMethods.cs:65)
Rethrow as TypeInitializationException: An exception was thrown by the type initializer for LibPDBinding.LibPD
Magicolo.AudioTools.PureDataBridge.SetAudioSettings () (at Assets/Magicolo/AudioTools/PureData/PureDataBridge.cs:46)
Magicolo.AudioTools.PureDataBridge.StartLibPD () (at Assets/Magicolo/AudioTools/PureData/PureDataBridge.cs:25)
Magicolo.AudioTools.PureDataBridge.Start () (at Assets/Magicolo/AudioTools/PureData/PureDataBridge.cs:50)
PureData.StartAll () (at Assets/Magicolo/AudioTools/PureData/PureData.cs:141)
PureData.Initialize () (at Assets/Magicolo/AudioTools/PureData/PureData.cs:58)
PureData.Awake () (at Assets/Magicolo/AudioTools/PureData/PureData.cs:173)
Visual studio warnings x 50 or so:
Warning CS0436 The type 'LibPD' in 'D:\unity projects\libpdinunity\Assets\Magicolo\AudioTools\PureData\LibPD\LibPdNativeUnity.cs' conflicts with the imported type 'LibPD' in 'LibPDBinding, Version=0.3.0.177, Culture=neutral, PublicKeyToken=null'. Using the type defined in 'D:\unity projects\libpdinunity\Assets\Magicolo\AudioTools\PureData\LibPD\LibPdNativeUnity.cs'. libpdinunity.CSharp D:\unity projects\libpdinunity\Assets\Magicolo\AudioTools\PureData\PureDataCommunicator.cs 87 Active
Thanks again for any help. Much appreciated.
Looking at your error, libpdcsharp is currently placed at <ProjectDirectory>/Assets/Magicolo/AudioTools/PureData/Plugins/
This DLL file should be placed at <ProjectDirectory>/Asset.
Also, make sure that libpdcsharp is compiled with .NET 2.0 or 3.5. This won't work if you compile it with .NET 4.0 and above.
I am the maintainer of C# binding for LibPD.
You also need to change the link to the correct libwinpthread-1.dll version (64 bit instead of 32). In your final project you need all 3 dlls:
LibPDBinding.dll (the managed wrapper)
libpdcsharp.dll (the native dll)
libwinpthread-1.dll (for enabling POSIX threads on Windows)
Also: Have you tried the version from NuGet? No need to compile your own version of the binding.
EDIT: Your warning are from a different source: You have two classes named LibPD, and the C# compiler does not know, which one to use.
I am not sure, if these types are in the same namespace.
If they are in the same namespace, then change your namespace.
If they are in different namespaces, add a line using LibPd = LibPDBinding.LibPD; if you want to use the version from NuGet, or using LibPd = <yournamespace>.LibPD; if you want to use your version.
It's working. The uPD github code has an error as far as I can tell. So hopefully it gets fixed. In the meantime I just started the process from scratch and this is what I did to get the uPD test scene working in unity 5:
Get uPD from github. Theres a unity package in there
Get libPD from github.
Follow the instructions on the libPD github page to compile a x64
vresion of libpdcsharp.dll
replace the existing libpdcsharp.dll inside your unity project with
the x64 version you compiled
locate libpd\libs\mingw64\libwinthread-1.dll in the libpd files and
place this in the your unity project - the same place as the other dll is fine. I
don't think it matters where you put the Dll's (at least it made no
difference in my tests)
Run the project and notice you get an error in unity EntryPointNotFoundException: libpd_safe_init
double click the error to open up the problem script.
Find the following line of code: [DllImport("libpdcsharp", EntryPoint="libpd_safe_init")]
Change it to [DllImport("libpdcsharp", EntryPoint="libpd_init")] That's the real entry point for the init function in libpdcsharp. I don't know what libpd_safe_init is.
Thanks to the contributors who helped me to troubleshoot and find the issue!

Using .NET dependencies with Iron.io IronWorker

I'm brand new to Iron.io's IronWorker, and I'm attempting to interface with it via C#.NET. I have managed to get the sample on the site to work, which utilizes the System.Web.Script.Serialization library. However, I want to use Newtonsoft.Json library for serialization/deserialization. My .worker file (located in the \bin directory of a Visual Studio project) looks like this:
runtime 'mono'
exec 'Debug\hello.exe'
file 'Debug\Newtonsoft.Json.dll'
I'm fairly certain that Newtonsoft.Json is being loaded, because adding the 3rd line of file: 'Debug\Netwonsoft.Json.dll' fixed the error that said that Newtonsoft could not be found.
The error I'm receiving when attempting to run a task is the following (I removed any seemingly sensitive information):
{"code_id":"[CODE_ID]","code_name":"hello","created_at":"2014-11-
16T13:48:01Z","duration":2452,"end_time":"2014-11-16T13:48:06Z","id":"[ID]","msg":"Unhandled
Exception: System.TypeLoadException: Could not load type
'System.Runtime.CompilerServices.ExtensionAttribute' from assembly
'Newtonsoft.Json'.\n at HelloWorld.Main (System.String[] args) [0x00000] in <filename
unknown>:0 \n[ERROR] FATAL UNHANDLED EXCEPTION: System.TypeLoadException: Could
not load type 'System.Runtime.CompilerServices.ExtensionAttribute' from assembly
'Newtonsoft.Json'.\n","project_id":"[PROJECTID]","start_time":
"2014-11-16T13:48:04Z","timeout":3600,"updated_at":"2014-11-16T13:48:07Z","status"
:"error"}
In the course of tracking down the error: Could not load type 'System.Runtime.CompilerServices.ExtensionAttribute' from assembly 'Newtonsoft.Json', I ran across a SO thread that suggested there might be a .NET framework mismatch between v4.0 and v4.5. I'm using v4.5, and I tried changing it to v4.0 with no luck (so this may not be the issue).
Can someone please point me in the right direction as to how to properly utilize a dependency, such as Newtonsoft.Json (so at least the sample on Iron.io will work)?
P.S. I've been toying with IronSharp as an API to assist with interfacing with Iron.io products (once I get past this dependency issue). But, I'm open to suggestions if there's anything better I should be checking out.
SOLUTION
#Featalion's answer below really helped, becuase he pointed me to the stack command, which I placed in like so:
runtime 'mono'
stack 'mono-3.6'
exec 'Debug\hello.exe'
file 'Debug\Netwonsoft.Json.dll'
I believe it resolved the error at hand, because the higher mono framework seems to handle the latest version of Newtwonsoft.Json.dll. I'll post here if I find otherwise.
Make note that as of 11/17/2014, it seems the highest stack version supported by IronWorker is 3.6 (even though 3.10 is available, details found here). Check the highest stable release from Iron.io at this link.
This is, possibly, related to mono version on the IronWorker service. I found related question on StackOverflow. Try to change the stack in your .worker file to mono-3.6 or mono-3.0. See all possible runtime environments on Iron.io's dev site. mono runtime is default, and it is alias for mono-2.10.

C#, Mono 2.6.7 - SIGSEGV when hovering over TreeView

Environment:
Mono 2.6.7
CentOS 5.4, 32-bit
I have a Gtk Window with various docking panels (MonoDevelop.Components.Docking). In a tabbed group of panels, one panel crashes the application with a SIGSEGV in a very specific situation:
click on another tab to view a panel other than this one in the tabbed group
click back to the problem panel
move mouse around over the first row of the TreeView displayed in the panel
this only occurs if there is at least one row with data, and only when moving the mouse around over the first row of data
Here is the stack trace when the app crashes:
Stacktrace:
at (wrapper managed-to-native) Gtk.Application.gtk_main () <0x00004>
at (wrapper managed-to-native) Gtk.Application.gtk_main () <0x00004>
at Gtk.Application.Run () <0x0000a>
at MyApp.MainClass.Main (string[]) <0x002ad>
at (wrapper runtime-invoke) <Module>.runtime_invoke_void_object (object,intptr,intptr,intptr) <0x00043>
Native stacktrace:
mono [0x80d93bb]
mono [0x8114180]
[0xdb6440]
/mydir/deps/lib/libcairo.so.2 [0x1f47178]
/mydir/deps/lib/libcairo.so.2 [0x1f432e0]
/mydir/deps/lib/libcairo.so.2 [0x1f4354d]
/mydir/deps/lib/libcairo.so.2 [0x1f17396]
/mydir/deps/lib/libcairo.so.2 [0x1f1989d]
/mydir/deps/lib/libcairo.so.2 [0x1f19cc1]
/mydir/deps/lib/libcairo.so.2 [0x1f164e6]
/mydir/deps/lib/libcairo.so.2 [0x1f06cb3]
/mydir/deps/lib/libcairo.so.2(cairo_fill_preserve+0x2c) [0x1eff00c]
/mydir/deps/lib/libcairo.so.2(cairo_fill+0x12) [0x1eff032]
/mydir/deps/lib/libgtk-x11-2.0.so.0 [0x345cc16]
/mydir/deps/lib/libgtk-x11-2.0.so.0(gtk_cell_renderer_render+0x92) [0x34596b2]
/mydir/deps/lib/libgtk-x11-2.0.so.0 [0x363594d]
/mydir/deps/lib/libgtk-x11-2.0.so.0 [0x3636689]
/mydir/deps/lib/libgtk-x11-2.0.so.0 [0x3632e88]
/mydir/deps/lib/libgtk-x11-2.0.so.0 [0x352804e]
/mydir/deps/lib/libgobject-2.0.so.0 [0x1ec5729]
/mydir/deps/lib/libgobject-2.0.so.0(g_closure_invoke+0x123) [0x1ec6f33]
/mydir/deps/lib/libgobject-2.0.so.0 [0x1ed80c1]
/mydir/deps/lib/libgobject-2.0.so.0(g_signal_emit_valist+0x68f) [0x1ed8d7f]
/mydir/deps/lib/libgobject-2.0.so.0(g_signal_emit+0x29) [0x1ed9179]
/mydir/deps/lib/libgtk-x11-2.0.so.0 [0x3645f78]
/mydir/deps/lib/libgtk-x11-2.0.so.0(gtk_main_do_event+0x507) [0x35225e7]
/mydir/deps/lib/libgdk-x11-2.0.so.0 [0xcfb38b]
/mydir/deps/lib/libgdk-x11-2.0.so.0(gdk_window_process_all_updates+0xf8) [0xcfba38]
/mydir/deps/lib/libgdk-x11-2.0.so.0 [0xcfba5b]
/mydir/deps/lib/libgdk-x11-2.0.so.0 [0xce1a18]
/mydir/deps/lib/libglib-2.0.so.0 [0x26ba01]
/mydir/deps/lib/libglib-2.0.so.0(g_main_context_dispatch+0x17c) [0x26d5bc]
/mydir/deps/lib/libglib-2.0.so.0 [0x270a26]
/mydir/deps/lib/libglib-2.0.so.0(g_main_loop_run+0x1a9) [0x270dd9]
/mydir/deps/lib/libgtk-x11-2.0.so.0(gtk_main+0xb4) [0x3522844]
[0x609524e]
[0x6095213]
[0xcc0766]
[0xd5e88c]
mono [0x8062076]
mono(mono_runtime_invoke+0x40) [0x8158b20]
mono(mono_runtime_exec_main+0xea) [0x815ed0a]
mono(mono_runtime_run_main+0x1ba) [0x81602fa]
mono(mono_main+0x1670) [0x80b5f80]
mono [0x80594a1]
/lib/libc.so.6(__libc_start_main+0xdc) [0x49ee9c]
mono(g_ascii_strcasecmp+0x95) [0x80593b1]
Debug info from gdb:
=================================================================
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.
=================================================================
It's difficult to give any code since it's proprietary to my employer, and it's pretty messy code. I was just wondering if anyone had some tips on some general cases that might cause this that I could look for.
Also, please do not just reply with "use a later version of Mono", etc. This is the environment I'm given to work with. If there is some Mono bug related to this though, that would be helpful info.
Note that I did find a question here: MonoTouch NullReference for every event/Action (SIGSEGV),
that mentions local UI components with attached events getting disposed. I did a search in my code and the only local UI components with events are 2 popup menu items. However, the code is not being hit in this situation because I'm not triggering the menu (right-click).
Thank you.
Mono 2.6.7 is kind of old, can you test with stable Mono 2.10.6?
You said "please do not just reply with "use a later version of Mono" but I just don't see how you don't go down that route. Perhaps the crashes are coming from GtkSharp being out of date and buggy, perhaps it's Mono. We just don't know what is causing it given the error posted.
With that said, I'd write up a test case with the UI on a single window and then try to isolate the problem outside of the proprietary app. This way if the problem is still occurring you can post code here that will be easier for people to pinpoint a problem or even assist in bug filing against Mono or GtkSharp, etc..
The problem happened to be a bug in the cairo package we were using. We upgraded to a newer version - problem solved. The bug had to do with rendering pixbufs in the cells.
To the other 2 posts, yes using a newer version of Mono would be great. However, we are working under time/money constraints while supporting legacy projects. Hopefully we'll have an effort to upgrade Mono and test everything next year.

MonoTouch 4.0 throwing ReflectionTypeLoadException using the simulator

Since updating a working MonoTouch 3.x app to MonoTouch 4.0, I have been getting the following error when attempting to start a Debug or Release build on the Simulator:
Unhandled Exception: System.Reflection.ReflectionTypeLoadException: The classes in the module cannot be loaded.
at (wrapper managed-to-native) System.Reflection.Assembly:GetTypes (System.Reflection.Assembly,bool)
at System.Reflection.Assembly.GetTypes () [0x00000] in <filename unknown>:0
at MonoTouch.ObjCRuntime.Runtime.RegisterAssembly (System.Reflection.Assembly a) [0x00000] in <filename unknown>:0
The problem appears to be with System.Xml.Linq, however I have checked all projects and references, and this doesn't even appear to be a transitive reference from what I can tell. I just don't appear to be using this anywhere.
Exception Message is always "Could not load System.Xml.Linq.XNodeNavigator from System.Xml.Linq".
Anyone have any ideas?
The root cause is that the iOS simulator only update files, it never deletes them (even if you delete your app from the simulator).
This cause problems if you have some files (e.g. System.Xml.Linq) at one point and remove them later (directly or use the linker which can remove it). From there you'll have some old, stale files in your simulator app directory.
The next issue is that MonoTouch register all assemblies present in the directory. This means the stale one are registered too in the simulator. At this stage this can work (and you won't see an error) or this could fail (with the error you have above) because the stale assembly does not have everything it needs to be loaded properly.
The easiest workaround is to reset your iOS simulator, that will remove everything (all apps and all files).

Categories