Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android StrictMode reporting false positives

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.

like image 746
StefanK Avatar asked May 26 '11 15:05

StefanK


1 Answers

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.

like image 131
Yury Avatar answered Sep 22 '22 11:09

Yury