Android StrictMode报告误报

时间:2011-05-26 15:34:39

标签: android android-strictmode

我一直在尝试在StrictMode中运行我的应用程序来检查可能已经潜入的任何隐藏问题。我遇到的一个问题是使用ContentResolver时Leaked DatabaseConections似乎是误报。

经过一些实验后,问题被简化为以下两行代码:

Cursor c = context.getContentResolver().query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, cols, null, null, MediaStore.Video.Media.DEFAULT_SORT_ORDER);

c.close()

上面的2行产生以下StrictMode违规:

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)

我假设这是特定于Cursor由contentProvider返回的事实(因此它不是直接的SQLite游标)。

如果这确实是误报或者确实存在漏光标记,是否有人有任何见解。

1 个答案:

答案 0 :(得分:1)

我想我可以解释问题所在。 查询数据库时,您可以收到异常。因此,将创建一个Cursor,因为存在异常,所以不会调用c.close()。因此,您必须在try catch块中创建Cursor并关闭finally块中的curson。