c++ - 為什麼vector在reserve的時候會產生段錯誤?
巴扎黑
巴扎黑 2017-06-23 09:14:58
0
1
1470
Program terminated with signal 11, Segmentation fault.
#0  0x000000368d275fd5 in malloc_consolidate () from /lib64/libc.so.6
Missing separate debuginfos, use: debuginfo-install bzip2-libs-1.0.5-7.el6_0.x86_64 cyrus-sasl-lib-2.1.23-13.el6_3.1.x86_64 glibc-2.12-1.166.el6_7.7.x86_64 keyutils-libs-1.4-5.el6.x86_64 krb5-libs-1.10.3-65.el6.x86_64 libcom_err-1.41.12-23.el6.x86_64 libcurl-7.19.7-37.el6_5.3.x86_64 libgcc-4.4.7-4.el6.x86_64 libidn-1.18-2.el6.x86_64 libselinux-2.0.94-7.el6.x86_64 libssh2-1.4.2-1.el6.x86_64 libstdc++-4.4.7-4.el6.x86_64 nspr-4.10.6-1.el6_5.x86_64 nss-3.16.1-4.el6_5.x86_64 nss-softokn-freebl-3.14.3-10.el6_5.x86_64 nss-util-3.16.1-1.el6_5.x86_64 openldap-2.4.23-34.el6_5.1.x86_64 openssl-1.0.1e-57.el6.x86_64 zlib-1.2.3-29.el6.x86_64
(gdb) bt
#0  0x000000368d275fd5 in malloc_consolidate () from /lib64/libc.so.6
#1  0x000000368d279c28 in _int_malloc () from /lib64/libc.so.6
#2  0x000000368d27ab1c in malloc () from /lib64/libc.so.6
#3  0x000000368debd09d in operator new(unsigned long) () from /usr/lib64/libstdc++.so.6
#4  0x000000000040fbcd in __gnu_cxx::new_allocator<unsigned char>::allocate (this=0x7ffc7e0136d8, __n=4096)
    at /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/ext/new_allocator.h:89
#5  0x000000000040f575 in std::_Vector_base<unsigned char, std::allocator<unsigned char> >::_M_allocate (this=0x7ffc7e0136d8, __n=4096)
    at /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/stl_vector.h:140
#6  0x000000000040f186 in std::vector<unsigned char, std::allocator<unsigned char> >::_M_allocate_and_copy<unsigned char*> (this=0x7ffc7e0136d8, __n=4096, 
    __first=0x0, __last=0x0) at /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/stl_vector.h:963
#7  0x000000000040e7c7 in std::vector<unsigned char, std::allocator<unsigned char> >::reserve (this=0x7ffc7e0136d8, __n=4096)
    at /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/vector.tcc:74
#8  0x00000000004cc616 in ByteBuffer::ByteBuffer (this=0x7ffc7e0136c0) at ../../../../src/shared/./util/ByteBuffer.h:19
#9  0x00000000004cb947 in ReplayMgr::SaveToDB (this=0x23a40d0, pBase=0x57a1160) at ./Replay/ReplayMgr.cpp:95
#10 0x0000000000484aa1 in Room_YunNan_FeiXiaoJi_MahJong::OnEventGameConclude (this=0x5a163b0, cbReason=0 'rrreee0', isNormal=true)
    at ./Room/Room_YunNan_FeiXiaoJi_MahJong.cpp:849
#11 0x000000000048c33a in Room_YunNan_FeiXiaoJi_MahJong::OnUserOperateCard (this=0x5a163b0, wChairID=3, cbOperateCode=64 '@', cbOperateCard=0x7ffc7e013d59 "")
    at ./Room/Room_YunNan_FeiXiaoJi_MahJong.cpp:2436
#12 0x000000000046771b in Player::HandleYNFXJOperateCard (this=0x27d7260, recvPacket=...) at ./Player/Player.cpp:513
#13 0x000000000043bcbc in GameSession::OnDealData (this=0x7fa9e4000b30, packet=0x7fa9e4000930) at ./GameSession/GameSession.cpp:61
#14 0x0000000000524f2d in Session::DealPacket (this=0x7fa9e4000b30) at ./Net/Session.cpp:110
#15 0x0000000000525044 in Session::DealData (this=0x7fa9e4000b30) at ./Net/Session.cpp:149
#16 0x00000000005253c9 in Session::Update (this=0x7fa9e4000b30) at ./Net/Session.cpp:264
#17 0x0000000000525ec7 in SessionMgr::Update (this=0x23623d0) at ./Net/SessionMgr.cpp:66
#18 0x00000000004bc11d in UserInterface::OnRun (this=0xab1c60) at ./User/UserInterface.cpp:124
#19 0x0000000000475811 in Master::Loop (this=0xab1bc0) at ./Master.cpp:217
#20 0x000000000047532a in Master::Run (this=0xab1bc0) at ./Master.cpp:84
#21 0x00000000004c841c in unix_main (argc=1, argv=0x7ffc7e014248) at ./main.cpp:119
#22 0x00000000004c84b7 in main (argc=1, argv=0x7ffc7e014248) at ./main.cpp:142

這個是core檔的呼叫棧,恕不方便貼程式碼,出現這種情況一般都會有哪幾種原因呢?

巴扎黑
巴扎黑

全部回覆(1)
某草草

出現了Segmentation fault,基本上的原因是,非法的記憶體存取。你的報錯資訊中很多都出現了malloc,就是申請記憶體的意思。 vector 的reserve增加了vector的capacity,但它的size沒有改變! reserve是容器預留空間,但在空間內不真正建立元素對象,所以在沒有新增新的物件之前,不能引用容器內的元素。加入新的元素時,要呼叫push_back()/insert()函數。你沒有程式碼,我也很難判斷到底是什麼原因,以上是我個人見解,希望能幫到你,謝謝你邀請我回答。

熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板