static void release()
{
if(m_pMemoryPools)
{
m_pMemoryPools->~CMemoryPools();
free(m_pMemoryPools);
m_pMemoryPools = NULL;
}
}
还有内存有泄漏,对Close()修改了一下
void CMemoryPools::Close()
{
/** @brief 添加线程安全 */
CAutoLock autolock(&m_ThreadLock);
/** @brief 删除所有已经分配的文件块 */
_MemoryList* pCurrMemoryList = m_pMemoryList;
while(NULL != pCurrMemoryList)
{
_MemoryBlock* pMemoryUsed = pCurrMemoryList->m_pMemoryUsed;
while(NULL != pMemoryUsed)
{
if(NULL != pMemoryUsed->m_pBrick)
{
free(pMemoryUsed->m_pBrick);
pMemoryUsed->m_pBrick = NULL;
}
_MemoryBlock* toDel = pMemoryUsed;
pMemoryUsed = pMemoryUsed->m_pNext;
free(toDel);
}
_MemoryBlock* pMemoryFree = pCurrMemoryList->m_pMemoryFree;
while(NULL != pMemoryFree)
{
if(NULL != pMemoryFree->m_pBrick)
{
free(pMemoryFree->m_pBrick);
pMemoryFree->m_pBrick = NULL;
}
_MemoryBlock* toDel = pMemoryFree;
pMemoryFree = pMemoryFree->m_pNext;
free(toDel);
}
m_pMemoryList = pCurrMemoryList;
pCurrMemoryList = pCurrMemoryList->m_pMemLNext;
free(m_pMemoryList);
}
} 新手不熟定制new、delete的规则
请问楼主,为何重载了operator new, operator delete, operator delete[]
却没有重载operator new[]? 楼主 我爱死你了,我上次看见一个qq群来着,又给忘了,求加入啊 "其实内存池的作用大家也知道,一般是解决大量的new和delete频繁操作引起的内存碎片,效率是一方面,另外长时间后的安全性也是一个问题。"
现在的操作系统,内存碎片都会自己清理,重点已经不是碎片 而是性能
new很耗性能 delete更好性能 从前视列表中拆大内存为小内存 耗性能 回收时查找相邻空闲内存并且合并更耗内存 因为这个需要安全操作类似加锁
还有就是现在很多杀毒软件会钩new这个函数使得new性能下降非常多
内存池的性能提升是非常可观的
页:
1
[2]