Fix memory leaks in libsurfaceflinger_unittest
Every class that inherits from RefBase must be held by an sp<>.
Creating a RefBAse object without an sp<> causes a memory leak.
Test: adb shell /data/nativetest64/libsurfaceflinger_unittest/libsurfaceflinger_unittest
Bug: 181807839
Change-Id: I37ec274b01c5212a5a3cecc2e1eb5b4c9d6122f4
diff --git a/services/surfaceflinger/tests/unittests/CachingTest.cpp b/services/surfaceflinger/tests/unittests/CachingTest.cpp
index 6bc2318..6a7ec9b 100644
--- a/services/surfaceflinger/tests/unittests/CachingTest.cpp
+++ b/services/surfaceflinger/tests/unittests/CachingTest.cpp
@@ -31,7 +31,8 @@
class SlotGenerationTest : public testing::Test {
protected:
- BufferStateLayer::HwcSlotGenerator mHwcSlotGenerator;
+ sp<BufferStateLayer::HwcSlotGenerator> mHwcSlotGenerator =
+ sp<BufferStateLayer::HwcSlotGenerator>::make();
sp<GraphicBuffer> mBuffer1{new GraphicBuffer(1, 1, HAL_PIXEL_FORMAT_RGBA_8888, 1, 0)};
sp<GraphicBuffer> mBuffer2{new GraphicBuffer(1, 1, HAL_PIXEL_FORMAT_RGBA_8888, 1, 0)};
sp<GraphicBuffer> mBuffer3{new GraphicBuffer(10, 10, HAL_PIXEL_FORMAT_RGBA_8888, 1, 0)};
@@ -41,7 +42,7 @@
sp<IBinder> binder = new BBinder();
// test getting invalid client_cache_id
client_cache_t id;
- uint32_t slot = mHwcSlotGenerator.getHwcCacheSlot(id);
+ uint32_t slot = mHwcSlotGenerator->getHwcCacheSlot(id);
EXPECT_EQ(BufferQueue::INVALID_BUFFER_SLOT, slot);
}
@@ -50,19 +51,19 @@
client_cache_t id;
id.token = binder;
id.id = 0;
- uint32_t slot = mHwcSlotGenerator.getHwcCacheSlot(id);
+ uint32_t slot = mHwcSlotGenerator->getHwcCacheSlot(id);
EXPECT_EQ(BufferQueue::NUM_BUFFER_SLOTS - 1, slot);
client_cache_t idB;
idB.token = binder;
idB.id = 1;
- slot = mHwcSlotGenerator.getHwcCacheSlot(idB);
+ slot = mHwcSlotGenerator->getHwcCacheSlot(idB);
EXPECT_EQ(BufferQueue::NUM_BUFFER_SLOTS - 2, slot);
- slot = mHwcSlotGenerator.getHwcCacheSlot(idB);
+ slot = mHwcSlotGenerator->getHwcCacheSlot(idB);
EXPECT_EQ(BufferQueue::NUM_BUFFER_SLOTS - 2, slot);
- slot = mHwcSlotGenerator.getHwcCacheSlot(id);
+ slot = mHwcSlotGenerator->getHwcCacheSlot(id);
EXPECT_EQ(BufferQueue::NUM_BUFFER_SLOTS - 1, slot);
}
@@ -77,12 +78,12 @@
id.id = cacheId;
ids.push_back(id);
- uint32_t slot = mHwcSlotGenerator.getHwcCacheSlot(id);
+ uint32_t slot = mHwcSlotGenerator->getHwcCacheSlot(id);
EXPECT_EQ(BufferQueue::NUM_BUFFER_SLOTS - (i + 1), slot);
cacheId++;
}
for (uint32_t i = 0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) {
- uint32_t slot = mHwcSlotGenerator.getHwcCacheSlot(ids[i]);
+ uint32_t slot = mHwcSlotGenerator->getHwcCacheSlot(ids[i]);
EXPECT_EQ(BufferQueue::NUM_BUFFER_SLOTS - (i + 1), slot);
}
@@ -90,7 +91,7 @@
client_cache_t id;
id.token = binder;
id.id = cacheId;
- uint32_t slot = mHwcSlotGenerator.getHwcCacheSlot(id);
+ uint32_t slot = mHwcSlotGenerator->getHwcCacheSlot(id);
EXPECT_EQ(BufferQueue::NUM_BUFFER_SLOTS - (i + 1), slot);
cacheId++;
}