簡體   English   中英

AddressSanitizer GCC 4.8的雙重免費錯誤

[英]Double Free error with AddressSanitizer GCC 4.8

考慮以下玩具程序( 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的情況下提到,以及其他各種文件,但沒有任何效果。

另外,從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需要一個簡單的可復制類型,你的A不是,因為它的vector<int>類型的成員(例如)不是簡單的可復制構造。

GCC僅檢測自5.0版以來違反該要求的情況。

舊的gcc版本編譯代碼的事實並不意味着它是有效的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM