我正在维护一些遗留代码,这些遗留代码在托管的对齐指针类中缺少复制赋值构造函数。我添加了一个如下(简化视图):
#include <iostream>
#include <cstring>
#include <stdlib.h>
template <class T, unsigned AlignB>
class AlignedPtr {
private:
T *mpBlock;
unsigned mBlkSize;
public:
// Size specific Ctor
AlignedPtr(unsigned uNum) :
mpBlock(static_cast<T*> (aligned_alloc(uNum*sizeof(T), AlignB))),
mBlkSize(uNum) {}
// Default, empty Ctor
AlignedPtr(void) : mpBlock(nullptr), mBlkSize(0) {}
// Copy Assignment Ctor
AlignedPtr& operator=(const AlignedPtr& x)
{
T *mpNewBlock(static_cast<T*>(aligned_alloc(x.mBlkSize*sizeof(T), AlignB)));
for (size_t index=0; index < x.mBlkSize; index++) {
mpNewBlock[index] = x.mpBlock[index];
}
free(mpBlock);
mpBlock = mpNewBlock;
mBlkSize = x.mBlkSize;
return *this;
}
// Destroy managed pointer
~AlignedPtr() {
free(mpBlock);
}
};
int main(int argc, char *argv[])
{
AlignedPtr<float, 16> first_ptr;
std::cout << "Pointer Initialized" << std::endl;
first_ptr = AlignedPtr<float, 16>(8);
std::cout << "Pointer Re-initialized" << std::endl;
return 0;
}
我的期望是程序将正常终止,但是当 first_ptr 超出范围(主终止)时,我看到AlignedPtr Dtor失败。 我在上面进行了编译而没有任何优化:
g++ -std=c++11 -g aligned_alloc_bug.cpp -o aab
在带有g ++ 4.8.2的Ubuntu 14.04上,得到以下运行时错误:
Pointer Initialized
Pointer Re-initialized
*** Error in `./aab': free(): invalid next size (fast): 0x0000000001cf9080 ***
Aborted (core dumped)
有趣的是,当我用 malloc 或 posix_memalign 替换 aligned_alloc 时,程序会正确终止。这是 aligned_alloc 中的错误还是我遗漏了一些基本的东西?
P.S:1)我简要搜索了一个返回false的gcc bug。 2)建议尽早确认避免管理原始指针,但我希望对手头的问题有任何帮助。
答案 0 :(得分:1)
问题是您有两个指向同一内存的对象:匿名对象和赋值运算符完成后的first_ptr具有相同的地址。当他们各自的析构者被召唤.....你可能猜到会发生什么。
答案 1 :(得分:0)
您正在以错误的顺序"documentation"将参数传递给aligned_alloc
。
void * aligned_alloc(size_t alignment,size_t size);
然后会导致内存损坏,只有在您调用free
时才会检测到。
此外,您应该考虑实现复制构造函数。这是一个简单的实现:
AlignedPtr(const AlignedPtr& x) { *this = x; }