出现此 Valgrind 错误的原因是什么?
Valgrind 正在抱怨 substr 调用。
string Message::nextField(string& input) {
int posSeparator = input.find_first_of(SEPARATOR);
string temp;
temp = input.substr(0, posSeparator); //Error points to this line
input.erase(0, posSeparator + 1);
return temp;
}
错误如下:
12个块中的290个字节肯定在丢失记录1 of 1中丢失
该函数的作用基本上是解析输入,返回由分隔符字符分隔的字符串部分。该函数是从另一个类的方法中调用的,并具有下一个定义:
void doSomething(string input) {
input.erase(0,2);
string temp = nextField(input);
this->room = atoi(temp.c_str());
temp = input;
this->money = atoi(temp.c_str());
}
没有任何其他奇怪或重要的内容可以包含在此处。 我使用 Eclipse Indigo 的 Valgrind 分析中的 Valgrind 默认设置。 有什么想法吗?
Valgrind is complaining with a substr invocation.
string Message::nextField(string& input) {
int posSeparator = input.find_first_of(SEPARATOR);
string temp;
temp = input.substr(0, posSeparator); //Error points to this line
input.erase(0, posSeparator + 1);
return temp;
}
The error goes:
290 bytes in 12 blocks are definitely lost in loss record 1 of 1
What the function does is basically parse the input, returning portions of string separated by SEPARATOR character. This function is invoked from another class's method with the next definition:
void doSomething(string input) {
input.erase(0,2);
string temp = nextField(input);
this->room = atoi(temp.c_str());
temp = input;
this->money = atoi(temp.c_str());
}
There's nothing else weird or important enough to be included here.
I use the default setup for Valgrind from Eclipse Indigo's Valgrind profiling.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这可能不是您的代码中的错误。由于 C++ 标准库的实现细节,可能会报告此错误。要验证这一点,请尝试 Valgrind 常见问题解答中的以下内容:
It is probably not a bug in your code. This error could be reported due to details of implementation of C++ standard library. To verify this try the following from Valgrind FAQ:
您的源代码中的其他地方可能有错误。我尝试使用以下代码复制错误:
然后运行 valgrind:
,输出为:
所以,您的问题可能不在这部分代码中
You probably have an error somewhere else in your source. I tried to replicate the error using the following code:
Then a ran valgrind:
and the output was:
So, probably your problem is not in this part of code
您不检查 posSeparator 是否实际上与 string::npos 不同 - 这可能会导致擦除问题。这是一个疯狂的尝试,但无论如何它可能会修复一个错误。
You don't check if the posSeparator is actually different from string::npos - this might cause problems in the erase. It's a wild shot, but it might fix a bug anyway.