Java 数据库的 SQLiteConnection 对象被泄露!请修复您的应用程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25307868/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 00:04:55  来源:igfitidea点击:

A SQLiteConnection object for database was leaked! Please fix your application

javaandroiddatabasesqlite

提问by user3906040

My application give me this warning

我的应用程序给了我这个警告

A SQLiteConnection object for database '+data+data+com_example_test+database' was leaked! Please fix your application to end transactions in progress properly and to close the database when it is no longer needed.

数据库 '+data+data+com_example_test+database' 的 SQLiteConnection 对象被泄露!请修复您的应用程序以正确结束正在进行的事务并在不再需要时关闭数据库。

But I close the db object and the cursor after every use.

但是每次使用后我都会关闭 db 对象和游标。

        try {
            while (cursor.moveToNext()) {
              ...
            }
        } finally {
            if (cursor != null && !cursor.isClosed())
                cursor.close();
        }

...
    db.close();

Can you help me for understand what is the problem? thanks!!!

你能帮我理解是什么问题吗?谢谢!!!

UPDATE!I try this solution from this post SQLite Connection leaked although everything closed

更新!我从这篇文章中尝试了这个解决方案 SQLite Connection 泄漏,尽管一切都关闭了

and I don't have memory leak anymore, is it a good solution?

我不再有内存泄漏,这是一个好的解决方案吗?

回答by Devrath

Possible Solutions:

可能的解决方案

  • You have not committed the transactionsyou have started (You should always close the transaction once you started)
  • Check whether you have closed the cursors you have opened if you are using Sqlite(Looks like you have done this step from the code you posted)
  • Also move the db.closeto finallyblock
  • You have not called db.closeon a database before deleting it with context.deleteDatabase(...)and then recreating it with dbHelper.getWritableDatabase()
  • 你有not committed the transactions你已经开始(你应该总是关闭交易,一旦你开始)
  • 如果您正在使用,请检查您是否关闭了您打开的游标Sqlite(看起来您已经从您发布的代码中完成了这一步)
  • 也移动db.closefinally
  • db.close在删除数据库之前没有调用数据库context.deleteDatabase(...),然后用dbHelper.getWritableDatabase()

回答by G. Blake Meike

Just drag that db.closeup into the finallyblock.

只是拖动db.close成的finally块。

回答by IgniteCoders

In my case the error was caused when y try to download new data and database should be updated.

在我的情况下,错误是在尝试下载新数据和数据库时引起的。

I solved it instantiating the database by calling a SELECT 0. That cause database to be updated, so after that I try to download the new data. And worked fine.

我通过调用一个SELECT 0. 这会导致数据库更新,因此之后我尝试下载新数据。并且工作得很好。

回答by steve Rogers

Probably you forgot to remove the break pointof debugging sample:

可能你忘了去掉调试示例的断点

Sample Screenshot

示例截图

回答by kaso

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity {

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    MenuInflater menuInflater = getMenuInflater();
    menuInflater.inflate(R.menu.main_menu, menu);

    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    super.onOptionsItemSelected(item);

    switch (item.getItemId()){
        case R.id.settings:
            Log.i("Menu item selected", "Settings");
            return true;
        case R.id.help:
            Log.i("Menu item selected", "Help");
            return  true;
        default:
            return false;
    }
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}
}

It is giving me database object leak error. Though the application runs fine and also logs in the Run tab but the android monitor is showing me the error and not logging the info.

它给了我数据库对象泄漏错误。尽管应用程序运行良好,并且还登录了“运行”选项卡,但 android 监视器向我显示了错误并且没有记录信息。

回答by Ave Maria

//Inside your SQLite helper class
@Override
public synchronized void close () {
    if (db != null) {
        db.close();
        super.close();
    }
}

//Inside the activity that makes a connection to the helper class
@Override
protected void onDestroy () {
    super.onDestroy();
    //call close() of the helper class
    dbHelper.close();
}