复制赋值构造函数中的aligned_alloc内存块在释放时崩溃
aligned_alloc memory block in a copy assignment constructor crashes while being freed
我正在维护一些遗留代码,这些代码在托管对齐指针类中缺少复制赋值构造函数。我添加了一个如下(简化视图(:
#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;
}
我的期望是程序将正常终止,但是我看到 AlignedPtr Dtor 在超出范围(主终止(时first_ptr失败。我在上面编译时没有任何优化:
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中的错误还是我缺少一些基本的东西?
附言:1(我简短地搜索了一个返回false的gcc错误。2(建议避免管理原始指针是提前确认的,但我希望对手头的问题有任何帮助。
问题是您有两个对象指向相同的内存:匿名对象和赋值运算符完成后的first_ptr具有相同的地址。当他们各自的析构函数被调用时.....你大概可以猜到会发生什么。
您以错误的顺序将参数传递给aligned_alloc
,"文档"。
空 *aligned_alloc(size_t对齐,size_t大小(;
然后,这会导致内存损坏,只有在调用 free
时才会检测到内存损坏。
此外,还应考虑实现复制构造函数。下面是一个简单的实现:
AlignedPtr(const AlignedPtr& x) { *this = x; }
相关文章:
- 包含矢量指针的结构的内存释放问题
- Valgrind 声称内存释放中的自由空间太多
- 当 C 和 C++ 中严格要求内存释放时
- C++ 中指针变量的内存释放
- 德克内存释放
- 临时对象 c++ 的内存释放
- C 全局对象变量内存释放
- C++矢量动态内存释放/删除
- 从 cv::Mat 初始化的 IplImage 的内存释放
- SWIG类型映射中的内存释放
- STL容器中的内存释放
- QWebView内存释放
- 内存释放 C++
- 大返回值(如字符串)的内存释放如何在C++中发生
- 重新分配之前的内存释放
- Linux C++ 中的内存释放
- VS2012编译器奇怪的内存释放问题
- 调试断言失败!错误的内存释放
- 负责 COM 互操作中的内存释放
- imread命令后OpenCV矩阵内存释放