复制赋值构造函数中的aligned_alloc内存块在释放时崩溃

aligned_alloc memory block in a copy assignment constructor crashes while being freed

本文关键字:内存 释放 崩溃 alloc 构造函数 赋值 aligned 复制      更新时间:2023-10-16

我正在维护一些遗留代码,这些代码在托管对齐指针类中缺少复制赋值构造函数。我添加了一个如下(简化视图(:

#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)

有趣的是,当我用mallocposix_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; }