Closed zsoltk closed 9 years ago
Thanks for the test project! We'll take a look.
Ok, it looks to me like the issue here is that your database instance is not an app-wide singleton as recommended by this wiki page. A instance is getting created in the service that is never closed, and when it is collected by the garbage collector that warning is printed by the OS. I can actually make the warning print from your sample app on a KitKat emulator too by adding a System.gc() call after the call to fetch and clicking the button a couple times.
We mention in various places in the documentation that your database instances should be created as singletons, although perhaps we should be more explicit that we mean global singletons -- i.e., one instance per database for the lifetime of the application. You'll have to decide what kind of singleton management makes sense for your app -- we use a dependency injector to manage our singletons, but you could also maintain a static instance in an application subclass, or some other strategy.
My findings were basically the same as @sbosley's.
One more thing to note -- if you keep your database instance as a singleton, you actually shouldn't need to worry about closing it explicitly. See this stack overflow post.
Right! Thanks for the info, now it's all clear.
Hello,
I'm trying to investigate why I get SQLiteConnection leak warnings using SquiDB.
At its simplest, I get the warning in a following scenario:
I threw together an absolutely minimal test project here: https://github.com/zsoltk/squidb-sqliteconnection-leak
Steps to recreate:
Logcat lines should read:
The last line should be repeated exactly as many times as many times you pressed the button. Running the service four times creates four leaks.
Relevant lines from the test project code:
Activity:
Service:
I tested on a Lollipop physical device, Lollipop emulator, KitKat emulator, Jelly Bean emulator. The warning is there on both Lollipop instances, but does not appear on the others. So either the leak itself relates to Lollipop, or the warning message didn't exist in earlier platforms (lack of knowledge here).
I also don't know whether the issue has anything to do with services at its core, but at least there's no warning in a normal Activity context.
Any idea why this happens? Is there anything I'm doing wrong?