首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用AddressSanitizer时出现双重自由错误,GCC 4.8

使用AddressSanitizer时出现双重自由错误,GCC 4.8
EN

Stack Overflow用户
提问于 2016-10-09 04:03:03
回答 1查看 678关注 0票数 3

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

代码语言:javascript
复制
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 error。我阅读了三/五规则,这通常是指在双重释放的情况下,以及各种其他文档,但都没有工作。

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

代码语言:javascript
复制
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;
   ^
EN

回答 1

Stack Overflow用户

发布于 2016-10-09 04:44:32

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

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

尽管老版本的gcc编译了这些代码,但这并不意味着它是有效的。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39936828

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档