SQLite连接对象泄露 - Android

时间:2014-02-01 09:14:16

标签: android sqlite memory-leaks sqliteopenhelper

我正在制作我的第一个Android应用程序,我首先学习了一些sqlite教程,这教会我使用扩展SQLiteOpenHelper的databaseHelper。所以我的databaseHelper确实扩展了SQLiteOpenHelper。我在Logcat中收到了一个sqlite连接泄漏警告,所以想要了解如何解决这个问题。

我收到此错误:

02-01 21:39:50.740: W/SQLiteConnectionPool(32061): A SQLiteConnection object for database '/data/data/com.btf271.fashionassistant/databases/clothingManager' was leaked!  Please fix your application to end transactions in progress properly and to close the database when it is no longer needed.

在发生泄漏的地方调用我的databaseHelper函数:

 public List<Sticker> getObjectsByGenderAndCategory(String gender, String category) {
        List<Sticker> objects = new ArrayList<Object>();
        String selectQuery = String.format(
                "SELECT * FROM %s WHERE %s = \"%s\" AND %s = \"%s\"",
                TABLE_OBJECT, KEY_GENDER, gender, KEY_CATEGORY, category);

        Log.e(LOG, selectQuery);

        SQLiteDatabase db = this.getReadableDatabase();
        Cursor c = db.rawQuery(selectQuery, null);
     try{
        // looping through all rows and adding to list
        if (c.moveToFirst()) {
            do {
                Object o = createClothingItemJavaObject(c);

                // adding to object list
                objects.add(o);
            } while (c.moveToNext());
        }
     }finally {
           c.close();
db.close();
     }
        return objects;
    }

我找到了this which I will try tomorrow. It's late

感谢。

4 个答案:

答案 0 :(得分:17)

我所做的只是implement this answer to a similar question,现在它没有显示SQL连接对象泄漏错误。我不能推荐这个。实施和工作只需要几分钟。

以下是代码:

public class DatabaseHelper extends SQLiteOpenHelper { 

  private static DatabaseHelper mInstance = null;

  private static final String DATABASE_NAME = "database_name";
  private static final String DATABASE_TABLE = "table_name";
  private static final int DATABASE_VERSION = 1;

  public static DatabaseHelper getInstance(Context ctx) {

    // Use the application context, which will ensure that you 
    // don't accidentally leak an Activity's context.
    // See this article for more information: http://bit.ly/6LRzfx
    if (mInstance == null) {
      mInstance = new DatabaseHelper(ctx.getApplicationContext());
    }
    return mInstance;
  }

  /**
   * Constructor should be private to prevent direct instantiation.
   * make call to static factory method "getInstance()" instead.
   */
  private DatabaseHelper(Context ctx) {
    super(ctx, DATABASE_NAME, null, DATABASE_VERSION);
  }
}

答案 1 :(得分:11)

我通过添加

来解决这个问题
@Override
protected void finalize() throws Throwable {
    this.close();
    super.finalize();
}

到我的SQLiteOpenHelper扩展类

答案 2 :(得分:5)

将对getReadableDatabase()getWritableDatabase()的每次调用与同一数据库对象上的相应close()匹配。

例如,您的getAllClothingItemsByGenderAndCategory()来电getReadableDatabase()但不close()。在db.close()之后添加c.close()

您的closeDB()毫无意义,因为它使用getReadableDatabase()获取对数据库的新引用,并仅关闭 。它不会关闭任何现有的数据库连接。

答案 3 :(得分:-1)

每次打开数据库(可读或可写)时,必须使用&#34; .close();&#34;取消分配使用内存资源的游标。在每个数据库函数的使用结束之后..由your_database object.close();和cursor object.close();