简体   繁体   English

AddressSanitizer GCC 4.8的双重免费错误

[英]Double Free error with AddressSanitizer GCC 4.8

Consider the following toy program( prog.cpp ): 考虑以下玩具程序( 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;
}

This when compiled normally ( g++ prog.cpp -latomic ), works fine. 这通常编译时( g++ prog.cpp -latomic ),工作正常。 But when compiled as g++ -fsanitize=address -fno-omit-frame-pointer prog.cpp -latomic produces Double Free error when executed. 但是当编译为g++ -fsanitize=address -fno-omit-frame-pointer prog.cpp -latomic在执行时会产生Double Free错误。 A program based on similar lines as above has to be used in a multi-threaded application, where even the normal compilation is producing Double Free error. 基于类似行的程序必须在多线程应用程序中使用,即使正常编译也会产生Double Free错误。 I read up Rule of Three/Five, which is generally referred to in case of Double Free, and various other documentations but nothing worked. 我读了三/五规则,一般在Double Free的情况下提到,以及其他各种文件,但没有任何效果。

Also, removing noexcept specifier from the class A 's default constructor produces this strange error, which also I would like to know about. 另外,从class 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 requires a trivially copyable type, which your A is not, because its member of type vector<int> (eg) is not trivially copy constructible. std::atomic需要一个简单的可复制类型,你的A不是,因为它的vector<int>类型的成员(例如)不是简单的可复制构造。

GCC only detects a violation of that requirement since version 5.0. GCC仅检测自5.0版以来违反该要求的情况。

The fact that older gcc versions compile the code does not mean that it is valid. 旧的gcc版本编译代码的事实并不意味着它是有效的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM