【问题标题】:Valgrind leak of memory in a function that concatenates strings连接字符串的函数中的 Valgrind 内存泄漏
【发布时间】:2020-08-25 05:39:08
【问题描述】:

我使用 valgrind 来查找内存泄漏。我写了一个函数“prefix_to_string”来连接任意两个字符串,但是当我使用命令时

valgrind --leak-check=full ./helloworld

它说我有很多内存泄漏。我真的不知道在哪里以及为什么。我问一个朋友为什么会这样,他说这是为了做 malloc 2 次,1 次在函数中,1 次在函数中,但我不知道如何处理泄漏,因为我认为我需要做那些内存请求。 这是 Valgrind 给我的输出:

==9078== Memcheck, a memory error detector
==9078== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==9078== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info
==9078== Command: ./holamundo
==9078==
==9078== error calling PR_SET_PTRACER, vgdb might block
150:62
bye
==9078==
==9078== HEAP SUMMARY:
==9078==     in use at exit: 63 bytes in 4 blocks
==9078==   total heap usage: 5 allocs, 1 frees, 575 bytes allocated
==9078==
==9078== 5 bytes in 1 blocks are definitely lost in loss record 1 of 4
==9078==    at 0x4C2DB8F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==9078==    by 0x400740: prefix_to_string (in /mnt/c/Users/MrRaul/desktop/Tareas_edd/test_en_C/test_informales/holamundo)
==9078==    by 0x4008AC: main (in /mnt/c/Users/MrRaul/desktop/Tareas_edd/test_en_C/test_informales/holamundo)
==9078==
==9078== 7 bytes in 1 blocks are definitely lost in loss record 2 of 4
==9078==    at 0x4C2DB8F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==9078==    by 0x400740: prefix_to_string (in /mnt/c/Users/MrRaul/desktop/Tareas_edd/test_en_C/test_informales/holamundo)
==9078==    by 0x4008C3: main (in /mnt/c/Users/MrRaul/desktop/Tareas_edd/test_en_C/test_informales/holamundo)
==9078==
==9078== 8 bytes in 1 blocks are definitely lost in loss record 3 of 4
==9078==    at 0x4C2DB8F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==9078==    by 0x400740: prefix_to_string (in /mnt/c/Users/MrRaul/desktop/Tareas_edd/test_en_C/test_informales/holamundo)
==9078==    by 0x4008D8: main (in /mnt/c/Users/MrRaul/desktop/Tareas_edd/test_en_C/test_informales/holamundo)
==9078==
==9078== 43 bytes in 1 blocks are definitely lost in loss record 4 of 4
==9078==    at 0x4C2DB8F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==9078==    by 0x400897: main (in /mnt/c/Users/MrRaul/desktop/Tareas_edd/test_en_C/test_informales/holamundo)
==9078==
==9078== LEAK SUMMARY:
==9078==    definitely lost: 63 bytes in 4 blocks
==9078==    indirectly lost: 0 bytes in 0 blocks
==9078==      possibly lost: 0 bytes in 0 blocks
==9078==    still reachable: 0 bytes in 0 blocks
==9078==         suppressed: 0 bytes in 0 blocks
==9078==
==9078== For counts of detected and suppressed errors, rerun with: -v
==9078== ERROR SUMMARY: 4 errors from 4 contexts (suppressed: 0 from 0)

这是我的主要代码,这样你就可以重现问题:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <unistd.h>
#include <string.h>
#include <math.h>

char* prefix_to_string(char* first_string,char* second_string){
    char* name = first_string;
    char* extension = second_string;    
    char* name_with_extension;
    name_with_extension = malloc(strlen(name)*(sizeof(char))+strlen(extension)*(sizeof(char))+1); /* make space for the new string (should check the return value ...) */
    strcpy(name_with_extension, name); /* copy name into the new var */
    strcat(name_with_extension, extension); /* add the extension */
    return name_with_extension;
}

static char *itoa_simple_helper(char *dest, int i) {
  if (i <= -10) {
    dest = itoa_simple_helper(dest, i/10);
  }
  *dest++ = '0' - i%10;
  return dest;
}

char *itoa_simple(char *dest, int i) {
  char *s = dest;
  if (i < 0) {
    *s++ = '-';
  } else {
    i = -i;
  }
  *itoa_simple_helper(s, i) = '\0';
  return dest;
}

int main(int argc, char *argv[]) {

int idx = 150;
int id = 62;
char str_idx[20];
char str_id[20];
itoa_simple( str_idx ,idx);
itoa_simple( str_id,id);
char *text_to_write;
text_to_write = malloc(2+sizeof(str_id)+sizeof(str_idx)+1);
text_to_write = prefix_to_string(str_idx,":");
text_to_write = prefix_to_string(text_to_write,str_id);
text_to_write = prefix_to_string(text_to_write,"\n");

printf("%s",text_to_write);
printf("bye\n");
free(text_to_write);




return 1;
}

【问题讨论】:

  • 我知道我需要在 text_to_write 之后执行 1 但这就是我所知道的全部
  • 如果您在 prefix_to_string 中使用 malloc char*,则不需要在 main 中使用 malloc text_to_write,因为它会被 prefix_to_string 的输出覆盖。因此,必须在 main 中解释您对 malloc 的担忧。
  • oooo okok 我不知道,但即便如此,它仍然给我一个泄漏,更少,但仍然存在

标签: c string memory-leaks valgrind


【解决方案1】:

你调用free() 的次数不够多——如果你不调用free() 来释放每个单独的内存分配,你就不能期望避免内存泄漏。而您在main() 中对text_to_write 的重复分配意味着您丢弃了指向某些已分配内存的唯一指针,因此您无法释放已分配的内存。 C 需要对内存管理无休止的关注。

你有:

char *text_to_write;
text_to_write = malloc(2+sizeof(str_id)+sizeof(str_idx)+1);
// This assignment throws away the memory from the previous malloc
text_to_write = prefix_to_string(str_idx,":");
// This assignment throws away the memory from the previous prefix_to_string
text_to_write = prefix_to_string(text_to_write,str_id);
// This assignment also throws away the memory from the previous prefix_to_string
text_to_write = prefix_to_string(text_to_write,"\n");

printf("%s",text_to_write);
printf("bye\n");
// Calling free here only releases the last allocation from prefix_to_string
free(text_to_write);

你需要更多类似的东西:

char *part1 = prefix_to_string(str_idx, ":");
char *part2 = prefix_to_string(part1, str_id);
char *part3 = prefix_to_string(part2, "\n");

printf("%s", part3);
printf("bye\n");

free(part1);
free(part2);
free(part3);

【讨论】:

    猜你喜欢
    • 2021-01-07
    • 1970-01-01
    • 1970-01-01
    • 2020-11-09
    • 2013-06-24
    • 2013-01-12
    • 2021-11-28
    • 2017-04-23
    • 2020-03-31
    相关资源
    最近更新 更多