android – Crazy SQLite and cursor leak on AutoComplete – Education Career Blog

In my activity, I have an AutoCompleteTextView that gets its contents from my custom adapter. I created my adapter by following this example.

The adapter works so far, but I am getting so many errors on leaks and cursors that are not finalized. My question is: how do I close the db in runQueryOnBackgroundThread?

Here is my method:

@Override
public Cursor runQueryOnBackgroundThread(CharSequence constraint) {
    if (getFilterQueryProvider() != null) { 
        return getFilterQueryProvider().runQuery(constraint); 
    }

    StringBuilder buffer = null;
    String args = null;

    if (constraint != null) {
        buffer = new StringBuilder();
        buffer.append("UPPER (");
        buffer.append(DbUtilities.KEY_SEARCH_TERM);
        buffer.append(") GLOB ?");
        args = new String { "*" + constraint.toString().toUpperCase() + "*" };
    }

    final DbUtilities favsDB = new DbUtilities(context);
    return favsDB.getAllRecents(buffer == null ? null : buffer.toString(), args);
}

I tried modifying it to this:

final DbUtilities favsDB = new DbUtilities(context);
Cursor c = favsDB.getAllRecents(buffer == null ? null : buffer.toString(), args);
favsDB.close();
return c;

But I am getting the Invalid statement in fillWindow() error and the AutoCompleteTextView does not display the dropdown.

Here’s how I set my adapter in my class, which by the way does not extend Activity but instead extends RelativeView (because I am using this to set the content of my tabs):

AutoCompleteTextView searchTerm = (AutoCompleteTextView) findViewById(R.id.autocomp_search_what);

DbUtilities db = new DbUtilities(mActivity.getBaseContext());
Cursor c = db.getAllSearchTerms();
AutoCompleteCursorAdapter adapter = new AutoCompleteCursorAdapter(mActivity.getApplicationContext(), 
    R.layout.list_item_autocomplete_terms, c);
c.close();
searchTerm.setAdapter(adapter);

I cannot use startManagingCursor(), so I close the cursor manually. But I still get the Cursor not finalized exception:

10-20 16:08:09.964: INFO/dalvikvm(23513): Uncaught exception thrown by finalizer (will be discarded):
10-20 16:08:09.974: INFO/dalvikvm(23513): Ljava/lang/IllegalStateException;: Finalizing cursor [email protected] on search_terms that has not been deactivated or closed
10-20 16:08:09.974: INFO/dalvikvm(23513):     at android.database.sqlite.SQLiteCursor.finalize(SQLiteCursor.java:596)
10-20 16:08:09.974: INFO/dalvikvm(23513):     at dalvik.system.NativeStart.run(Native Method)

Any ideas on how I can resolve these errors? Thanks!

,

First, you are closing too early. You cannot close the Cursor while it is being used by AutoCompleteCursorAdapter. I am also unsure if you can safely close the database while you have an open Cursor on it.

Second, I do not know why you say you “cannot use startManagingCursor()“, because that would seem to be a fine answer in your case.

how do I close the db in runQueryOnBackgroundThread?

You could open the database in onCreate() and close it in onDestroy().

Leave a Comment