出现此 Valgrind 错误的原因是什么?

发布于 2024-12-05 14:41:18 字数 720 浏览 0 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一抹淡然 2024-12-12 14:41:18

这可能不是您的代码中的错误。由于 C++ 标准库的实现细节,可能会报告此错误。要验证这一点,请尝试 Valgrind 常见问题解答中的以下内容:

使用 GCC 2.91、2.95、3.0 和 3.1,使用 STL 编译所有源代码
与-D__USE_MALLOC。提防!这已从 GCC 中删除,开始于
版本3.3。

对于 GCC 3.2.2 及更高版本,您应该导出环境变量
在运行程序之前使用 GLIBCPP_FORCE_NEW。

在 GCC 3.4 及更高版本中,该变量已更改名称为
GLIBCXX_FORCE_NEW。

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:

With GCC 2.91, 2.95, 3.0 and 3.1, compile all source using the STL
with -D__USE_MALLOC. Beware! This was removed from GCC starting with
version 3.3.

With GCC 3.2.2 and later, you should export the environment variable
GLIBCPP_FORCE_NEW before running your program.

With GCC 3.4 and later, that variable has changed name to
GLIBCXX_FORCE_NEW.

浅笑轻吟梦一曲 2024-12-12 14:41:18

您的源代码中的其他地方可能有错误。我尝试使用以下代码复制错误:

#include <string>
#include <iostream>
#include <cstdlib>

using namespace std;

const char SEPARATOR = ':';

struct Foo
{
public:
    int room;
    int money;

    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());
    }

    string 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;
    }
};

int main()
{
    Foo f;
    f.doSomething("--234:12");
    std::cout << f.room << " - " << f.money << std::endl;
}

然后运行 ​​valgrind:

valgrind --tool=memcheck <executable>

,输出为:

HEAP SUMMARY:
    in use at exit: 0 bytes in 0 blocks
  total heap usage: 2 allocs, 2 frees, 61 bytes allocated

All heap blocks were freed -- no leaks are possible

For counts of detected and suppressed errors, rerun with: -v
ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 4 from 4)

所以,您的问题可能不在这部分代码中

You probably have an error somewhere else in your source. I tried to replicate the error using the following code:

#include <string>
#include <iostream>
#include <cstdlib>

using namespace std;

const char SEPARATOR = ':';

struct Foo
{
public:
    int room;
    int money;

    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());
    }

    string 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;
    }
};

int main()
{
    Foo f;
    f.doSomething("--234:12");
    std::cout << f.room << " - " << f.money << std::endl;
}

Then a ran valgrind:

valgrind --tool=memcheck <executable>

and the output was:

HEAP SUMMARY:
    in use at exit: 0 bytes in 0 blocks
  total heap usage: 2 allocs, 2 frees, 61 bytes allocated

All heap blocks were freed -- no leaks are possible

For counts of detected and suppressed errors, rerun with: -v
ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 4 from 4)

So, probably your problem is not in this part of code

苍白女子 2024-12-12 14:41:18

您不检查 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文