title: Crazy SQLite and cursor leak on AutoComplete
body: <p>In my activity, I have an AutoCompleteTextView that gets its contents from my custom adapter.  I created my adapter by following <a href="http://thinkandroid.wordpress.com/2010/02/08/writing-your-own-autocompletetextview/" rel="nofollow">this example</a>.</p>
  
  <p>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?</p>
  
  <p>Here is my method:</p>
  
  <pre><code>@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);
  }
  </code></pre>
  
  <p>I tried modifying it to this:</p>
  
  <pre><code>final DbUtilities favsDB = new DbUtilities(context);
  Cursor c = favsDB.getAllRecents(buffer == null ? null : buffer.toString(), args);
  favsDB.close();
  return c;
  </code></pre>
  
  <p>But I am getting the <code>Invalid statement in fillWindow()</code> error and the AutoCompleteTextView does not display the dropdown.</p>
  
  <p>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):</p>
  
  <pre><code>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);
  </code></pre>
  
  <p>I cannot use <code>startManagingCursor()</code>, so I close the cursor manually.  But I still get the Cursor not finalized exception:</p>
  
  <pre><code>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 android.database.sqlite.SQLiteCursor@43d63338 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)
  </code></pre>
  
  <p>Any ideas on how I can resolve these errors?  Thanks!</p>