Unity ios build: work with debug mode but crash with release mode (archive ipa)

I’m trying to use sqlite with sqlcipher for unity ios game.
I get an sqlite unity example https://github.com/takezoux2/unity-orm
I added a method for unity to call function set key:
[DllImport("__Internal", EntryPoint = “sqlite3_key”)]
private static extern int sqlite3_key(IntPtr stmHandle, string key, int len);

I compiled sqlcipher to static library (libsqlcipher.a). Add it to Assets/Plugins/iOS with header sqlite3.h, sqlite3ext.h

In Unity editor, it works without encryption. It’s ok. I just want encryption work on ios devices.
I keep moving on export Xcode project, config OPENSSL_SRC in Source trees, add C flag -DSQLITE_HAS_CODEC, add search header “sqlcipher”, add Security framework then run on device.

It works! There is no issue. I get the database encrypted in Document folder (i tested it with hexdump -C, SQLiteManager)

The problem only occurred after I archive the app to ipa file (using enterprise provisioning profile). I Use iFunbox to install app to device. The app crash immediately on launch.

This is what i get from crash log:
Exception Type: EXC_BAD_ACCESS (SIGABRT)
Exception Subtype: KERN_INVALID_ADDRESS at 0x00000000

I try to replace sqlite3_key with PRAGMA key=‘123456’;
No more crash, everything works but the output database is not encrypted.

I don’t know what different between Archive and Build mode. My example works if i build directly to device (with both sqlite3_key and PRAGMA key=‘123456’;), but archive mode does not.

I also try to import sqlcipher xcode project to unity xcode project (remove libsqlcipher.a from Plugins/iOS), set all architectures to armv7 (also try with/without -mno-thumb), add C flag -DSQLITE_HAS_CODEC, add search header “sqlcipher”, add Security framework then run directly on device. It work!!!

Then i try to archive app, install app with iFunbox. Again, i get crash on launch.

I’m not sure if i’m setting something wrong with my project (https://github.com/tamhv/unity-orm-with-encrypt). Please someone take a look on this and give me advice. Thank you so much!

Are you sure there are no other differences in your project setup between Debug and Release configurations? Based on the behavior you’re describing it seems very possible that some compiler or linker settings are not correct for the Release build profile.

I tried to to set Release and Debug mode same configurations. There is no changes. Still get crash with Release mode.
But if I set Optimization Level Debug = Fastest, Smallest, i get crash in debug mode. It mean setting Optimization Level Release = None may help. But it’s still crashed!! :frowning:
Only Optimization Level give me a difference behavior between Debug and Release.

I guess the problem come from Unity3d. Because i can build a pure ios project( not Unity xcode project) in Release mode without issues.

This may not work:

[DllImport("__Internal", EntryPoint = "sqlite3_key")]
private static extern int sqlite3_key(IntPtr stmHandle, string key, int len); 

I did try to wrap all sqlite api in a plugin, then Unity will call to functions of the plugin, like this:

[DllImport("__Internal")]
private static extern int _sqlite3_key(IntPtr stmHandle, string key, int len);

And the plugin SQLitePlugin.mm:

#import <sqlite3.h>
extern "C" int _sqlite3_key(sqlite3 *db, const void *pKey, int nKey){
    return sqlite3_key(db, pKey, nKey);
}

...
//and all other sqlite api

Now sqlite work with encryption both Debug and Release mode. :smile: But i still don’t know why the code can work in Debug mode (actually i did try to set all schema Run,Build,… to Release and no crash!?, just crash after archiving) but can not work after Release.
(I’m using Unity 4.5.4f1, Xcode 6.1)