内存泄漏
Memory Leak Valgrind
我写了一个程序来将字符串五五分块。这是我的程序。
struct list
{
char *str;
struct list* next;
};
struct list* head = NULL;
void insert(char *cont)
{
struct list* temp = (struct list*)malloc(sizeof(struct list));
size_t len = strlen(cont);
char *heapString = (char*)malloc(len);
strcpy(heapString,cont);
temp->str = heapString;
temp->next = NULL;
if(head == NULL)
{
head = temp;
return ;
}
temp->next = head;
head = temp;
}
void print()
{
struct list* temp = head;
while(temp != NULL)
{
printf("%s\n",temp->str);
temp = temp->next;
}
}
void clearmem()
{
struct list* temp = head;
while(temp != NULL)
{
free(temp->str);
free(temp);
temp = temp->next;
}
}
int main()
{
char text[] = "abcdefghijklmno";
size_t len = strlen(text);
while(len !=0)
{
char *temp;
temp = text ;
temp = temp + len - 5;
insert(temp);
*(text+len-5) = '[=10=]';
len = strlen(text);
free(temp);
}
print();
clearmem();
}
我的程序运行良好。但是当我尝试通过 Valgrind 运行 这个程序时,我收到了以下消息。它说有 12 个错误。
==2055== Invalid write of size 1
==2055== at 0x4C32E0D: strcpy (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==2055== by 0x10888C: insert (in /home/infant/Documents/Sample_codes/a.out)
==2055== by 0x1089BD: main (in /home/infant/Documents/Sample_codes/a.out)
==2055== Address 0x522d095 is 0 bytes after a block of size 5 alloc'd
==2055== at 0x4C2FB0F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==2055== by 0x108875: insert (in /home/infant/Documents/Sample_codes/a.out)
==2055== by 0x1089BD: main (in /home/infant/Documents/Sample_codes/a.out)
==2055==
==2055== Invalid free() / delete / delete[] / realloc()
==2055== at 0x4C30D3B: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==2055== by 0x1089EB: main (in /home/infant/Documents/Sample_codes/a.out)
==2055== Address 0x1fff00030a is on thread 1's stack
==2055== in frame #1, created by main (???:)
==2055==
==2055== Invalid read of size 1
==2055== at 0x4C32D44: __strlen_sse2 (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==2055== by 0x4EBC9D1: puts (ioputs.c:35)
==2055== by 0x1088FC: print (in /home/infant/Documents/Sample_codes/a.out)
==2055== by 0x1089FC: main (in /home/infant/Documents/Sample_codes/a.out)
==2055== Address 0x522d1d5 is 0 bytes after a block of size 5 alloc'd
==2055== at 0x4C2FB0F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==2055== by 0x108875: insert (in /home/infant/Documents/Sample_codes/a.out)
==2055== by 0x1089BD: main (in /home/infant/Documents/Sample_codes/a.out)
==2055==
abcde
fghij
klmno
==2055== Invalid read of size 8
==2055== at 0x108947: clearmem (in /home/infant/Documents/Sample_codes/a.out)
==2055== by 0x108A06: main (in /home/infant/Documents/Sample_codes/a.out)
==2055== Address 0x522d188 is 8 bytes inside a block of size 16 free'd
==2055== at 0x4C30D3B: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==2055== by 0x108942: clearmem (in /home/infant/Documents/Sample_codes/a.out)
==2055== by 0x108A06: main (in /home/infant/Documents/Sample_codes/a.out)
==2055== Block was alloc'd at
==2055== at 0x4C2FB0F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==2055== by 0x108855: insert (in /home/infant/Documents/Sample_codes/a.out)
==2055== by 0x1089BD: main (in /home/infant/Documents/Sample_codes/a.out)
==2055==
==2055==
==2055== HEAP SUMMARY:
==2055== in use at exit: 0 bytes in 0 blocks
==2055== total heap usage: 7 allocs, 10 frees, 1,087 bytes allocated
==2055==
==2055== All heap blocks were freed -- no leaks are possible
==2055==
==2055== For counts of detected and suppressed errors, rerun with: -v
==2055== ERROR SUMMARY: 12 errors from 4 contexts (suppressed: 0 from 0)
即使我清除了堆中的所有内存,我仍然从 4 个上下文中收到 12 个错误。我这里的错误是什么?
循序渐进。
Invalid write of size 1
您的 malloc()
没有为字符串终止符分配 space,但 strcpy()
尝试写入它。使用
char *heapString = malloc(len + 1);
相反。 (注意:no need to cast void*
到 char*
!)。为简单起见,您也可以尝试使用(非标准)strdup(cont)
.
Invalid free() / delete / delete[] / realloc()
您的 temp
指向 text
中的 char
。使用 free()
没有任何意义,因为那里没有分配任何内容。删除该调用。
Invalid read of size 1
这应该与第一个错误有关。有趣的是,在 print()
中,编译时 printf("%s\n",temp->str)
是如何转换为(更快的)puts(temp->str)
。这就是为什么 Valgrind 抱怨调用 puts
.
Invalid read of size 8
在
free(temp);
temp = temp->next;
释放后您阅读temp
。
我写了一个程序来将字符串五五分块。这是我的程序。
struct list
{
char *str;
struct list* next;
};
struct list* head = NULL;
void insert(char *cont)
{
struct list* temp = (struct list*)malloc(sizeof(struct list));
size_t len = strlen(cont);
char *heapString = (char*)malloc(len);
strcpy(heapString,cont);
temp->str = heapString;
temp->next = NULL;
if(head == NULL)
{
head = temp;
return ;
}
temp->next = head;
head = temp;
}
void print()
{
struct list* temp = head;
while(temp != NULL)
{
printf("%s\n",temp->str);
temp = temp->next;
}
}
void clearmem()
{
struct list* temp = head;
while(temp != NULL)
{
free(temp->str);
free(temp);
temp = temp->next;
}
}
int main()
{
char text[] = "abcdefghijklmno";
size_t len = strlen(text);
while(len !=0)
{
char *temp;
temp = text ;
temp = temp + len - 5;
insert(temp);
*(text+len-5) = '[=10=]';
len = strlen(text);
free(temp);
}
print();
clearmem();
}
我的程序运行良好。但是当我尝试通过 Valgrind 运行 这个程序时,我收到了以下消息。它说有 12 个错误。
==2055== Invalid write of size 1
==2055== at 0x4C32E0D: strcpy (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==2055== by 0x10888C: insert (in /home/infant/Documents/Sample_codes/a.out)
==2055== by 0x1089BD: main (in /home/infant/Documents/Sample_codes/a.out)
==2055== Address 0x522d095 is 0 bytes after a block of size 5 alloc'd
==2055== at 0x4C2FB0F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==2055== by 0x108875: insert (in /home/infant/Documents/Sample_codes/a.out)
==2055== by 0x1089BD: main (in /home/infant/Documents/Sample_codes/a.out)
==2055==
==2055== Invalid free() / delete / delete[] / realloc()
==2055== at 0x4C30D3B: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==2055== by 0x1089EB: main (in /home/infant/Documents/Sample_codes/a.out)
==2055== Address 0x1fff00030a is on thread 1's stack
==2055== in frame #1, created by main (???:)
==2055==
==2055== Invalid read of size 1
==2055== at 0x4C32D44: __strlen_sse2 (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==2055== by 0x4EBC9D1: puts (ioputs.c:35)
==2055== by 0x1088FC: print (in /home/infant/Documents/Sample_codes/a.out)
==2055== by 0x1089FC: main (in /home/infant/Documents/Sample_codes/a.out)
==2055== Address 0x522d1d5 is 0 bytes after a block of size 5 alloc'd
==2055== at 0x4C2FB0F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==2055== by 0x108875: insert (in /home/infant/Documents/Sample_codes/a.out)
==2055== by 0x1089BD: main (in /home/infant/Documents/Sample_codes/a.out)
==2055==
abcde
fghij
klmno
==2055== Invalid read of size 8
==2055== at 0x108947: clearmem (in /home/infant/Documents/Sample_codes/a.out)
==2055== by 0x108A06: main (in /home/infant/Documents/Sample_codes/a.out)
==2055== Address 0x522d188 is 8 bytes inside a block of size 16 free'd
==2055== at 0x4C30D3B: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==2055== by 0x108942: clearmem (in /home/infant/Documents/Sample_codes/a.out)
==2055== by 0x108A06: main (in /home/infant/Documents/Sample_codes/a.out)
==2055== Block was alloc'd at
==2055== at 0x4C2FB0F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==2055== by 0x108855: insert (in /home/infant/Documents/Sample_codes/a.out)
==2055== by 0x1089BD: main (in /home/infant/Documents/Sample_codes/a.out)
==2055==
==2055==
==2055== HEAP SUMMARY:
==2055== in use at exit: 0 bytes in 0 blocks
==2055== total heap usage: 7 allocs, 10 frees, 1,087 bytes allocated
==2055==
==2055== All heap blocks were freed -- no leaks are possible
==2055==
==2055== For counts of detected and suppressed errors, rerun with: -v
==2055== ERROR SUMMARY: 12 errors from 4 contexts (suppressed: 0 from 0)
即使我清除了堆中的所有内存,我仍然从 4 个上下文中收到 12 个错误。我这里的错误是什么?
循序渐进。
Invalid write of size 1
您的 malloc()
没有为字符串终止符分配 space,但 strcpy()
尝试写入它。使用
char *heapString = malloc(len + 1);
相反。 (注意:no need to cast void*
到 char*
!)。为简单起见,您也可以尝试使用(非标准)strdup(cont)
.
Invalid free() / delete / delete[] / realloc()
您的 temp
指向 text
中的 char
。使用 free()
没有任何意义,因为那里没有分配任何内容。删除该调用。
Invalid read of size 1
这应该与第一个错误有关。有趣的是,在 print()
中,编译时 printf("%s\n",temp->str)
是如何转换为(更快的)puts(temp->str)
。这就是为什么 Valgrind 抱怨调用 puts
.
Invalid read of size 8
在
free(temp);
temp = temp->next;
释放后您阅读temp
。