c++ - AddressSanitizer GCC 4.8 双重释放错误

标签 c++ gcc atomic noexcept address-sanitizer

考虑以下玩具程序(prog.cpp):

class A {
public:
    vector<int> vec;
    A() noexcept {}
    A(vector<int> s) : vec(s) {}
};

class B {

private:
    vector<atomic<A>> a_table;

public:
    B(int capacity) : a_table(capacity) {}

    void update(int index) {
        A newValue(vector<int>(10,1));
        a_table[index].store(newValue);
    }
};


int main(int argc, char** argv) 
{
B b(5);
b.update(2);
return 0;
}

正常编译时(g++ prog.cpp -latomic),工作正常。但是,当编译为 g++ -fsanitize=address -fno-omit-frame-pointer prog.cpp -latomic 时,执行时会产生 Double Free 错误。基于与上述类似行的程序必须在多线程应用程序中使用,即使是正常编译也会产生 Double Free 错误。我阅读了 Double Free 情况下通常提到的三/五规则和其他各种文档,但没有任何效果。

此外,从 A 类 的默认构造函数中删除 noexcept 说明符会产生这个奇怪的错误,我也想知道这一点。

error: function ‘std::atomic<_Tp>::atomic() [with _Tp = A]’ defaulted on its first declaration with an exception-specification that differs from the implicit declaration ‘std::atomic<A>::atomic()’
   atomic() noexcept = default;
   ^

最佳答案

std::atomic需要 trivially copyable类型,你的 A不是,因为它的成员类型为 vector<int> (例如)不是简单的可复制构造的。

GCC only detects a violation of that requirement since version 5.0.

旧 gcc 版本编译代码的事实并不意味着它是有效的。

关于c++ - AddressSanitizer GCC 4.8 双重释放错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39936828/

相关文章:

node.js - node fs.fsync(什么时候使用?)

c++ - 如何将一个范围内的更新索引锁定为最大值?

c++ - 为什么不允许无符号 OpenMP 索引变量?

c++ - qDebug 和 QString const 引用的问题

c++ - 如果删除一个对象,究竟会发生什么? (gcc)(当双删除崩溃时?)

linux - 检查 g++ 的返回值

cuda - 何时以及为何在 CUDA 中使用atomicInc()?

javascript - Webassembly:可能有共享对象吗?

c++ - 内存排序、指令重新排序和缺少 happens-before 关系

gcc - 编译器是否真的产生机器码?