Skip to content

Commit 872a52c

Browse files
Johannes CarlssonJohan Redestig
authored andcommitted
Closing cursor in finalizer to avoid GREF and fd leak in acore
The finalize() call did not clean up completely, this eventually caused the android.process.acore to crash since it ran out of fds and GREF to increased above 2000 if an application forgot to close its cursor objects. A warning was also added when this happens so that application developers can correct their mistake. The included test case tries to verify that the finalizer works as expected by creating a bunch of Cursor objects without closing them (without this fix the acore process crashes after about 400 iterations and the test case ends with "Process crashed"). Change-Id: I11e485cef1ac02e718b2742108aa88793666c31d
1 parent fcf3d12 commit 872a52c

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

core/java/android/content/ContentResolver.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1398,9 +1398,11 @@ public void close() {
13981398

13991399
@Override
14001400
protected void finalize() throws Throwable {
1401+
// TODO: integrate CloseGuard support.
14011402
try {
14021403
if(!mCloseFlag) {
1403-
ContentResolver.this.releaseProvider(mContentProvider);
1404+
Log.w(TAG, "Cursor finalized without prior close()");
1405+
close();
14041406
}
14051407
} finally {
14061408
super.finalize();
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright (C) 2010 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package android.content;
17+
18+
import android.content.ContentResolver;
19+
import android.provider.ContactsContract;
20+
import android.test.AndroidTestCase;
21+
import android.test.suitebuilder.annotation.LargeTest;
22+
23+
public class ContentResolverTest extends AndroidTestCase {
24+
private ContentResolver mContentResolver;
25+
26+
@Override
27+
protected void setUp() throws Exception {
28+
super.setUp();
29+
mContentResolver = mContext.getContentResolver();
30+
}
31+
32+
@LargeTest
33+
public void testCursorFinalizer() throws Exception {
34+
// TODO: Want a test case that more predictably reproduce this issue. Selected
35+
// 600 as this causes the problem 100% of the runs on current hw, it might not
36+
// do so on some other configuration though.
37+
for (int i = 0; i < 600; i++) {
38+
mContentResolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
39+
}
40+
}
41+
}

0 commit comments

Comments
 (0)