Android SQLite leak problems

Hey, I've got my database connection setup in an Android Application but LogCat keep's telling me about a SQLite leak.

12-03 15:07:23.169: ERROR/Database(2509): Leak found
12-03 15:07:23.169: ERROR/Database(2509): java.lang.IllegalStateException: /data/data/com.domain/databases/db.db SQLiteDatabase created and never closed
12-03 15:07:23.169: ERROR/Database(2509):     at android.database.sqlite.SQLiteDatabase.<init>(SQLiteDatabase.java:1784)
12-03 15:07:23.169: ERROR/Database(2509):     at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:804)
12-03 15:07:23.169: ERROR/Database(2509):     at com.domain.DatabaseConnection.openDataBase(DatabaseConnection.java:118)
12-03 15:07:23.169: ERROR/Database(2509):     at com.domain.global.ZonesLoaded.InitZones(ZonesLoaded.java:32)
12-03 15:07:23.169: ERROR/Database(2509):     at com.domain.ZoneActivity.onCreate(ZoneActivity.java:34)
12-03 15:07:23.169: ERROR/Database(2509):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
12-03 15:07:23.169: ERROR/Database(2509):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2544)
12-03 15:07:23.169: ERROR/Database(2509):     at android.app.ActivityThread.startActivityNow(ActivityThread.java:2411)
12-03 15:07:23.169: ERROR/Database(2509):     at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:127)
12-03 15:07:23.169: ERROR/Database(2509):     at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:339)
12-03 15:07:23.169: ERROR/Database(2509):     at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:648)
12-03 15:07:23.169: ERROR/Database(2509):     at android.widget.TabHost.setCurrentTab(TabHost.java:320)
etc...

After reading other posts like here or here, it would suggest that I wasn't implementing the close method in my DBAdapter class. However I did create this:

public synchronized void close()
{
    if(myDatabase != null)
        myDatabase.close();

    super.close();
}

I'm starting to wonder if it is down to how I'm using the database adapter, although I just tested my app on a much faster device (Samsung Galaxy S) and this worked with no problems whatsoever! Does anyone have any tips here?

Thanks

Answers


I had this problem too until I made sure to call close explicitly in my activity's onPause method. I tend to follow the pattern of opening the DB on resume/create and close it before a pause (as with all other resources that hold state).

To be very clear, I do not recommend opening/closing a DB on-demand or on an ad-hoc basis. It is almost always best to open the DB up early and keep it open for the duration of your activity and close it when the activity gets suspended.


Need Your Help

What is the meaning of the format control specifier %016I64X in sprintf_s

string-formatting printf format-specifiers

What is the meaning of the format control specifier "%S\%016I64X%S" in this sprintf_s command ?

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.