praeclarum / sqlite-net

Simple, powerful, cross-platform SQLite client and ORM for .NET
MIT License
4.08k stars 1.42k forks source link

iOS: API call with invalid database connection pointer #850

Open b12kab opened 5 years ago

b12kab commented 5 years ago

On iOS I'm getting this: 2019-07-05 13:12:24.365627-0400 TwoTypeExample.iOS[2189:144181] [logging] API call with invalid database connection pointer 2019-07-05 13:12:24.365901-0400 TwoTypeExample.iOS[2189:144181] [logging] misuse at line 154344 of [95fbac39ba]

I've tried narrowing this down and it seems to be coming from the Finalize of the SQLiteConnection. I'm not getting any errors on iOS other than that error message, the data has not been corrupted or missing. I've not seen anything like this on Android.

I tried opening the connection with and without the SQLiteOpenFlags.SharedCache flag, but the error message occurred either way. This is the same in both Android and iOS:

SQLiteConnection conn = new SQLiteConnection(_filespec, SQLiteOpenFlags.SharedCache | SQLiteOpenFlags.ReadWrite | SQLiteOpenFlags.Create | SQLiteOpenFlags.FullMutex, true, null);

There's been one XF forum entry on a similar issue, but it didn't help me determine the root cause.

I do have a sample project here which also contains a video as well as a log file.

Any thoughts would be appreciated.

zizusoft commented 5 years ago

Hi - I am getting exactly the same error on iOS. Any ideas?

Thanks

b12kab commented 5 years ago

@zizusoft After giving a full example of the problem with an example, no one but you has actually responded to this issue.

I've thought about opening a Stack Overflow question about it - given the lack of comment here. Perhaps you might want to go over to SO and post the link in here :)

I'd love to get the answer, so I could submit my to the app store.

StepanMynarik commented 5 years ago

Same here on v1.6.292, Xcode 11.0, iOS 13

Except the line number in my case is different, it reads: "misuse at line 162552"

b12kab commented 5 years ago

@StevenKek I believe the line number is dependent on the version of the embedded SQLite that's installed on the iOS device (or emulator).

As I showed in my example, this seems to be appearing from a garbage collection problem with the recycling forcing closed all open connections with the database, not just the current object being GC'd.

If you have time, you might post about this on SO, as I've not yet had time to create a question about it there.

StepanMynarik commented 5 years ago

@b12kab I'm in a similar situation here. Let's hope @praeclarum will come to the rescue soon :D

b12kab commented 5 years ago

@StevenKek Given that I opened up this bug 3 months ago with a full solution showing how to recreate the error, "soon" becomes relative :)

StepanMynarik commented 5 years ago

I'm in no particular rush since the error seems to be more of a warning than anything else because the SQLite queries work. But yeah, I'm not happy about it.

DM1145 commented 5 years ago

Getting identical error as @StevenKek

b12kab commented 4 years ago

@DM1145 Perhaps you'd like to create a SO question pointing to this issue? Unfortunately, my free time to do that hasn't appeared.

@StevenKek Have you submitted this to Apple with this error occurring? I've not bothered, figuring they'd reject it off hand.

RuddyOne commented 4 years ago

Similar issue here. I cant even track it down, seems random.

b12kab commented 4 years ago

@Ruddy2007 In the sample project I've linked on this issue (along with the log and video), it shows that there is a problem with the garbage collection of sqlite instance on iOS. From my investigation of the log, it appears that the garbage collection is ending all instances that are running agains sqlite instead of just the specific instance that is being garbage collected.

jemunk commented 4 years ago

Same here on v1.6.292, Xcode 11.3, iOS 13

JensTKrueger commented 4 years ago

I had the same error because I forgot to call Close() on my SQLiteConnection object. Fixing this made the error disappear.

StepanMynarik commented 4 years ago

Well... I'm reusing a single connection for the whole lifetime of the application. For performance reasons. So I don't want to close it. And I keep getting this error/warning on a regular basis. The app works as expected though.

JensTKrueger commented 4 years ago

That's weird. I only got it after starting a second connection without closing the first one. But I agree on the part, that the app still works as expected.

NSExceptional commented 4 years ago

I'm also having this issue and I'm pulling my hair out. I'm not using this library though, I'm using the raw SQLite3 C API

vniehues commented 4 years ago

I have the same issue. Did someone find a fix yet?

vniehues commented 4 years ago

Just found something! I was lazy-initializing my database at the first access. I changed that to a classic static init and now i dont get that error anymore.

static readonly Lazy<SQLiteAsyncConnection> _databaseConnectionHolder = new Lazy<SQLiteAsyncConnection>(() => new SQLiteAsyncConnection(_databasePath, SQLiteOpenFlags.ReadWrite | SQLiteOpenFlags.Create | SQLiteOpenFlags.SharedCache));
       static SQLiteAsyncConnection DatabaseConnection => _databaseConnectionHolder.Value;

changed to:

static SQLiteAsyncConnection DatabaseConnection = new SQLiteAsyncConnection(_databasePath, SQLiteOpenFlags.ReadWrite | SQLiteOpenFlags.Create | SQLiteOpenFlags.SharedCache);
b12kab commented 4 years ago

@vniehues I've not yet seen a response to this issue. If you can use a static connection - that seems to have worked - if they can do that. Unfortunately, I and others can't :(

b12kab commented 3 years ago

I've looked at this again with the current version, 1.7.335, and it gives the same error message under Xamarin iOS as when I opened this solution:

2021-07-17 16:12:15.786594-0400 TwoTypeExample.iOS[3734:122935] [logging] API call with invalid database connection pointer 2021-07-17 16:12:15.786748-0400 TwoTypeExample.iOS[3734:122935] [logging] misuse at line 169014 of [02c344acea]

I've updated my example sample project and added the log file; to keep things the same I've only updated the sqlite-net-pcl nuget.

I've worked around this by using a singleton pattern for the connection. If a mutli-threaded solution is really needed, I'd suggest looking at another solution.

Good luck!