Valgrind 和全局变量

发布于 2024-10-13 16:02:50 字数 1487 浏览 4 评论 0原文

我正在运行 valgrind 来查找内存泄漏。我在主函数中分配了两个全局变量;然后,在 main 的末尾我释放了这两个变量,但 Valgrind 继续写道:

==18311== 16 bytes in 1 blocks are definitely lost in loss record 1 of 2
==18311==    at 0x4025BD3: malloc (vg_replace_malloc.c:236)
==18311==    by 0x804A30C: main (application.c:730)
==18311== 
==18311== 16 bytes in 1 blocks are definitely lost in loss record 2 of 2
==18311==    at 0x4025BD3: malloc (vg_replace_malloc.c:236)
==18311==    by 0x804A31D: main (application.c:731)

泄漏摘要:

==18311== LEAK SUMMARY:
==18311==    definitely lost: 32 bytes in 2 blocks
==18311==    indirectly lost: 0 bytes in 0 blocks
==18311==      possibly lost: 0 bytes in 0 blocks
==18311==    still reachable: 0 bytes in 0 blocks
==18311==         suppressed: 0 bytes in 0 blocks

为什么我无法释放这两个变量?

Edit

someList *something; *something_else;

使用的结构有两个 char * 类型的字段和一个 someList *next 类型的字段。 后来就有很多代码了。一些线程将使用这两个变量添加/编辑/删除对象。

something -> object 1 -> ... -> object n
something_else -> object 1 -> ... -> object m

其中->表示something->next = object 1object k都是someList *.

在应用程序接近结束时,我释放了每个对象 k 元素的每个字段。然后,在最后一部分中:

free(something);
free(something_else);

我可能忘记释放对象的字段。这会导致我在这里的行为吗?

我希望现在更清楚了。

I'm running valgrind to look for memory leaks. I have alloc'd two global variables in the main function; then, at the end of main I free both, but Valgrind keeps writing:

==18311== 16 bytes in 1 blocks are definitely lost in loss record 1 of 2
==18311==    at 0x4025BD3: malloc (vg_replace_malloc.c:236)
==18311==    by 0x804A30C: main (application.c:730)
==18311== 
==18311== 16 bytes in 1 blocks are definitely lost in loss record 2 of 2
==18311==    at 0x4025BD3: malloc (vg_replace_malloc.c:236)
==18311==    by 0x804A31D: main (application.c:731)

Leak summary:

==18311== LEAK SUMMARY:
==18311==    definitely lost: 32 bytes in 2 blocks
==18311==    indirectly lost: 0 bytes in 0 blocks
==18311==      possibly lost: 0 bytes in 0 blocks
==18311==    still reachable: 0 bytes in 0 blocks
==18311==         suppressed: 0 bytes in 0 blocks

Why I can't free these two variables?

Edit

someList *something; *something_else;

The struct used has two fields of type char * and a field someList *next.
Later there's a lot of code. A few threads are going to add/edit/delete objects using this two variables.

something -> object 1 -> ... -> object n
something_else -> object 1 -> ... -> object m

Where -> means that something->next = object 1, and object k are all instances of someList *.

Near the end of the application, I freed every field of each object k element. Then, in the last part:

free(something);
free(something_else);

It's possible that I forgot to free a field of an object. Can this cause the behavior I'm having here?

I hope it's clearer, now.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

带上头具痛哭 2024-10-20 16:02:50

您确定要释放所有变量吗?瓦尔格林德说你不是。

尝试使您的代码精简并将其发布到此处,或者进行更多调试。

Are you sure you're deallocating all of your variables? Valgrind says you're not.

Try and make your code slim and post it here, or run some more debugging.

一腔孤↑勇 2024-10-20 16:02:50

对于每个 malloc() 调用,您通常需要相应的 free() 调用,以避免内存泄漏。为了获得最佳的内存使用率,您应该在使用完每个分配的内存块后尽快free()

在您的情况下,您在 application.c:730application.c:731 中有两个 malloc() 调用,对此没有程序终止时进行的 free() 调用。因此,在那里分配的内存块没有被释放。这会导致 Valgrind 检测并报告内存泄漏。

除非您向我们提供有关您的程序的一些代码和/或更多信息,否则我们无法为您提供更多帮助。

编辑:

您的 Valgrind 输出中有两行有趣的行:

==18311==    definitely lost: 32 bytes in 2 blocks
...
==18311==    still reachable: 0 bytes in 0 blocks

您肯定“丢失”了两个内存块,并且在程序终止时没有指向它们的指针。这意味着在程序运行过程中,指向这两个块的指针实际上被覆盖或丢失了它们的值。

此问题的一个常见原因是将两次 malloc() 调用的结果存储到同一指针,而没有中间 free() 调用。另一个可能的原因是指针算术运算导致指针值发生变化 - Valgrind 努力检测其中一些情况,但并不总是成功,具体取决于您的代码。

For each malloc() call you typically need a corresponding free() call, in order to avoid memory leaks. For optimal memory usage, you should free() each allocated memory block as soon as possible after being done with it.

In your case, you have two malloc() calls in application.c:730 and application.c:731, for which there is no free() call made by the time your program terminates. Therefore, the memory blocks allocated there are not freed. This causes a memory leak that Valgrind detects and reports.

Unless you provide us with some code and/or more information on your program there is no way for us to help you more.

EDIT:

There are two interesting lines in your Valgrind output:

==18311==    definitely lost: 32 bytes in 2 blocks
...
==18311==    still reachable: 0 bytes in 0 blocks

You have definitely "lost" two memory blocks and there no pointers to them at the time your program terminates. This means that the pointers to these two blocks were in fact overwritten or otherwise lost their value during the course of your program.

A common cause of this issue is storing the result of two malloc() calls to the same pointer without an intermediate free() call. Another possible cause is a changed pointer value due to pointer arithmetic operations - Valgrind makes an effort to detect some of these cases, but it is not always successful, depending on your code.

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