Skip to content

Commit bbd0731

Browse files
authored
删除含有bug隐患的createObject<T1>接口
该接口存在隐含的bug,相关分析写在个人博客中,如果有误,欢迎指正 https://blog.csdn.net/it_wjw/article/details/104032466
1 parent 8e95d7d commit bbd0731

File tree

1 file changed

+27
-59
lines changed

1 file changed

+27
-59
lines changed

kbe/src/lib/common/objectpool.h

Lines changed: 27 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ namespace KBEngine{
2020
#define OBJECT_POOL_INIT_SIZE 16
2121
#define OBJECT_POOL_INIT_MAX_SIZE OBJECT_POOL_INIT_SIZE * 1024
2222

23-
// 每5分钟检查一次瘦身
23+
// 每5分钟检查一次瘦身
2424
#define OBJECT_POOL_REDUCING_TIME_OUT 300 * stampsPerSecondD()
2525

26-
// 追踪对象分配处
26+
// 追踪对象分配处
2727
#define OBJECTPOOL_POINT fmt::format("{}#{}", __FUNCTION__, __LINE__).c_str()
2828

2929
template< typename T >
@@ -42,9 +42,9 @@ class ObjectPoolLogPoint
4242
};
4343

4444
/*
45-
一些对象会非常频繁的被创建, 例如:MemoryStream, Bundle, TCPPacket等等
46-
这个对象池对通过服务端峰值有效的预估提前创建出一些对象缓存起来,在用到的时候直接从对象池中
47-
获取一个未被使用的对象即可。
45+
一些对象会非常频繁的被创建, 例如:MemoryStream, Bundle, TCPPacket等等
46+
这个对象池对通过服务端峰值有效的预估提前创建出一些对象缓存起来,在用到的时候直接从对象池中
47+
获取一个未被使用的对象即可。
4848
*/
4949
template< typename T, typename THREADMUTEX = KBEngine::thread::ThreadMutexNull >
5050
class ObjectPool
@@ -134,40 +134,8 @@ class ObjectPool
134134
}
135135

136136
/**
137-
强制创建一个指定类型的对象。 如果缓冲里已经创建则返回现有的,否则
138-
创建一个新的, 这个对象必须是继承自T的。
139-
*/
140-
template<typename T1>
141-
T* createObject(const std::string& logPoint)
142-
{
143-
pMutex_->lockMutex();
144-
145-
while(true)
146-
{
147-
if(obj_count_ > 0)
148-
{
149-
T* t = static_cast<T1*>(*objects_.begin());
150-
objects_.pop_front();
151-
--obj_count_;
152-
incLogPoint(logPoint);
153-
t->poolObjectCreatePoint(logPoint);
154-
t->onEabledPoolObject();
155-
t->isEnabledPoolObject(true);
156-
pMutex_->unlockMutex();
157-
return t;
158-
}
159-
160-
assignObjs();
161-
}
162-
163-
pMutex_->unlockMutex();
164-
165-
return NULL;
166-
}
167-
168-
/**
169-
创建一个对象。 如果缓冲里已经创建则返回现有的,否则
170-
创建一个新的。
137+
创建一个对象。 如果缓冲里已经创建则返回现有的,否则
138+
创建一个新的。
171139
*/
172140
T* createObject(const std::string& logPoint)
173141
{
@@ -197,7 +165,7 @@ class ObjectPool
197165
}
198166

199167
/**
200-
回收一个对象
168+
回收一个对象
201169
*/
202170
void reclaimObject(T* obj)
203171
{
@@ -207,7 +175,7 @@ class ObjectPool
207175
}
208176

209177
/**
210-
回收一个对象容器
178+
回收一个对象容器
211179
*/
212180
void reclaimObject(std::list<T*>& objs)
213181
{
@@ -225,7 +193,7 @@ class ObjectPool
225193
}
226194

227195
/**
228-
回收一个对象容器
196+
回收一个对象容器
229197
*/
230198
void reclaimObject(std::vector< T* >& objs)
231199
{
@@ -243,7 +211,7 @@ class ObjectPool
243211
}
244212

