valgrind 报告未释放的块

发布于 2024-11-28 16:41:43 字数 549 浏览 2 评论 0原文

Valgrind 泄漏文件摘要:

ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
malloc/free: in use at exit: 45,065 bytes in 12 blocks.
malloc/free: 161 allocs, 149 frees, 53,301 bytes allocated.
searching for pointers to 12 not-freed blocks.
checked 583,764 bytes.

这 12 个块之一来自 strdup。我同意,我应该释放 strdup 分配的东西。

我的问题是,一般来说,留下未释放的块是否不好?从技术上讲它被称为mem-leak吗?

一旦程序终止,它们不会返回给系统吗?

请指教。

编辑0:感谢您的回复。我怎么知道这 12 个未释放的块在哪里?代码的哪一部分生成它们?

Valgrind leak file summary:

ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
malloc/free: in use at exit: 45,065 bytes in 12 blocks.
malloc/free: 161 allocs, 149 frees, 53,301 bytes allocated.
searching for pointers to 12 not-freed blocks.
checked 583,764 bytes.

One of this 12 blocks is from strdup. I should have freed things allocated by strdup, I agree.

My question is, in general, is it bad to leave non-freed blocks? Is it called mem-leak technically?

Are they not given back to the system once program dies?

Please advise.

Edit 0: Thanks for your responses. How can I know where are these 12 non-freed blocks? And what part of code is generating them?

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

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

发布评论

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

评论(2

雨巷深深 2024-12-05 16:41:43

它被返回给系统。

如果您有对内存的引用,那么从技术上讲,这并不是内存泄漏。要发生内存泄漏,您必须取消引用内存。

void *str = malloc(10);
str = NULL;

任何时候留下未释放的块都是不好的。如果程序正在完成它,它可能并没有那么糟糕,但对于您将来可能做的任何更改都不利(例如:提取一个函数并多次调用它)。

此外,消除所有内存泄漏将使使用 valgrind 更容易跟踪任何新的(和相关的)内存泄漏。

It is given back to the system.

It it not technically a memory leak if you have a reference to the memory. To be a memory leak you must de-reference the memory.

void *str = malloc(10);
str = NULL;

It is bad to leave non-freed blocks at any point. If the program is finishing it, it might not be that bad, but it is not good for any future change you might do (e.g.: extract a function and call it multiple times).

Also, getting rid of all memory leaks will make it easier to track with valgrind any new (and relevant) one.

满地尘埃落定 2024-12-05 16:41:43

是的,留下未释放的块是不好的。这称为内存泄漏。如果您允许它,您的程序最终将使用系统中的所有可用内存。
程序终止后,程序分配的内存将被释放。

Yes, it is bad to leave non-freed blocks. It's called memory-leak. If you let it your program will eventually use all available memory in your system.
After program dies memory allocated by your program is freed.

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