Files
ViewDesignEngine/tests/core/test_object_pool.cpp
T
hm 646b3eee8a fix(core,spatial): 修复 Octree 插入 SEGFAULT 与 ThreadSafeObjectPool 堆破坏
Octree(src/spatial/octree.cpp):
- 根因:subdivide() 在 nodes.reserve() 之前取 OctreeNode& 引用,扩容后
  node.children[i] 写入悬垂内存 → InsertMultiple SEGFAULT;subdivide_recursive /
  insert_recursive 同样持引用跨扩容。
- 修复:改为快照 + 按索引 append,不在 vector 扩容期间持有引用;子节点改默认
  构造后 children.fill(-1)(避免聚合初始化把 children 置 0 造成"每个子节点都像
  内部节点"的无限递归);remove_from_node 改为无引用实现;根包围盒扩张语义
  文档化并补回归(越界项保留在节点、仍可查询/删除)。
- 新增回归:多级细分查询、根扩张后插入可查询。

ThreadSafeObjectPool(include/vde/core/object_pool.h):
- 根因:static thread_local 池被所有实例共享且生命周期长于实例;release() 会把
  任意来源指针塞进当前线程池,实例销毁后仍留下悬垂指针 → 下次 acquire 返回已
  释放内存(0xc0000374 堆破坏)。
- 修复:改为每实例 mutex + 私有 ObjectPool + 原子计数(不再跨实例/跨线程混用
  指针),API 保持兼容;单线程 ObjectPool 语义不变。
- 新增回归:双实例隔离、同线程重复创建销毁 200 轮、跨线程 release。
2026-10-25 16:12:41 +08:00

193 lines
6.1 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#include <gtest/gtest.h>
#include "vde/core/object_pool.h"
#include <atomic>
#include <cstddef>
#include <set>
#include <thread>
#include <vector>
using namespace vde::core;
struct TestObj { int x = 0; double y = 0.0; };
TEST(ObjectPoolTest, AcquireRelease) {
ObjectPool<TestObj> pool;
auto* obj = pool.acquire();
ASSERT_NE(obj, nullptr);
obj->x = 42;
pool.release(obj);
auto* obj2 = pool.acquire(); // should reuse
EXPECT_EQ(obj2->x, 0); // reset to default
EXPECT_GT(pool.reuse_rate(), 0.0);
}
TEST(ObjectPoolTest, AcquireMany) {
ObjectPool<TestObj, 64> pool;
std::vector<TestObj*> objs;
for (int i = 0; i < 1000; ++i) objs.push_back(pool.acquire());
for (auto* o : objs) pool.release(o);
EXPECT_EQ(pool.total_allocated(), 1000u);
EXPECT_GT(pool.total_reused(), 0u);
}
TEST(ObjectPoolTest, ReleaseNullptr) {
ObjectPool<TestObj> pool;
pool.release(nullptr); // should not crash
EXPECT_EQ(pool.total_allocated(), 0u);
}
TEST(ObjectPoolTest, ExpandChunk) {
ObjectPool<TestObj, 10> pool;
// Acquire all 25 without releasing to force expansion (3 chunks of 10)
std::vector<TestObj*> objs;
for (int i = 0; i < 25; ++i) {
objs.push_back(pool.acquire());
}
EXPECT_GE(pool.pool_size(), 30u); // 3 chunks × 10 = 30
for (auto* o : objs) pool.release(o);
}
TEST(ThreadSafePoolTest, ConcurrentAcquire) {
ThreadSafeObjectPool<TestObj, 64> pool;
std::atomic<int> count{0};
auto worker = [&]() {
for (int i = 0; i < 100; ++i) {
auto* o = pool.acquire();
o->x = i;
pool.release(o);
count++;
}
};
std::thread t1(worker), t2(worker), t3(worker), t4(worker);
t1.join(); t2.join(); t3.join(); t4.join();
EXPECT_EQ(count, 400);
}
// Regression: two live ThreadSafeObjectPool instances must never hand out each
// other's memory. The previous implementation used one shared thread_local
// ObjectPool for all instances, so a release() could push an instance-local
// pointer into the shared per-thread free list and a later instance (or a
// destroyed instance's chunk) could be handed back out.
TEST(ThreadSafePoolTest, TwoInstancesAreIsolated) {
ThreadSafeObjectPool<TestObj, 8> pool_a;
ThreadSafeObjectPool<TestObj, 8> pool_b;
std::vector<TestObj*> a;
std::vector<TestObj*> b;
for (int i = 0; i < 8; ++i) a.push_back(pool_a.acquire());
for (int i = 0; i < 8; ++i) b.push_back(pool_b.acquire());
const std::set<const TestObj*> b_set(b.begin(), b.end());
const std::set<const TestObj*> a_set(a.begin(), a.end());
ASSERT_EQ(a_set.size(), 8u);
ASSERT_EQ(b_set.size(), 8u);
for (auto* p : a) EXPECT_EQ(b_set.count(p), 0u);
for (auto* p : b) EXPECT_EQ(a_set.count(p), 0u);
for (auto* p : a) pool_a.release(p);
for (auto* p : b) pool_b.release(p);
// Re-acquiring from A must reuse A's own released objects only.
for (int i = 0; i < 8; ++i) {
TestObj* p = pool_a.acquire();
ASSERT_NE(p, nullptr);
EXPECT_EQ(a_set.count(p), 1u);
EXPECT_EQ(b_set.count(p), 0u);
}
EXPECT_EQ(pool_a.total_allocated(), 16u);
EXPECT_EQ(pool_b.total_allocated(), 8u);
}
// Regression: repeatedly creating and destroying pools in the same thread must
// not leave dangling free-list entries behind (the old thread_local pool
// outlived each instance and kept pointers into freed chunks).
TEST(ThreadSafePoolTest, RepeatedCreateDestroySameThread) {
for (int round = 0; round < 200; ++round) {
ThreadSafeObjectPool<TestObj, 16> pool;
std::vector<TestObj*> objs;
for (int i = 0; i < 40; ++i) { // forces several chunks
auto* o = pool.acquire();
ASSERT_NE(o, nullptr);
o->x = i;
objs.push_back(o);
}
for (auto* o : objs) pool.release(o);
// All 40 must be reusable after release, with a clean default state.
for (int i = 0; i < 40; ++i) {
auto* o = pool.acquire();
ASSERT_NE(o, nullptr);
EXPECT_EQ(o->x, 0);
o->x = i;
pool.release(o);
}
}
SUCCEED();
}
// Pointers acquired by one thread may be released by another.
TEST(ThreadSafePoolTest, ReleaseFromOtherThread) {
ThreadSafeObjectPool<TestObj, 16> pool;
std::vector<TestObj*> objs;
for (int i = 0; i < 64; ++i) objs.push_back(pool.acquire());
constexpr int kThreads = 4;
std::vector<std::thread> threads;
for (int t = 0; t < kThreads; ++t) {
threads.emplace_back([&pool, &objs, t]() {
for (size_t i = static_cast<size_t>(t); i < objs.size(); i += kThreads)
pool.release(objs[i]);
});
}
for (auto& th : threads) th.join();
// Everything must be back in this pool and reusable.
std::vector<TestObj*> again;
for (int i = 0; i < 64; ++i) {
TestObj* p = pool.acquire();
ASSERT_NE(p, nullptr);
again.push_back(p);
}
EXPECT_EQ(std::set<TestObj*>(again.begin(), again.end()).size(), 64u);
for (auto* p : again) pool.release(p);
}
TEST(CowPtrTest, ReadOnly) {
CowPtr<std::vector<int>> cow(std::vector<int>{1, 2, 3});
EXPECT_EQ(cow.read().size(), 3u);
EXPECT_EQ(cow.read()[0], 1);
}
TEST(CowPtrTest, ShallowCopy) {
CowPtr<std::vector<int>> cow(std::vector<int>{1, 2, 3});
auto cow2 = cow.shallow_copy();
EXPECT_EQ(cow.use_count(), 2);
EXPECT_EQ(cow2.use_count(), 2);
}
TEST(CowPtrTest, WriteDetaches) {
CowPtr<std::vector<int>> cow(std::vector<int>{1, 2, 3});
auto cow2 = cow.shallow_copy();
EXPECT_EQ(cow.use_count(), 2);
cow.write().push_back(4);
EXPECT_EQ(cow.use_count(), 1); // detached
EXPECT_EQ(cow2.use_count(), 1);
EXPECT_EQ(cow2.read().size(), 3u); // cow2 unchanged
}
TEST(CowPtrTest, DeepCopy) {
CowPtr<std::vector<int>> cow(std::vector<int>{1, 2, 3});
auto cow2 = cow.deep_copy();
EXPECT_EQ(cow.use_count(), 1);
EXPECT_EQ(cow2.use_count(), 1);
}
TEST(CowPtrTest, UniqueCheck) {
CowPtr<int> cow(42);
EXPECT_TRUE(cow.unique());
auto cow2 = cow.shallow_copy();
EXPECT_FALSE(cow.unique());
}