c - 我不是在某处释放内存吗?

标签 c memory-leaks valgrind

我有一个程序,它从一个文件中读取并将项目存储在队列中,然后将它们打印出来。我从 valgrind 得到这个:

 HEAP SUMMARY:
     in use at exit: 302 bytes in 14 blocks
   total heap usage: 30 allocs, 16 frees, 1,230 bytes allocated

 302 bytes in 14 blocks are definitely lost in loss record 1 of 1
    at 0x4A05FDE: malloc (vg_replace_malloc.c:236)
    by 0x372167FB41: strdup (strdup.c:43)
    by 0x400A6A: addtoqueue(main.c:30)
    by 0x400B5A: addfiletoqueue(main.c:45)
    by 0x400C27: main (main.c:62)

 LEAK SUMMARY:
    definitely lost: 302 bytes in 14 blocks
    indirectly lost: 0 bytes in 0 blocks
      possibly lost: 0 bytes in 0 blocks
    still reachable: 0 bytes in 0 blocks
         suppressed: 0 bytes in 0 blocks

main.c的第62行

addfiletoqueue (queue, argv[argi]);

main.c的第45行

addtoqueue (queue, file, filename);

main.c 的第 30 行

readline = strdup (buffer);
assert (readline != NULL);
queue_add (queue, readline);

我需要免费阅读线吗?

最佳答案

是的,strdup()动态分配您必须 free() 的内存。从链接的引用页面:

The strdup() function returns a pointer to a new string which is a duplicate of the string s. Memory for the new string is obtained with malloc(3), and can be freed with free(3).

关于c - 我不是在某处释放内存吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15063871/

相关文章:

c - CMSG_ALIGN 宏的值是多少

c套接字: send() send only 1 byte at a time

Hadoop:运行超出虚拟内存限制,显示大量数据

c - 尝试使用 valgrind 的输出调试 c 程序段错误

c - 使用 Libgcrypt 的程序中的内存泄漏

java - Valgrind 检测 Java Web 应用程序中的内存泄漏

c - GCC 的 itoa 函数

c - 为什么这段代码不能正确压缩空格?

Delphi 7.0和内存泄漏?

memory-leaks - 在 flutter 中预加载图像 Assets