简体   繁体   English

Valgrind 给出错误 memory “仍然可达”

[英]Valgrind gives error memory "still reachable"

I am having the following error when I run Valgrind, I tried to free all the used functions, but still have the same error message运行 Valgrind 时出现以下错误,我试图释放所有使用的函数,但仍然有相同的错误消息

==303912== HEAP SUMMARY:
==303912==     in use at exit: 348 bytes in 2 blocks
==303912==   total heap usage: 1,192 allocs, 1,190 frees, 153,918 bytes allocated
==303912== 
==303912== 348 bytes in 2 blocks are still reachable in loss record 1 of 1
==303912==    at 0x483B7F3: malloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==303912==    by 0x490050E: strdup (strdup.c:42)
==303912==    by 0x109B8E: main (minishell.c:178)
==303912== 
==303912== LEAK SUMMARY:
==303912==    definitely lost: 0 bytes in 0 blocks
==303912==    indirectly lost: 0 bytes in 0 blocks
==303912==      possibly lost: 0 bytes in 0 blocks
**==303912==    still reachable: 348 bytes in 2 blocks**
==303912==         suppressed: 0 bytes in 0 blocks
==303912== 
==303912== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

Line 42 in valgrind error message refers to "signal_value = 1", not sure why?:: valgrind 错误消息中的第 42 行是指“signal_value = 1”,不知道为什么?::

sigjmp_buf jmpbuf;
volatile sig_atomic_t signal_value= false;

void catch_signal(int sign) {
    if (!signal_value) {
        write(STDOUT_FILENO, "\n", 1);
        siglongjmp(jmpbuf, 1);
        signal_value = 1;
    }
    else{
        signal_value = 0;
        printf("\n");
    }
}

and line 178 here is right at "argvals[num_tokens] = strdup(token);"第 178 行就在“argvals[num_tokens] = strdup(token);” the second part where it is referring to in valgrind它在 valgrind 中所指的第二部分

        while(token!=NULL) {
            argvals[num_tokens] = strdup(token);
            num_tokens++;
            token = strtok(NULL, " ");
        }
        if (num_tokens == 0) {
            continue;
        } if (strcmp(argvals[0], "exit") == 0) {
            for (int j = 0; j < num_tokens; j++) {
                        free((char*)argvals[j]);
            }
            return EXIT_SUCCESS;
        } 

The line of Valgrind output that should concern you is this specific one:你应该关心的 Valgrind output 的行是这个特定的:

==303912==    by 0x109B8E: main (minishell.c:178)

Other lines above it are referring to code inside library functions, which you do not have control over.它上面的其他行是指库函数中的代码,您无法控制这些代码。 They are just automatically shown by Valgrind as part of the stack trace.它们只是由 Valgrind 自动显示为堆栈跟踪的一部分。

Memory marked by Valgrind as "still reachable" is memory that is:由 Valgrind 标记为“仍然可达”的 Memory 是 memory 即:

  1. Allocated by your program at some point.在某个时候由您的程序分配。
  2. Not freed before the program exit.程序退出前未释放。
  3. Still pointed to by some variable at program exit (this is what "reachable" means).在程序退出时仍然被一些变量指向(这就是“可达”的意思)。

Valgrind tells you that your call to strdup() allocated some memory that you never freed, but this is not a real error or a problem. Valgrind 告诉您,您对strdup()的调用分配了一些您从未释放的 memory,但这不是真正的错误或问题。 You could in theory free() said memory just before returning from main , but it isn't really needed, as the operating system will clear the entire memory space of your program on exit.理论上,您可以在从main返回之前free()说 memory ,但这并不是真正需要的,因为操作系统将在退出时清除程序的整个 memory 空间。 If you need some memory allocated until the end of your program, you can avoid "wasting time" freeing it.如果您需要分配一些 memory 直到程序结束,您可以避免“浪费时间”释放它。 This is why Valgrind tells you about it, but does not actually report it as an error.这就是为什么 Valgrind 会告诉你它,但实际上并没有将它报告为错误。

If you instead don't need that allocated memory until the very end of your program, you should free it as soon as it is not needed anymore.如果您在程序结束之前不需要分配的 memory,那么您应该在不再需要它时立即释放它。 Valgrind can't really know whether this is the case though, it's your call as the programmer to understand it. Valgrind 不能真正知道这是否是这种情况,这是你作为程序员的电话来理解它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM