Brian
Brian

Reputation: 609

Database LEAK, when taking a photo. SQLiteDatabase created and never closed. Android, Java, SQLite

Greetings yet again. I have an minor issue when taking a photo. I have a button that invokes the camera, successfully takes a photo, and returns to my entry form. My only problem is the database leak that occurs when pressing the button to call the camera. My code looks a little something like this.

public void takephoto(){

        Intent in = new
        Intent(this, takephoto.class);
        startActivityForResult(in,1);
    }

Here's a snippet of how the database is opened:

 public journalDbAdapter open() throws SQLException {
        mDbHelper = new DatabaseHelper(mCtx);
        mDb = mDbHelper.getWritableDatabase();
        return this;
    }

    public void close() {
        mDbHelper.close();
    }

I run :

mDbHelper = new journalDbAdapter(this);
        mDbHelper.open();

In the onCreate of the Activity or Class that I am calling the camera in. I have not coded in mDbHelper.close(); anywhere not sure if i should when or where. I think i would rather just leave it open while capturing one image.

Logcat:

08-17 21:33:37.582: ERROR/Database(18297): java.lang.IllegalStateException: /data/data/com.growjournal.beta/databases/grower SQLiteDatabase created and never closed
08-17 21:33:37.582: ERROR/Database(18297):     at android.database.sqlite.SQLiteDatabase.(SQLiteDatabase.java:1785)

What possible problems could I face if this was never fixed??

Everything seems to be working fine, but i would defiantly like to avoid any issues amongst the many android devices out there.

Upvotes: 0

Views: 929

Answers (2)

aschmack
aschmack

Reputation: 66

You're getting this error because the camera app is a huge memory hog and is most likely causing your app to close instead of just pausing. You should save your records and close the database in onPause and reopen it in onResume and/or onCreate.

Upvotes: 1

DeRagan
DeRagan

Reputation: 22930

I think you are missing this line

mDb.close();

Upvotes: 0

Related Questions