I have been trying to run my application in StrictMode to check for any hidden problem that may have sneaked. One issue I ran across is what seems to be a false positive of Leaked DatabaseConections when using ContentResolver.
After some experiments got the issue simplified to the following 2 lines of code:
Cursor c = context.getContentResolver().query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, cols, null, null, MediaStore.Video.Media.DEFAULT_SORT_ORDER);
c.close()
The 2 lines above generate the following StrictMode violation:
ERROR/StrictMode(26219): Releasing cursor in a finalizer. Please ensure that you explicitly call close() on your cursor:
ERROR/StrictMode(26219): android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here
ERROR/StrictMode(26219):
at android.database.CursorWindow.<init>(CursorWindow.java:62)
at android.content.ContentProviderProxy.query(ContentProviderNative.java:403)
at android.content.ContentResolver.query(ContentResolver.java:302)
I am assuming that this is something specific to the fact that the Cursor was returned by a contentProvider (so it is not a direct SQLite cursor).
Does anyone have any insight if this is indeed a false positive or there is really a leaky cursor.
I think I can explain where the problem is. When you query database you can receive an exception. Thus, a Cursor will be created c.close() will not be called because there is an exception. Thus, you have to put the creation of the Cursor in try catch block and close the curson in the finally block.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With