简体   繁体   中英

Why some users are getting FC at database.close?

I have the following code:

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

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

This close method is called at onDestroy of MainActivity:

@Override
protected void onDestroy() {
  super.onDestroy();

  mDbHelper.close();
}

I've got the following error log from my user:

device_model:bravo
build_version:1.6.17
condition:1
processName:spb.bridges
pid:23493
uid:10057
tag:null
shortMsg:android.database.sqlite.SQLiteException
longMsg:android.database.sqlite.SQLiteException: unable to close due to unfinalised statements
stackTrace:java.lang.RuntimeException: Unable to destroy activity {my.package/my.package.MainActivity}: android.database.sqlite.SQLiteException: unable to close due to unfinalised statements
at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:2680)
at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:2698)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:2814)
at android.app.ActivityThread.access$1600(ActivityThread.java:117)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3694)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.database.sqlite.SQLiteException: unable to close due to unfinalised statements
at android.database.sqlite.SQLiteDatabase.dbclose(Native Method)
at android.database.sqlite.SQLiteDatabase.onAllReferencesReleased(SQLiteDatabase.java:323)
at android.database.sqlite.SQLiteDatabase.close(SQLiteDatabase.java:884)
at android.database.sqlite.SQLiteOpenHelper.close(SQLiteOpenHelper.java:220)
at spb.bridges.DbAdapter.close(DbAdapter.java:177)
at spb.bridges.MainActivity.onDestroy(MainActivity.java:159)

What is wrong with my code?

It's possible that they try to close the app while the app is performing a database operation. The easiest thing to do would be to put a try/catch/log around that mDbHelper.close(); statement.

OnDestroy() is not guaranteed to run. If say a user is using a task killer, it may sudo kill -9 the thing, and not run. Other people on SO have also said there are situations where even Android won't call it. It's supposed to be called right before android kills it to free up memory.

It would probably be best if you put that logic to close in OnPause() and then again initialized in OnResume().

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM