gpt4 book ai didi

c++ - 使用 g++ 中设置的 -O2 执行 std::string 分配时无效 free

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:00:57 33 4
gpt4 key购买 nike

在我打开另一个问题之前,这个问题与我几天前打开的另一个问题有关:

C++ Program Always Crashes While doing a std::string assign

根据一些答案进一步调查后,我提出了更多问题和更多可能有助于调试问题的信息。

所以这里...

有问题的代码:

bool peopleSProtocol::SignalProtocolCreated(BaseProtocol *pProtocol,
Variant customParameters) {

LOG("peopleSProtocol::SignalProtocolCreated");

SetOutboundConnectParameters(customParameters);

// Tie up the peopleSProtocol Instance to the BaseOutboundStream Instance.
BaseClientApplication *pApplication = ClientApplicationManager::FindAppByName("peoplestreamer");

std::string lStreamName;
printf("1b [%d] [%s]", (int) lStreamName.size(), lStreamName.c_str()); // line 217

SetApplication(pApplication); // line 218

printf("1c [%d] [%s]", (int) lStreamName.size(), lStreamName.c_str()); // line 219

BaseRTAppProtocolHandler *pProtocolHandler = (BaseRTAppProtocolHandler *)pApplication->GetProtocolHandler(PT_OUTBOUNDRT);

printf("1d [%d] [%s]", (int) lStreamName.size(), lStreamName.c_str());

uint32_t protocolId = customParameters["customParameters"]["outboundRTProtocolId"];
uint32_t streamId = customParameters["customParameters"]["streamId"];

printf("1d [%d] [%s]", (int) lStreamName.size(), lStreamName.c_str());

BaseOutboundStream *lpBaseOutboundStream = (BaseOutboundStream*)pProtocolHandler->FindByProtocolIdById(protocolId,streamId);

printf("1e [%d] [%s]", (int) lStreamName.size(), lStreamName.c_str());

RegisterOutboundStream(lpBaseOutboundStream);

printf("2 [%d] [%s]", (int) lStreamName.size(), lStreamName.c_str());

// Get the address of our peer (the RT peer, NOT the peopleS peer)...

int32_t lRTClientFd = lpBaseOutboundStream->GetProtocol()->GetIOHandler()->GetFd();

struct sockaddr_in lPeerAddressStruct;
int lPeerLength = sizeof(lPeerAddressStruct);
getpeername(lRTClientFd,(sockaddr*)&lPeerAddressStruct,(socklen_t*)&lPeerLength);

string lPeerIpAddressString = inet_ntoa(lPeerAddressStruct.sin_addr);

uint16_t lPeerPort = ntohs(lPeerAddressStruct.sin_port);

printf("3 [%d] [%s]", (int) lStreamName.size(), lStreamName.c_str());

// Get the name of the file in accordance with the RT spec...

GetStreamName(STR(M_INVOKE_PARAM(customParameters,1)), lStreamName); // line 247
return SendPlayCommand(lStreamName,lPeerIpAddressString,lPeerPort);
}

bool peopleSProtocol::GetStreamName(const char* pRawPath, std::string &pStreamName)
{
vector<string> parts = split(pRawPath,"/"); // line 260
pStreamName = parts.back(); // line 261

parts = split(pStreamName.c_str(),":");
pStreamName = parts.back();
return true;
}

#define ADD_VECTOR_END(v,i) (v).push_back((i))

vector<string> split(string str, string separator) {
vector<string> result;

string::size_type position = str.find(separator);
uint32_t separatorLength = separator.length();

while (position != str.npos) {
string temp = str.substr(0, position);
ADD_VECTOR_END(result, temp);
str = str.substr(position + separatorLength);
position = str.find(separator);
}
ADD_VECTOR_END(result, str);
return result;
}

GDB 回溯:

