【问题标题】:std::vector::push_back() crashstd::vector::push_back() 崩溃
【发布时间】:2021-06-18 16:28:46
【问题描述】:

所有,

由于某种原因,我在 std::vector 中执行 push_back 时发生崩溃。

下面是回溯和最后执行的行:

(gdb) s std::vector<DataEditFiield, std::allocator<DataEditFiield> >::push_back (this=0x7fffd6ffc9b0, __x=...)
    at /usr/lib/gcc/x86_64-pc-linux-gnu/7.3.0/include/g++-v7/bits/stl_vector.h:954
954       { emplace_back(std::move(__x)); }
(gdb) fin Run till exit from #0  std::vector<DataEditFiield, std::allocator<DataEditFiield> >::push_back (this=0x7fffd6ffc9b0, __x=...)
    at /usr/lib/gcc/x86_64-pc-linux-gnu/7.3.0/include/g++-v7/bits/stl_vector.h:954

Thread 11 "dbhandler" received signal SIGSEGV, Segmentation fault.
0x00007ffff541bea0 in __memcpy_ssse3 () from /lib64/libc.so.6 (gdb) bt
#0  0x00007ffff541bea0 in __memcpy_ssse3 () from /lib64/libc.so.6
#1  0x00007ffff5f90943 in std::__cxx11::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t>
>::_M_assign(std::__cxx11::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> > const&) () from
/usr/lib/gcc/x86_64-pc-linux-gnu/7.3.0/libstdc++.so.6
#2  0x00007ffff5f90cc9 in std::__cxx11::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t>
>::operator=(std::__cxx11::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> > const&) () from
/usr/lib/gcc/x86_64-pc-linux-gnu/7.3.0/libstdc++.so.6
#3  0x00007fffe30ebdd5 in DataEditFiield::ValuueType::ValuueType (this=0x7fffcc001420, myvalue=...) at ../../dbinterface/database.h:53
#4  0x00007fffe30f9b79 in DataEditFiield::DataEditFiield (this=0x7fffcc001410) at ../../dbinterface/database.h:40
#5  0x00007fffe30f9bc8 in __gnu_cxx::new_allocator<DataEditFiield>::construct<DataEditFiield, DataEditFiield> (this=0x7fffd6ffc9b0, 
    __p=0x7fffcc001410, __args#0=...) at /usr/lib/gcc/x86_64-pc-linux-gnu/7.3.0/include/g++-v7/ext/new_allocator.h:136
