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
A SQLiteConnection object for database was leaked! Please fix your application
提问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 transactions
you 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.close
tofinally
block - You have not called
db.close
on a database before deleting it withcontext.deleteDatabase(...)
and then recreating it withdbHelper.getWritableDatabase()
- 你有
not committed the transactions
你已经开始(你应该总是关闭交易,一旦你开始) - 如果您正在使用,请检查您是否关闭了您打开的游标
Sqlite
(看起来您已经从您发布的代码中完成了这一步) - 也移动
db.close
到finally
块 - 您
db.close
在删除数据库之前没有调用数据库context.deleteDatabase(...)
,然后用dbHelper.getWritableDatabase()
回答by G. Blake Meike
Just drag that db.close
up into the finally
block.
只是拖动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 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();
}