(gdb) b 217
Breakpoint 3, peopleSProtocol::SignalProtocolCreated (this=0x3c52020, pProtocol=<value optimized out>, customParameters=@0x413f9eb0) at peopleRTstreamer/src/peoplesprotocol.cpp:217
217 printf("1b [%d] [%s]", (int) lStreamName.size(), lStreamName.c_str());
(gdb) p lStreamName
$1 = {static npos = 18446744073709551615, _M_dataplus = {<std::allocator<char>> = {<__gnu_cxx::new_allocator<char>> = {<No data fields>}, <No data fields>}, _M_p = 0x3c52020 "\220R̪�*"}}
(gdb) c
[DEBUG] cpeopleTcpConnection - Connected to 127.0.0.1:37255
[DEBUG] cpeopleTcpConnection - Connected from server address : 127.0.0.1
[New Thread 1096964416 (LWP 10941)]
Breakpoint 4, peopleSProtocol::SignalProtocolCreated (this=0x3c52020, pProtocol=<value optimized out>, customParameters=@0x413f9eb0) at peopleRTstreamer/src/peoplesprotocol.cpp:218
218 SetApplication(pApplication);
(gdb) p lStreamName
$2 = {static npos = 18446744073709551615, _M_dataplus = {<std::allocator<char>> = {<__gnu_cxx::new_allocator<char>> = {<No data fields>}, <No data fields>}, _M_p = 0x2abbad098e78 ""}}
(gdb) c
Breakpoint 5, peopleSProtocol::SignalProtocolCreated (this=0x3c52020, pProtocol=<value optimized out>, customParameters=@0x413f9eb0) at peopleRTstreamer/src/peoplesprotocol.cpp:219
219 printf("1c [%d] [%s]", (int) lStreamName.size(), lStreamName.c_str());
(gdb) p lStreamName
$3 = {static npos = 18446744073709551615, _M_dataplus = {<std::allocator<char>> = {<__gnu_cxx::new_allocator<char>> = {<No data fields>}, <No data fields>}, _M_p = 0x2abbad098e78 ""}}
(gdb) c
[New Thread 1097099584 (LWP 11010)]
1b [0] []1c [0] []1d [0] []1d [0] [][DEBUG] peopleStreamingServer.RTServer - Opening connection from 127.0.0.1
[DEBUG] peopleStreamingServer.RTServer - Using server IP address 127.0.0.1
Breakpoint 6, peopleSProtocol::SignalProtocolCreated (this=0x3c52020, pProtocol=<value optimized out>, customParameters=@0x413f9eb0) at peopleRTstreamer/src/peoplesprotocol.cpp:247
247 GetStreamName(STR(M_INVOKE_PARAM(customParameters,1)), lStreamName);
(gdb) p lStreamName
$4 = {static npos = 18446744073709551615, _M_dataplus = {<std::allocator<char>> = {<__gnu_cxx::new_allocator<char>> = {<No data fields>}, <No data fields>}, _M_p = 0x2abbad098e78 ""}}
(gdb) c
Breakpoint 7, peopleSProtocol::GetStreamName (this=0x3c52020, pRawPath=0x3c40808 "RT://127.0.0.1/mp4:popeye.mp4", pStreamName=@0x413f9e10) at peopleRTstreamer/src/peoplesprotocol.cpp:260
260 vector<string> parts = split(pRawPath,"/");
(gdb) p pStreamName
$5 = (string &) @0x413f9e10: {static npos = 18446744073709551615, _M_dataplus = {<std::allocator<char>> = {<__gnu_cxx::new_allocator<char>> = {<No data fields>}, <No data fields>},
_M_p = 0x2abbad098e78 ""}}
(gdb) c
*** glibc detected *** /home/ml01/t-live/TEngine/StreamingServer/.libs/peoplestreamingserver: free(): invalid pointer: 0x00002abbad098e60 ***

======= Backtrace: =========
/lib64/libc.so.6[0x3560871634]
/lib64/libc.so.6(cfree+0x8c)[0x3560874c5c]
/home/ml01/t-HEAD/gcc/4.2.4/lib/../lib64/libstdc++.so.6(_ZNSs6assignERKSs+0x90)[0x2abbace3fd20]
/home/ml01/usr/local/lib/RTserver/libpeopleRTstreamer.so(_ZN12peopleSProtocol13GetStreamNameEPKcRSs+0x9a)[0x2aaaaaabd16a]
/home/ml01/usr/local/lib/RTserver/libpeopleRTstreamer.so(_ZN12peopleSProtocol21SignalProtocolCreatedEP12BaseProtocol7Variant+0x2e4)[0x2aaaaaabd894]
/home/ml01/usr/local/lib/RTserver/libpeopleRTstreamer.so(_ZN12TCPConnectorI12peopleSProtocolE7OnEventER11epoll_event+0x1a6)[0x2aaaaaabf046]
/home/ml01/t-HEAD/cRTserver/20091229/lib/libthelib.so(_ZN16IOHandlerManager5PulseEv+0x4a6)[0x2abbad425620]
/home/ml01/usr/local/lib/libpeopleRT.so.0(_ZN3people15cRTServerLoop19RTProtocolHandlerEv+0x55)[0x2abba2d2df45]
/home/ml01/usr/local/lib/libpeopleRT.so.0(_Z19HandleTrafficThreadPv+0x9)[0x2abba2d2e009]
/home/ml01/t-HEAD/glib/2.18.0/lib/libglib-2.0.so.0[0x2abbabc84394]
/lib64/libpthread.so.0[0x35614062f7]
/lib64/libc.so.6(clone+0x6d)[0x35608d1b6d]

