Search code examples
androidsqlitememory-leakssqliteopenhelper

Sqlite memory leak. Bad tutorial or it's usage?


I am trying to build my first app which, but struggling with sqlite.

I get this

W/SQLiteConnectionPool﹕ A SQLiteConnection object for database '/data/data/com.example.user.myApp/databases/testJavaNorm.sqlite' was leaked!  Please fix your application to end transactions in progress properly and to close the database when it is no longer needed.
W/SQLiteConnectionPool﹕ A SQLiteConnection object for database '/data/data/com.example.user.myApp/databases/testJavaNorm.sqlite' was leaked!  Please fix your application to end transactions in progress properly and to close the database when it is no longer needed.
W/SQLiteConnectionPool﹕ A SQLiteConnection object for database '/data/data/com.example.user.myApp/databases/testJavaNorm.sqlite' was leaked!  Please fix your application to end transactions in progress properly and to close the database when it is no longer needed.

As a beginner I used Sqlite tutorial. As I understand I have to write public methods in the end of DB class and call it like was shown in 3.

But Android studio sometimes show a memory leak.

I noticed that there isn't any db.close anywhere. So I added it in call methods, later public methods. Still have leaks. Now I have read that cursor also have to be closed.

Am I using this tutorial wrong? Or it's a bad tutorial?


Solution

  • Am I using this tutorial wrong? Or it's a bad tutorial?

    It is a very bad tutorial. In addition to the bugs that you pointed out, it hard-codes filesystem paths. NEVER HARD-CODE FILESYSTEM PATHS. It also completely breaks the SQLiteOpenHelper model.

    For the specific case of wanting to package a database with your app, please use SQLiteAssetHelper.