Android database leak found
-
29-05-2021 - |
سؤال
When i enter another activity to insert new item into the database,return and then go in that activity it gives me an error:
04-03 18:53:26.914: E/Database(18134): Leak found
04-03 18:53:26.914: E/Database(18134): java.lang.IllegalStateException: /data/data/com.app.myapp/databases/BabyApp.db SQLiteDatabase created and never closed
04-03 18:53:26.914: E/Database(18134): at android.database.sqlite.SQLiteDatabase.<init>(SQLiteDatabase.java:1695)
04-03 18:53:26.914: E/Database(18134): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:739)
04-03 18:53:26.914: E/Database(18134): at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:761)
04-03 18:53:26.914: E/Database(18134): at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:754)
04-03 18:53:26.914: E/Database(18134): at android.app.ApplicationContext.openOrCreateDatabase(ApplicationContext.java:473)
04-03 18:53:26.914: E/Database(18134): at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:193)
04-03 18:53:26.914: E/Database(18134): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:98)
04-03 18:53:26.914: E/Database(18134): at com.app.myapp.DatabaseHelper.open(DatabaseHelper.java:121)
04-03 18:53:26.914: E/Database(18134): at com.app.myapp.SettingsActivity.init(SettingsActivity.java:80)
here is the DatabaseHelper.java:121
database = dbHelper.getWritableDatabase();
here is the SettingsActivity line 80 code:
if (!db.isOpen())
db.open();
المحلول
Make sure you do execute db.close();
in order to close your database before switching to another activity
@Override
protected void onStop() {
super.onStop();
//closing database connection
if(db != null)
db.close();
}
نصائح أخرى
You should close it too... Close it when an Exception occurs or in any case it should never be left open since its System resource
لا تنتمي إلى StackOverflow