(gdb) where
#0 0x0000003560830155 in raise () from /lib64/libc.so.6
#1 0x0000003560831bf0 in abort () from /lib64/libc.so.6
#2 0x000000356086a38b in __libc_message () from /lib64/libc.so.6
#3 0x0000003560871634 in _int_free () from /lib64/libc.so.6
#4 0x0000003560874c5c in free () from /lib64/libc.so.6
#5 0x00002abbace3fd20 in std::string::assign (this=0x413f9e10, __str=<value optimized out>) at /home/ml01/ThirdParty/sources/gcc-4.2.4/x86_64-pc-linux-gnu/libstdc++-v3/include/bits/basic_string.h:238
#6 0x00002aaaaaabd16a in peopleSProtocol::GetStreamName (this=<value optimized out>, pRawPath=0x3c40808 "RT://127.0.0.1/mp4:popeye.mp4", pStreamName=@0x413f9e10)
at /home/ml01/t-HEAD/gcc/4.2.4/lib/gcc/x86_64-pc-linux-gnu/4.2.4/../../../../include/c++/4.2.4/bits/basic_string.h:491
#7 0x00002aaaaaabd894 in peopleSProtocol::SignalProtocolCreated (this=0x3c52020, pProtocol=<value optimized out>, customParameters=@0x413f9eb0) at peopleRTstreamer/src/peoplesprotocol.cpp:247
#8 0x00002aaaaaabf046 in TCPConnector<peopleSProtocol>::OnEvent (this=0x3c40aa0, event=<value optimized out>) at /home/ml01/t-HEAD/cRTserver/20091229/include/cRTserver/netio/epoll/tcpconnector.h:90
#9 0x00002abbad425620 in IOHandlerManager::Pulse () at /home/ml01/ThirdParty/sources/cRTserver/sources/thelib/src/netio/epoll/iohandlermanager.cpp:260
#10 0x00002abba2d2df45 in people::cRTServerLoop::RTProtocolHandler (this=0x3c4c180) at src/RTServerLoop.cpp:77
#11 0x00002abba2d2e009 in HandleTrafficThread (ipData=0x60bc) at src/RTServerLoop.cpp:39
#12 0x00002abbabc84394 in g_thread_create_proxy (data=0x3c4e060) at gthread.c:635
#13 0x00000035614062f7 in start_thread () from /lib64/libpthread.so.0
#14 0x00000035608d1b6d in clone () from /lib64/libc.so.6

所以为了解释以上内容,崩溃发生在字符串分配期间的第 261 行(请参阅代码中的行号注释)。出于某种原因,它试图释放无效内存,但我不确定为什么?它试图释放的地址不是我认为是第 217 行分配的空字符串的地址(_M_p = 0x3c52020 设置为 _M_p = 0x2abbad098e78 即空字符串),而是一些其他地址相距不远。

有什么可能导致此问题的想法吗?我想知道这是否可能是一个优化问题,因为它不会出现在 -O0 中并且我已经通过一系列测试运行代码以尝试找到可能导致这种奇怪行为的一大堆内存损坏问题...... .空手而归!

另一个有趣的注意事项是,如果我设置 std::string lStreamName = "";而不是 std::string lStreamName;程序不再崩溃???任何想法可能会导致这种情况?为什么这个字符串的简单初始化可以消除崩溃?

谢谢!

最佳答案

如果不能保证拆分不会返回空 vector ,我会添加一个检查是否为空。否则将空 vector 的 back() 分配给字符串可能会导致您收到的行为。

bool GetStreamName(const char* pRawPath, std::string& pStreamName)
{
std::vector<std::string> parts = split(pRawPath,'/'); // line 260
if( parts.size() == 0 )
{
cout << "Cannot parse '" << pRawPaths << "'" << endl;
return false;
}
pStreamName = parts.back(); // line 261

parts = split(pStreamName.c_str(),':');
pStreamName = parts.back();
return true;
}

关于c++ - 使用 g++ 中设置的 -O2 执行 std::string 分配时无效 free,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2314642/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com