c++ - C++ 池分配器程序仅在控制台关闭时崩溃
问题描述
我正在查看这个池分配器实现。我实际上已经对其进行了一些修改,我的完整代码是:
template <class T, size_t T_per_page = 200>
class PoolAllocator
{
private:
const size_t pool_size = T_per_page * sizeof(T);
std::vector<T *> pools;
size_t count;
size_t next_pos;
void alloc_pool() {
next_pos = 0;
void *temp = operator new(pool_size);
pools.push_back(static_cast<T *>(temp));
}
public:
PoolAllocator() {
count = 0;
alloc_pool();
}
void* allocate() {
if (next_pos == T_per_page)
alloc_pool();
void* ret = pools.back() + next_pos;
++next_pos;
++count;
return ret;
}
size_t getSize() const
{
return T_per_page * (pools.size() - 1) + next_pos;
}
size_t getCount() const
{
return count;
}
size_t getCapacity() const
{
return T_per_page * pools.size();
}
T* get(size_t index) const
{
if (index >= getCount()) { return NULL; }
size_t poolIndex = index / T_per_page;
return pools[poolIndex] + (index % T_per_page);
}
~PoolAllocator() {
std::cout << "POOL ALLOCATOR DESTRUCTOR CALLED" << std::endl;
while (!pools.empty()) {
T *p = pools.back();
size_t start = T_per_page;
if (pools.size() == 1){
start = next_pos;
}
std::cout << "start: " << start << std::endl;
for (size_t pos = start; pos > 0; --pos)
{
std::cout << "pos: " << pos << std::endl;
p[pos - 1].~T();
}
operator delete(static_cast<void *>(p));
pools.pop_back();
}
}
};
template<class T>
PoolAllocator<T>& getAllocator()
{
static PoolAllocator<T> allocator;
return allocator;
}
class Node
{
private:
int id;
std::vector<float> vertices;
public:
Node() : id(42)
{
std::cout << "Node constructor called" << std::endl;
}
~Node(){ std::cout << "Node destructor called" << std::endl; }
void* operator new(size_t size)
{
std::cout << "Node operator new called" << std::endl;
return getAllocator<Node>().allocate();
}
void operator delete(void*)
{
std::cout << "Node operator delete called" << std::endl;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
Node* n1 = new Node();
Node* n2 = new Node();
Node* n3 = new Node();
Node* n4 = new Node();
std::cout << "Count: " << getAllocator<Node>().getCount() << " size: " << getAllocator<Node>().getSize() << " capacity: " << getAllocator<Node>().getCapacity() << std::endl;
while (true){}
return 0;
}
当我在 Visual Studio 中运行此代码时,它似乎可以正常工作,直到我关闭控制台时出现访问冲突错误。我尝试在分配器上手动调用析构函数,它似乎工作正常,但我一定在某处犯了错误。我得到的错误是:
谁能发现我在哪里犯错?
编辑1:
经过进一步调查,即使 main 中没有新的 Node 行,它仍然会崩溃。似乎与 getAllocator() 方法以及如何调用析构函数有关?或者分配器是静态的事实?
编辑2:
我实际上根本不认为它与我的分配器有任何关系!如果我尝试代码:
class Node2
{
private:
int x;
public:
Node2():x(42){std::cout << "Node2 constructor called" << std::endl;};
Node2(const Node2& other){ std::cout << "Node2 copy constructor called" << std::endl; };
~Node2(){ std::cout << "Node2 destructor called" << std::endl; };
};
Node2& Test(){
static Node2 myIndex;
return myIndex;
}
int _tmain(int argc, _TCHAR* argv[])
{
Test();
while (true){}
return 0;
}
它导致同样的错误!情节变厚了。我假设是编写自定义分配器的新手,分配器代码是问题所在。仍然不确定为什么我的较小代码会发生此错误...
解决方案
写一个答案,因为我无法评论这个问题。
我在最后一个代码中没有发现任何明显的错误。您确定您正在编译正确的文件而不是旧的未保存版本等吗?
您可以尝试删除该行
while (true){}
并让程序正常结束。
此外,您可以尝试在调试模式下运行代码,单步执行指令以找到导致问题的指令。
推荐阅读
- c++ - 如何向向量添加类函数(即“nortmalVector.customFunction()”)
- python - cygwin 上的 PyInstaller
- json - 如何使用 Oracle 解析没有元素/属性名称的 JSON 数组
- common-lisp - 仅获取 plist 的键
- xcopy - xcopy 到文件而不是目录,可以反转吗?
- tensorflow - 连体多层感知器
- haskell - Haskell中的模棱两可的类型变量问题
- c++ - 为什么这种检测习语的使用会导致 Clang 和 GCC 的不同编译错误而 MSVC 没有
- flutter - 根据整数列表中的数字构建容器
- android - 类似于 TestCoroutineScope 的 RxJava 中暂停和恢复的解决方案