Skip to content

Commit cb84275

Browse files
bradfitzAndroid Code Review
authored andcommitted
Merge "Closing cursor in finalizer to avoid GREF and fd leak in acore"
2 parents fcf3d12 + 872a52c commit cb84275

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)