c++ - 为什么vector在reserve的时候会产生段错误?

查看:590
本文介绍了c++ - 为什么vector在reserve的时候会产生段错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

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 '\000', 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文件的调用栈,恕不方便贴代码,出现这种情况一般都会有哪几种原因呢?

解决方案

出现了Segmentation fault,基本上的原因是,非法的内存访问。你的报错信息中很多都出现了malloc,就是申请内存的意思。vector 的reserve增加了vector的capacity,但是它的size没有改变!reserve是容器预留空间,但在空间内不真正创建元素对象,所以在没有添加新的对象之前,不能引用容器内的元素。加入新的元素时,要调用push_back()/insert()函数。你没有代码,我也很难判断到底是什么原因,以上是我个人见解,希望能帮到你,谢谢你邀请我回答。

这篇关于c++ - 为什么vector在reserve的时候会产生段错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