Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A SQLiteConnection object for database was leaked! Please fix your application

My application give me this warning

A SQLiteConnection object for database '+data+data+com_example_test+database' was leaked! Please fix your application to end transactions in progress properly and to close the database when it is no longer needed.

But I close the db object and the cursor after every use.

        try {
            while (cursor.moveToNext()) {
              ...
            }
        } finally {
            if (cursor != null && !cursor.isClosed())
                cursor.close();
        }

...
    db.close();

Can you help me for understand what is the problem? thanks!!!

UPDATE! I try this solution from this post SQLite Connection leaked although everything closed

and I don't have memory leak anymore, is it a good solution?

like image 925
user3906040 Avatar asked Aug 14 '14 12:08

user3906040


4 Answers

Possible Solutions:

  • You have not committed the transactions you have started (You should always close the transaction once you started)
  • Check whether you have closed the cursors you have opened if you are using Sqlite (Looks like you have done this step from the code you posted)
  • Also move the db.close to finally block
  • You have not called db.close on a database before deleting it with context.deleteDatabase(...) and then recreating it with dbHelper.getWritableDatabase()
like image 152
Devrath Avatar answered Nov 06 '22 20:11

Devrath


Just drag that db.close up into the finally block.

like image 27
G. Blake Meike Avatar answered Nov 06 '22 20:11

G. Blake Meike


//Inside your SQLite helper class
@Override
public synchronized void close () {
    if (db != null) {
        db.close();
        super.close();
    }
}

//Inside the activity that makes a connection to the helper class
@Override
protected void onDestroy () {
    super.onDestroy();
    //call close() of the helper class
    dbHelper.close();
}
like image 7
Ave Maria Avatar answered Nov 06 '22 20:11

Ave Maria


this code stops the leak and fixes cursor problems.

 public class DatabaseHelper extends SQLiteOpenHelper { 
    
      private static DatabaseHelper sInstance;
    
      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 context) {
    
        // Use the application context, which will ensure that you 
        // don't accidentally leak an Activity's context.
        if (sInstance == null) {
          sInstance = new DatabaseHelper(context.getApplicationContext());
        }
        return sInstance;
      }
    
      /**
       * Constructor should be private to prevent direct instantiation.
       * make call to static factory method "getInstance()" instead.
       */
      private DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
      }
    }
like image 1
Basheer Adel Avatar answered Nov 06 '22 21:11

Basheer Adel