Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/125.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 应用程序在字符串上追加数据时崩溃_C++_String_Macos_Stdstring - Fatal编程技术网

C++ 应用程序在字符串上追加数据时崩溃

C++ 应用程序在字符串上追加数据时崩溃,c++,string,macos,stdstring,C++,String,Macos,Stdstring,我有一个跨平台的控制台应用程序。它的目的是在两个对等点之间传输数据。有时收到的数据包不完整,所以我们在完整数据包到达时将数据附加到字符串中。但过了一段时间,它在字符串中追加数据时崩溃了。这是后备箱- 1 libsystem_c.dylib 0x93b77acf pthread_kill + 101 2 libsystem_c.dylib 0x93bae4f8 abort + 168 3 libc++abi.dylib 0x9698180c abort_message + 151 4 libc++

我有一个跨平台的控制台应用程序。它的目的是在两个对等点之间传输数据。有时收到的数据包不完整,所以我们在完整数据包到达时将数据附加到字符串中。但过了一段时间,它在字符串中追加数据时崩溃了。这是后备箱-

1 libsystem_c.dylib 0x93b77acf pthread_kill + 101
2 libsystem_c.dylib 0x93bae4f8 abort + 168
3 libc++abi.dylib 0x9698180c abort_message + 151
4 libc++abi.dylib 0x9697f275 default_terminate() + 34
5 libc++abi.dylib 0x9697f2b5 safe_handler_caller(void (*)()) + 13
6 libc++abi.dylib 0x9697f31d std::terminate() + 23
7 libc++abi.dylib 0x96980412 __cxa_throw + 110
8 libstdc++.6.dylib 0x90e23d6c std::__throw_length_error(char const*) + 104
9 libstdc++.6.dylib 0x90e4f3a9 std::string::append(char const*, unsigned long) + 175
10 libConnector.dylib 0x13905228 ConnectionSocket::AdjustPartialData(char const*, int) 
14 libConnector.dylib 0x1383e0b6 ConnectionChannel::ProcessData(int, void const*, int, char*, int) + 7886
15 libConnector.dylib 0x13861ecb ConnectionManager::BaseThreadImpl() + 1185
16 libConnector.dylib 0x13861a23 ConnectionManager::BaseThread(void*) + 17
17 libsystem_c.dylib 0x93b76557 _pthread_start + 344
18 libsystem_c.dylib 0x93b60cee thread_start + 34 
有人能告诉我这个问题吗

示例代码:


如果partialDataBuffer.size+dataLen>partialDataBuffer。然后append抛出一个长度错误


partialDataBuffer或dataLen太大。

最后,我通过丢弃导致溢出的额外数据解决了这个问题。以前我认为这不是可行的解决方案,因为它可能会导致数据丢失。但是我发现TCP流的大小不应该大于65535,也就是16位。但在添加以下条件时发现另一个问题-

if(partialDataBuffer.size() + dataLen >= partialDataBuffer.max_size())
{
 // do not append data
}
else  partialDataBuffer.append(pData, dataLen);
问题是在windows平台中,即使partialDataBuffer的大小远小于partialDataBuffer.max_大小,它也会崩溃。因此,为了解决这个问题,我做了以下工作:

try{
 partialDataBuffer.append(pData, dataLen);
}
catch(...)
{
// got exception, return
}
这对我来说很好


注意:我发布了我的答案,因为它将对其他面临类似问题的人有所帮助。

请展示一个小的可复制代码示例。示例代码已添加到编辑后的帖子中。dataLen中可能有一些错误,可能在从有符号值到无符号值的转换中,尝试打印它或在dbugger中设置观察点:调试器说了什么如果生成的字符串长度超过max_size,将引发length_错误异常。是的,我考虑过该选项,但它可能会导致数据丢失。我想避免数据丢失。用更合适的数据结构替换字符串?或者更好地处理数据。不要只是让它在字符串中累积。@如果您仍然没有给出为什么在程序中的某个点不处理数据就在字符串中累积所有这些数据的信息。你的绳子便秘了。通常的解决方案是通过处理存在的数据来清除字符串,这样可以向字符串中添加更多的数据而不会出现问题。@Naseef-基本上,您需要查看字符串何时溢出边缘,当溢出边缘时,创建一个新字符串并使其获得多余的数据。然后这个新字符串将继续获取数据。冲洗并重复。同样,与简单的单字符串方法相比,它需要更多的逻辑,但希望您能理解。使用vector可以在短时间内完成此操作。但是,Windows与其他平台之间会存在差异。我的意思是,Windows的缓冲区可能比其他平台更短/更大。这可能会导致一些特定于平台的行为。实际上,max_size在不同的平台上也会返回不同的值。
try{
 partialDataBuffer.append(pData, dataLen);
}
catch(...)
{
// got exception, return
}