dan*_*rah 14 memory sqlite android memory-leaks cursor
有关CursorWindowAllocatoinException的SO有很多问题:
所有这些都表明光标必须在使用后关闭.但这并没有解决我的问题.这是我的代码:
String query = "select serial from tbl1 union select serial from tbl2 union select serial from tbl3";
SQLiteDatabase db = null;
Cursor cur = null;
try {
SettingsDatabaseHelper dal = new SettingsDatabaseHelper(
c);
db = dal.getReadableDatabase();
cur = db.rawQuery(query, null);
int numRows = cur.getCount();
if (numRows > 0) {
cur.moveToFirst();
int serialIdx = cur.getColumnIndexOrThrow("serial");
for (boolean hasItem = cur.moveToFirst(); hasItem; hasItem = cur
.moveToNext()) {
String serial = cur.getString(serialIdx);
if (Validator.getInstance().isValidSerial(serial))
serials.add(serial);
}
}
} finally {
if (cur != null)
cur.close();
if (db != null)
db.close();
}
Run Code Online (Sandbox Code Playgroud)
我每隔几秒运行一次这个方法.半小时后,我得到CursorWindowAllocationException
的int numRows=cur.getCount();
,然后我的服务停止.
由于我关闭了光标,因此我的Helper类可能存在问题.这是代码:
public class SettingsDatabaseHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "mydb.db";
private static final int DATABASE_VERSION = 1;
private static final String TBL_CREATE="create table...";
public SettingsDatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase database) {
database.execSQL(TBL_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
onCreate(db);
}
}
Run Code Online (Sandbox Code Playgroud)
如何防止抛出此异常?
我正在从服务查询数据库。当我从活动中执行此操作时,问题再也没有出现。
除了查询之外,我还从同一服务中的串行端口读取数据。也许这导致了问题。但是,当我使用活动而不是服务时,问题就再也没有发生过。
归档时间: |
|
查看次数: |
7269 次 |
最近记录: |