SQLite连接对象泄露 - Android

use*_*083 10 sqlite android 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.
Run Code Online (Sandbox Code Playgroud)

我的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;
    }
Run Code Online (Sandbox Code Playgroud)

我找到了这个我明天会尝试的.已经很晚了.

谢谢.

use*_*083 18

我所做的就是对类似的问题实现这个答案,现在它没有显示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);
  }
}
Run Code Online (Sandbox Code Playgroud)


小智 11

我通过添加修复此问题

@Override
protected void finalize() throws Throwable {
    this.close();
    super.finalize();
}
Run Code Online (Sandbox Code Playgroud)

到我的SQLiteOpenHelper扩展类

  • 这会通过隐藏泄漏来消除错误.这不是一个很好的解决方案,因为数据库连接只会在垃圾收集后关闭 - 可能要晚于你应该关闭它. (4认同)

laa*_*lto 5

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

例如,您getAllClothingItemsByGenderAndCategory()打来的电话getReadableDatabase()却没有close()db.close()在之后添加c.close()

closeDB()是没有意义的,因为它得到一个新的参考数据库getReadableDatabase(),并关闭只是。它不会关闭任何现有的数据库连接。