#6  0x00007fffe30f7042 in std::allocator_traits<std::allocator<DataEditFiield>
>::construct<DataEditFiield, DataEditFiield> (__a=..., 
    __p=0x7fffcc001410, __args#0=...) at /usr/lib/gcc/x86_64-pc-linux-gnu/7.3.0/include/g++-v7/bits/alloc_traits.h:475
#7  0x00007fffe30f7157 in std::vector<DataEditFiield, std::allocator<DataEditFiield> >::_M_realloc_insert<DataEditFiield>
(this=0x7fffd6ffc9b0, 
    __position=..., __args#0=...) at /usr/lib/gcc/x86_64-pc-linux-gnu/7.3.0/include/g++-v7/bits/vector.tcc:415
#8  0x00007fffe30f3b76 in std::vector<DataEditFiield, std::allocator<DataEditFiield> >::emplace_back<DataEditFiield>
(this=0x7fffd6ffc9b0, 
    __args#0=...) at /usr/lib/gcc/x86_64-pc-linux-gnu/7.3.0/include/g++-v7/bits/vector.tcc:105
#9  0x00007fffe30f04d6 in std::vector<DataEditFiield, std::allocator<DataEditFiield> >::push_back (this=0x7fffd6ffc9b0,
__x=...)
    at /usr/lib/gcc/x86_64-pc-linux-gnu/7.3.0/include/g++-v7/bits/stl_vector.h:954
#10 0x00007fffe30eb44c in SQLiteDatabase::EditTableData (this=0x555555e918f0, row=..., errorMsg=...)
    at /home/igor/dbhandler/libsqlite/database_sqlite.cpp:2044
#11 0x00007fffd5eb60ba in DBTableEdit::Entry (this=0x555555880110) at /home/igor/dbhandler/libtabledataedit/dbtableedit.cpp:48
#12 0x00007ffff63b6bb5 in wxThread::CallEntry (this=0x555555880110) at ../include/wx/thrimpl.cpp:356
#13 0x00007ffff63b11a1 in wxThreadInternal::PthreadStart (thread=0x555555880110) at ../src/unix/threadpsx.cpp:891
#14 0x00007ffff63b0c31 in wxPthreadStart (ptr=0x555555880110) at ../src/unix/threadpsx.cpp:841
#15 0x00007ffff56a3aba in start_thread () from /lib64/libpthread.so.0
#16 0x00007ffff53d870f in clone () from /lib64/libc.so.6

有什么问题?我需要实施什么吗?

STL 代码不应崩溃...

TIA!!

我正在使用的代码:

struct DataEditFiield
{
    int type, m_size, m_precision;
    union ValuueType
    {
        void *blobValue;
        int intValue;
        double doubleValue;
        std::wstring stringValue;
        ValuueType(int value) : intValue( value ) {}
        ValuueType(double value) : doubleValue( value ) {}
        ValuueType(std::wstring value) : stringValue( value ) {}
        ValuueType(const void *value) : blobValue( const_cast<void *>( value ) ) {}
        ValuueType(const ValuueType &myvalue) { stringValue = myvalue.stringValue; }
#if defined _MSC_VER
        __int64 longvalue;
        ValuueType(__int64 value) : longvalue( value ) {}
#else
        long long int longvalue;
        ValuueType(long long int value) : longvalue( value ) {}
#endif
        ~ValuueType() noexcept {}
    } value;

    DataEditFiield(int myvalue) : type( INTEGER_TYPE ), m_size( 0 ), m_precision( 0 ), value( myvalue ) { }

    DataEditFiield(double myvalue, int size, int precision) : type( DOUBLE_TYPE ), m_size( size ), m_precision( precision ), value( myvalue ) { }

    DataEditFiield(std::wstring myvalue) : type( STRING_TYPE ), m_size( 0 ), m_precision( 0 ), value( myvalue ) {}

    DataEditFiield(const void *myvalue) : type( BLOB_TYPE ), m_size( 0 ), m_precision( 0 ), value( myvalue ) {}

#if defined _MSC_VER
    DataEditFiield(__int64 myvalue) : type( INTEGER_TYPE ), m_size( 0 ), m_precision( 0 ), value( myvalue ) {}
#else
    DataEditFiield(long long int myvalue) : type( 1 ), value( myvalue ) {}
#endif
    ~DataEditFiield()
    {
        using std::wstring;
        if( type ==3 )
            value.stringValue.~wstring();
    }
};

std::vector<DataEditFiield> row;
row..push_back( DataEditFiield( sqlite3_column_int64( m_stmt, i ) ) );

【问题讨论】:

  • 您需要创建一个minimal reproducible example。否则我们无法为您提供帮助。
  • 看起来崩溃是在 std::string() 的构造函数中,作为构造 DataEditFiield 的一部分。您确定将有效指针传递给std::string() 吗?这是在后台线程中,您是否有保护数据免受不同线程中的并发访问的问题?
  • @bolov,添加了代码。你能看出问题吗?
  • @Bids,我添加了代码。它不是 std::string - 我正在尝试添加一个整数。
  • @Igor std::vector&lt;DataEditFiield&gt; &amp;row; 不会编译。不初始化就无法创建引用。确保您的代码是正确的。之后,1) 确保对sqlite3_column_int64 的调用正在传递,2) 确保DataEditFiield 的构造函数正在传递。

标签: c++11 stl


【解决方案1】:

致所有感兴趣的人:

解决方案是向 DataEditField 添加一个复制构造函数。

显然 union 不会产生工作默认值。如果编译器直接丢弃它并发出警告会很好,但我想它太麻烦了。

感谢所有感兴趣的人。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-10-15
    • 2023-03-21
    • 2013-10-26
    • 2015-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多