davidmartos96 / sqflite_sqlcipher

SQLite flutter plugin
BSD 2-Clause "Simplified" License
99 stars 44 forks source link

EXC_BAD_ACCESS on iOS #19

Closed davidmartos96 closed 3 years ago

davidmartos96 commented 4 years ago

Crash on iOS with the latest sqflite changes, when opening a non sqlcipher database.

With the latest sqflite version, 1.3.0, on iOS it can throw a native error EXC_BAD_ACCESS which crashes the app. It occurs when running the following new added code:

image

This error only happens when opening a normal sqflite after having opened an sqflite_sqlcipher database. Read below for the setup necessary for the crash to happen.

Some background: When you are using both sqflite_sqlcipher and sqflite in the same project (probably due to other dependencies like cache_network_image) the FMDB pod can't be installed with both the normal Sqlite and SqlCipher variants. To fix that, this repo has the branch fmdb_override which uses the SqlCipher pod in the orginal sqflite , as the pod shares the same API.

I have created a test sample in the SqlCipher group of tests in the example project from this repo which produces the crash. It's in the branch ios_test_crash. From what I've researched it looks like it has to do with thread synchronization and the FMDatabaseQueue but I'm not sure how can it be solved. In theory, the queue in the ObjC code on both sqflite and sqflite_sqlcipher are different.

cc @alextekartik I was hoping if you would know what could be the reason of the crash or if you know how could it be handled, while keeping the native implementations in the plugins as independent as possible.

Temporary solution

If you are getting this crash my current solution is to point to this specific commit when overriding sqflite

dependency_overrides:
  sqflite:
    git:
      url: https://www.github.com/davidmartos96/sqflite_sqlcipher.git
      path: sqflite
      ref: 237dbf50572a283b377ba6c690c55d8b00117a10
alextekartik commented 4 years ago

I'm not fully aware of namespaces in iOS but I think since you redefined the same class with the same name, link should even fail.... You can try to rename all classes and constants (except the static ones) and add a SqlCipher middle prefix. On sqflite I share the code source between macos and ios (they are always identical). It's likely the issue also exists on MacOS too.

davidmartos96 commented 4 years ago

Thanks @alextekartik . Well, the project links and builds just fine when using both sqflite and sqflite_sqlicipher with the pod override. It also runs great with versions of sqflite lower than 1.3.0. It's just with the latest changes that it throws this error at runtime. I will try to rename the classes as you suggest anyway to see if it helps.

gilescm commented 4 years ago

Hi, has there been any progress on this? I'm currently using the work around suggested, but am interested in knowing if the class rename helped.

davidmartos96 commented 4 years ago

@gilescm Unfortunately I wasn't able to get it working with that suggestion. I don't think is much problematic to lock the version of the sqflite package to version 1.3.0 though. It's been working rock solid for a long time. I'm also curious on why is not working, but it may simply have to do with the internal workings of the FMDatabase class which comes in the pod, so not an easy fix I'm afraid.

daohoangson commented 3 years ago

In my testing, the example app uses sqlite3 built by SQLCipher in _sqflitesqlcipher.openDatabase BUT it uses /usr/lib/libsqlite3.dylib in sqflite.openDatabase.

I have been able to make it work with 2 sets of changes:

Check screenshot:

davidmartos96 commented 3 years ago

This issue has been resolved in the latest update thanks to @daohoangson There is no longer need to change the ref in the dependency override. Using fmdb_override as ref is still needed though.