2011-05-26 47 views
9

我一直在尝试在StrictMode中运行我的应用程序,以检查是否有可能潜行的隐藏问题。我遇到的一个问题是,在使用ContentResolver时,似乎是泄漏的DatabaseConections的误报。Android StrictMode报告误报

一些实验后得到简化为以下两行代码的问题:上述

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游标)。

有没有人有任何洞察,如果这确实是一个误报或真的有漏光标。

+2

我看到类似的使用与Honeycomb一起发布的CursorLoader API。我有一个CursorLoader(它在后台线程中执行查询)和一个SimpleCursorAdapter。原则上,当我从CursorLoader中交换新的游标时,SimpleCursorAdapter应该关闭任何现有的游标。但是,我一直看到这些游标终结器StrictMode违规,所以这有点令人困惑。 – tomtheguvnor 2011-10-06 16:26:36

回答

1

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