以前可能会问过这个问题,但我找不到我需要的确切内容。
我的问题是,我有一个从 web 服务下载的数据加载的缓冲区。缓冲区采用 unsigned char* 形式,最后没有 '\0'。然后我有一个 poco xml 解析器需要一个字符串。
我尝试将它分配给字符串 valgrind 发现一些丢失的数据。(见下文)
这是代码:
DOMParser::DOMParser(unsigned char* consatData, int consatDataSize,
unsigned char* lagData, int lagDataSize) {
Poco::XML::DOMParser parser;
std::string consat;
consat.assign((const char*) consatData, consatDataSize);
pDoc = parser.parseString(consat);
ParseConsat();
}
Poco xml 解析器确实有一个 ParseMemory ,它需要一个 const char* 和数据大小,但由于某种原因,它只是给了我分段错误。
更新:这是 valgrind 结果的一部分:
==11880== 12,272 bytes in 1 blocks are possibly lost in loss record 1,126 of 1,143
==11880== at 0x402569A: operator new(unsigned int) (vg_replace_malloc.c:255)
==11880== by 0x4491D05: std::string::_Rep::_S_create(unsigned int, unsigned int, std::allocator<char> const&) (in /usr/lib/libstdc+$
==11880== by 0x4493F6F: std::string::_M_mutate(unsigned int, unsigned int, unsigned int) (in /usr/lib/libstdc++.so.6.0.13)
==11880== by 0x4494109: std::string::_M_replace_safe(unsigned int, unsigned int, char const*, unsigned int) (in /usr/lib/libstdc++.$
==11880== by 0x44941AD: std::string::assign(char const*, unsigned int) (in /usr/lib/libstdc++.so.6.0.13)
==11880== by 0x804DE03: DOMParser::DOMParser(unsigned char*, int, unsigned char*, int) (DOMParser.cpp:27)