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

7

55

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?

Snub answered 14/8, 2014 at 12:16 Comment(3)
There is some place where this does not happen.Taryn
it seems problem is in writing to DB, do you have uncommitted transactions?Aleece
Possible duplicate of SQLite Connection leaked although everything closedFlexed
B
32

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()
Bort answered 14/8, 2014 at 13:13 Comment(0)
L
9

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

Lavonlavona answered 14/8, 2014 at 13:18 Comment(0)
S
7
//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();
}
Sandstone answered 18/7, 2019 at 14:17 Comment(1)
Thank you for taking the time to leave an answer. Unfortunately, it is missing a description and explanation of what you have provided. Please update your question to include thisAntidepressant
T
1

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);
      }
    }
Thaw answered 1/9, 2021 at 22:37 Comment(0)
P
0

In my case the error was caused when y try to download new data and database should be updated.

I solved it instantiating the database by calling a SELECT 0. That cause database to be updated, so after that I try to download the new data. And worked fine.

Pinfish answered 18/10, 2017 at 4:6 Comment(0)
B
0

Probably you forgot to remove the break point of debugging sample:

Sample Screenshot

Bakemeier answered 19/3, 2018 at 5:38 Comment(0)
L
0

In my case, I was calling to getWritableDatabase or getReadableDatabase and not use it at all. for example if you use it with "execSQL" execSQL will call "releaseReference" "Releases a reference to the object, closing the object if the last reference was released."

Learned answered 21/9, 2022 at 7:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.