245213
/**
246-
回收一个对象容器
214+
回收一个对象容器
247215
*/
248216
void reclaimObject(std::queue<T*>& objs)
249217
{
@@ -296,15 +264,15 @@ class ObjectPool
296264

297265
protected:
298266
/**
299-
回收一个对象
267+
回收一个对象
300268
*/
301269
void reclaimObject_(T* obj)
302270
{
303271
if(obj != NULL)
304272
{
305273
decLogPoint(obj->poolObjectCreatePoint());
306274

307-
// 先重置状态
275+
// 先重置状态
308276
obj->onReclaimObject();
309277
obj->isEnabledPoolObject(false);
310278
obj->poolObjectCreatePoint("");
@@ -325,12 +293,12 @@ class ObjectPool
325293

326294
if (obj_count_ <= OBJECT_POOL_INIT_SIZE)
327295
{
328-
// 小于等于则刷新检查时间
296+
// 小于等于则刷新检查时间
329297
lastReducingCheckTime_ = now_timestamp;
330298
}
331299
else if (now_timestamp - lastReducingCheckTime_ > OBJECT_POOL_REDUCING_TIME_OUT)
332300
{
333-
// 长时间大于OBJECT_POOL_INIT_SIZE未使用的对象则开始做清理工作
301+
// 长时间大于OBJECT_POOL_INIT_SIZE未使用的对象则开始做清理工作
334302
size_t reducing = std::min(objects_.size(), std::min((size_t)OBJECT_POOL_INIT_SIZE, (size_t)(obj_count_ - OBJECT_POOL_INIT_SIZE)));
335303

336304
//printf("ObjectPool::reclaimObject_(): start reducing..., name=%s, currsize=%d, OBJECT_POOL_INIT_SIZE=%d\n",
@@ -359,28 +327,28 @@ class ObjectPool
359327

360328
bool isDestroyed_;
361329

362-
// 一些原因导致锁还是有必要的
363-
// 例如:dbmgr任务线程中输出log,cellapp中加载navmesh后的线程回调导致的log输出
330+
// 一些原因导致锁还是有必要的
331+
// 例如:dbmgr任务线程中输出log,cellapp中加载navmesh后的线程回调导致的log输出
364332
THREADMUTEX* pMutex_;
365333

366334
std::string name_;
367335

368336
size_t total_allocs_;
369337

370-
// Linux环境中,list.size()使用的是std::distance(begin(), end())方式来获得
371-
// 会对性能有影响,这里我们自己对size做一个记录
338+
// Linux环境中,list.size()使用的是std::distance(begin(), end())方式来获得
339+
// 会对性能有影响,这里我们自己对size做一个记录
372340
size_t obj_count_;
373341

374-
// 最后一次瘦身检查时间
375-
// 如果长达OBJECT_POOL_REDUCING_TIME_OUT大于OBJECT_POOL_INIT_SIZE,则最多瘦身OBJECT_POOL_INIT_SIZE个
342+
// 最后一次瘦身检查时间
343+
// 如果长达OBJECT_POOL_REDUCING_TIME_OUT大于OBJECT_POOL_INIT_SIZE,则最多瘦身OBJECT_POOL_INIT_SIZE个
376344
uint64 lastReducingCheckTime_;
377345

378-
// 记录的创建位置信息,用于追踪泄露点
346+
// 记录的创建位置信息,用于追踪泄露点
379347
std::map<std::string, ObjectPoolLogPoint> logPoints_;
380348
};
381349

382350
/*
383-
池对象, 所有使用池的对象必须实现回收功能。
351+
池对象, 所有使用池的对象必须实现回收功能。
384352
*/
385353
class PoolObject
386354
{
@@ -402,8 +370,8 @@ class PoolObject
402370
}
403371

404372
/**
405-
池对象被析构前的通知
406-
某些对象可以在此做一些工作
373+
池对象被析构前的通知
374+
某些对象可以在此做一些工作
407375
*/
408376
virtual bool destructorPoolObject()
409377
{
@@ -432,10 +400,10 @@ class PoolObject
432400

433401
protected:
434402

435-
// 池对象是否处于激活(从池中已经取出)状态
403+
// 池对象是否处于激活(从池中已经取出)状态
436404
bool isEnabledPoolObject_;
437405

438-
// 记录对象创建的位置
406+
// 记录对象创建的位置
439407
std::string poolObjectCreatePoint_;
440408
};
441409

0 commit comments

Comments
 (0)