开发者

Using the same database in successive activities in Android without memory leak

开发者 https://www.devze.com 2023-03-09 06:14 出处:网络
I\'ll preface this question with the note that I have looked at this similar question, but I\'m still encountering issues. Basically, I want to access the same database in two activities in my Android

I'll preface this question with the note that I have looked at this similar question, but I'm still encountering issues. Basically, I want to access the same database in two activities in my Android application. However, when I open it in the second activity, I'm getting two series of messages in my LogCat:

First:

"Uncaught exception thrown by finalizer (will be discarded):

Ljava/lang/IllegalStateException;: Finalizing cursor android.database.sqlite.SQLiteCursor@436053b8 on dogs that has not been deactivated or closed

at android.database.sqlite.SQLiteCursor.finalize(SQLiteCursor.java:596)"

(dogs is the name of a table in my database, dog_data)

Second:

"ERROR/Database(1316): Leak found

ERROR/Database(1316): java.lang.IllegalStateException: /data/data/com..../databases/dog_data SQLiteDatabase created and never closed"

As far as I can tell, I am closing my database upon exiting the first activity. Following the style of the notepad tutorial, I have a wrapper class "DbAdapter" around my SQLiteDatabase, and in the onPause() method of the first activity, I call the close method on that Adapter (which calls the close methods on my SQLiteDatabase and my SQLiteOpenHelper).

I think the issue is how I am 开发者_运维百科trying to reopen the database in my second activity:

SQLiteDatabase db = openOrCreateDatabase("dog_data", 
    SQLiteDatabase.CREATE_IF_NECESSARY, null);

(I choose not to use a wrapper because I only needed to run one query on the database, perhaps this is an issue).

Can someone advise as to where my issue might be? I'll admit (as may be clear from my question) that I don't fully understand the implications of "closing" a database (the documentation for SQLiteDatabase.close() is not particularly specific), which is probably the main reason for my problem.

Thanks.


Just in case someone happens to encounter a similar issue (seems possible but probably unlikely), I recently stumbled onto the solution. In the insert method of my "DbAdapter", I was (stupidly) checking uniqueness via a query for a row with a given value for one of the fields, and seeing whether that query returned any rows. This was creating a cursor that I wasn't closing, which resulted in the "Finalizing cursor" error noted above.


I've received that error before and had to use cursor.close() to correct the issue. I'm not exactly sure why because there are times when I didn't use close() and received no error. Maybe it's a warning that only gets noticed when it is sitting next to a show stopping error?

I will say the proper procedure is open database connection -> create cursor by running db method -> iterate through cursor -> close cursor -> close database connection.

0

精彩评论

暂无评论...
验证码 换一张
取 消