gpt4 book ai didi

c - 内存未释放但仍可访问,是否泄漏?

转载 作者:太空狗 更新时间:2023-10-29 12:41:32 25 4
gpt4 key购买 nike

通过使用 valgrind 检查,我发现在我的程序终止后有 5 个内存块没有被释放,但它们仍然可以访问。我需要被它打扰吗?

它是如何发生的?

zhanwu@gelata:~/sandbox$ valgrind ./a.out
==2430== Memcheck, a memory error detector
==2430== Copyright (C) 2002-2010, and GNU GPL'd, by Julian Seward et al.
==2430== Using Valgrind-3.6.1 and LibVEX; rerun with -h for copyright info
==2430== Command: ./a.out
==2430==
Hello world!
Thread1 returns 1
Thread2 returns 10
Thread3 returns 10
==2430==
==2430== HEAP SUMMARY:
==2430== in use at exit: 1,590 bytes in 5 blocks
==2430== total heap usage: 14 allocs, 9 frees, 2,442 bytes allocated
==2430==
==2430== LEAK SUMMARY:
==2430== definitely lost: 0 bytes in 0 blocks
==2430== indirectly lost: 0 bytes in 0 blocks
==2430== possibly lost: 0 bytes in 0 blocks
==2430== still reachable: 1,590 bytes in 5 blocks
==2430== suppressed: 0 bytes in 0 blocks
==2430== Rerun with --leak-check=full to see details of leaked memory
==2430==
==2430== For counts of detected and suppressed errors, rerun with: -v
==2430== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 4 from 4)

下面是我的代码,如果我想释放这 5 个 block ,我该怎么做?

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>

pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;

void* myfunction(void *ptr)
{
static int n_call = 0;
int *retval = malloc(sizeof(int));

pthread_mutex_lock( &mutex1 );
n_call++;
*retval = n_call;
pthread_mutex_unlock( &mutex1 );

if(n_call < 2)
{
char *msg;
msg = (char *)ptr;
printf("%s\n", msg);

return retval;
}
else
{
*retval = 10;
pthread_exit(retval);
}
}

int main(int argc, char *argv[])
{
pthread_t t1, t2, t3;

char *msg = "Hello world!";
pthread_create(&t1, NULL, myfunction, (void *)msg);
pthread_create(&t2, NULL, myfunction, (void *)msg);
pthread_create(&t3, NULL, myfunction, (void *)msg);

int **s1 = malloc(sizeof(int*));
int **s2 = malloc(sizeof(int*));
int **s3 = malloc(sizeof(int*));

pthread_join(t1, (void **)s1);
pthread_join(t2, (void **)s2);
pthread_join(t3, (void **)s3);

printf("Thread1 returns %d\n", **s1);
printf("Thread2 returns %d\n", **s2);
printf("Thread3 returns %d\n", **s3);

free(*s1);
free(*s2);
free(*s3);

free(s1);
free(s2);
free(s3);

return 0;
}

最佳答案

不,这不是内存泄漏。
这意味着您的程序仍然引用稍后将释放的内存。

Valgrind FAQ 区分不同的消息如下:

With Memcheck's memory leak detector, what's the difference between "definitely lost", "indirectly lost", "possibly lost", "still reachable", and "suppressed"?

The details are in the Memcheck section of the user manual.

In short:

  • definitely lost means your program is leaking memory -- fix those leaks!

  • indirectly lost means your program is leaking memory in a pointer-based structure. (E.g. if the root node of a binary tree is "definitely lost", all the children will be "indirectly lost".) If you fix the definitely lost leaks, the indirectly lost leaks should go away.

  • possibly lost means your program is leaking memory, unless you're doing funny things with pointers. This is sometimes reasonable.
    Use --show-possibly-lost=no if you don't want to see these reports.

  • still reachable means your program is probably ok -- it didn't free some memory it could have. This is quite common and often reasonable.
    Don't use --show-reachable=yes if you don't want to see these reports.

  • suppressed means that a leak error has been suppressed. There are some suppressions in the default suppression files. You can ignore suppressed errors.

关于c - 内存未释放但仍可访问,是否泄漏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41899237/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com