summaryrefslogtreecommitdiff
path: root/core/java/android/database/sqlite
diff options
context:
space:
mode:
authorLi Li <dualli@google.com>2020-11-03 18:02:56 -0800
committerLi Li <dualli@google.com>2020-11-05 14:32:48 -0800
commitbe5bebafabd509df9a1abcbf4c54a60c24504c5c (patch)
treec06ebbffc98822e039344041a38ee476f4941d24 /core/java/android/database/sqlite
parent6cac0b0fb209cd2cc6894f7f816cb03a1bdf4172 (diff)
Use CloseGuard for cursor leak detection
Protect Cursors with CloseGuard and report implicit cleanup. By default, the leak detection code will print a one-line warning message about failing to call the cleanup function. To report more details like the original stacktrace and SQLite query statement, enable the correspoinding VmPolicy flags as the example below, please. /* * public void onCreate() { * if (DEVELOPER_MODE) { * StrictMode.setVmPolicy(new VmPolicy.Builder() * .detectLeakedSqlLiteObjects() // for SqlLiteCursor * .detectLeakedClosableObjects() // for any Cursor * .penaltyLog() * .build()); * } * super.onCreate(); * } */ By enabling detectLeakedSqlLiteObjecs, the original SQLiteCursor query statement is reported when close() or its equivalent cleanup function is not called before finalize(). By enabling detectLeakedClosableObjects, the new CloseGuard report will be provided, along with the original stack trace captured in open() or its equivalent. The former has better performance as it doesn't capture an original stack trace during open() stage. Only enable the latter if performance impact is not an issue. Both of them can be enabled at the same time. Bug: 168639120 Test: manually test with an example SQLite Android application, with and without calling cursor.close() after a db.rawQuery(). Change-Id: Ibe9fcdc8119c2e4651df1983e7ccd793f29e8e9d Signed-off-by: Li Li <dualli@google.com>
Diffstat (limited to 'core/java/android/database/sqlite')
-rw-r--r--core/java/android/database/sqlite/SQLiteCursor.java22
1 files changed, 7 insertions, 15 deletions
diff --git a/core/java/android/database/sqlite/SQLiteCursor.java b/core/java/android/database/sqlite/SQLiteCursor.java
index 3bfbe7e7b93b..7ba63e6462f1 100644
--- a/core/java/android/database/sqlite/SQLiteCursor.java
+++ b/core/java/android/database/sqlite/SQLiteCursor.java
@@ -62,9 +62,6 @@ public class SQLiteCursor extends AbstractWindowedCursor {
/** A mapping of column names to column indices, to speed up lookups */
private Map<String, Integer> mColumnNameMap;
- /** Used to find out where a cursor was allocated in case it never got released. */
- private final Throwable mStackTrace;
-
/** Controls fetching of rows relative to requested position **/
private boolean mFillWindowForwardOnly;
@@ -102,11 +99,6 @@ public class SQLiteCursor extends AbstractWindowedCursor {
if (query == null) {
throw new IllegalArgumentException("query object cannot be null");
}
- if (StrictMode.vmSqliteObjectLeaksEnabled()) {
- mStackTrace = new DatabaseObjectNotClosedException().fillInStackTrace();
- } else {
- mStackTrace = null;
- }
mDriver = driver;
mEditTable = editTable;
mColumnNameMap = null;
@@ -283,17 +275,17 @@ public class SQLiteCursor extends AbstractWindowedCursor {
try {
// if the cursor hasn't been closed yet, close it first
if (mWindow != null) {
- if (mStackTrace != null) {
+ // Report original sql statement
+ if (StrictMode.vmSqliteObjectLeaksEnabled()) {
String sql = mQuery.getSql();
int len = sql.length();
StrictMode.onSqliteObjectLeaked(
- "Finalizing a Cursor that has not been deactivated or closed. " +
- "database = " + mQuery.getDatabase().getLabel() +
- ", table = " + mEditTable +
- ", query = " + sql.substring(0, (len > 1000) ? 1000 : len),
- mStackTrace);
+ "Finalizing a Cursor that has not been deactivated or closed. "
+ + "database = " + mQuery.getDatabase().getLabel()
+ + ", table = " + mEditTable
+ + ", query = " + sql.substring(0, (len > 1000) ? 1000 : len),
+ null);
}
- close();
}
} finally {
super.finalize();