Type Initializer exception when opening database

I’m seeing this crash quite often in my app start up when opening the CouchbaseLite database. I verified that the data name and path is correct and valid. Is this a known bug or is there a possible workaround?

*** Terminating app due to uncaught exception ‘System.TypeInitializationException: An exception was thrown by the type initializer for Com.Data.CouchbaseDatabase’, reason: 'System.TypeInitializationException: An exception was thrown by the type initializer for Com.Data.CouchbaseDatabase —> Couchbase.Lite.CouchbaseLiteException: Unable to create a storage engine, fatal error
at Couchbase.Lite.Database.Open () [0x00000] in :0
at Couchbase.Lite.Manager.GetDatabase (System.String name) [0x00000] in :0

Is this all the stack trace information you get? It looks incomplete. However the usual reason for that exception is the inability to load the native sqlite DLL on Windows. If you are on Windows verify that

A) The sqlite3.dll file appears alongside your application in the subdirectory x86 or x64 (depending on your architecture) and is of the correct architecture type for your machine.

B) The Visual Studio 2012 C++ runtime is installed

The only thing missing from the stack trace is the initial call from the UI/App start up code that initializes the database, otherwise that’s all the information I get.

The app is built with Xamarin and deployed to both iOS and Android. I only see this on the iOS version of the app. I am using the native embedded sqlite.

I’m using the CouchbaseLite .NET library. This is occurring often (but not all the time or in some consistent pattern) with version and in some instances, the database will clear and wipe out any existing data. I’ve sinced upgraded to version With the latest version, I don’t see the exception as much, but it still occurs.

Hmmm well without some other information I can’t really say what is going on. It’s definitely not a known issue, I almost never see this exception outside of Windows. Are you on the latest iOS version? The latest Xamarin version? Can you find some log output from the device (logs are written to the device’s console, and to the Xamarin console when run with a debugger).

crash.txt.zip (15.9 KB)

Attached is the captured crash log. I couldn’t get anymore information out of it. My app analytics says that it happens mostly on iOS 9.2.

Your link is not working for me, it’s coming back with an empty response.

Looks like the attachment function doesn’t seem to work.

Here’s a link via dropbox to the log file: https://www.dropbox.com/s/izhqlf78k9ee8lr/crash.txt?dl=0

Just a meta-note, the attachment function works but only after you get to the right level. This is built in for security. That said, your approach is probably better. :slightly_smiling:

I’ll keep an eye open for this message but this crash log doesn’t give me much information to work with. All I can say is that the native SQLite library is failing to load the database file for some reason.

@couchdb_user One of my colleagues found the source of the problem. It is a Xamarin linking bug that you can solve by setting Linker Behavior to “Don’t Link” in the iOS Build section of the Project Options. Unfortunately this is not something we can solve at the library level, but I’ve capture the exception and thrown a new one with this solution as part of the message.

@borrrden I am familiar with the Xamarin Linker bug. With the latest Xamarin release, if I set the linking is turned on, Couchbase Lite will crash consistently everytime at start up with a TypeInitializerException at System.Security.Permissions.SecurityPermission. Changing the linker settings to either “Don’t Link” or using the monotouch build arguments “-nolinkaway” will resolve this issue.

The crash I am seeing currently though still occurs when using the “nolinkaway” build argument. And it only happens about 10% of the time at app startup. I don’t think it is a Xamarin linker issue since the app works fine the other 80%. If it was a linker issue, then it would be a consistent crash every time.

Did you add a reference to System.Data and Mono.Data.SQLite ?