【发布时间】:2015-02-21 08:46:55
【问题描述】:
这更像是一个一般性的查询,如果产生的一些线程没有正常终止,valgrind memcheck 的行为会是什么。我的意思是线程分离并且不与父进程/线程共享任何内存的情况。
我没有任何代码 sn-p 或 valgrind 输出,因为这是一个出于好奇而提出的问题
【问题讨论】:
标签: c linux memory memory-leaks valgrind
这更像是一个一般性的查询,如果产生的一些线程没有正常终止,valgrind memcheck 的行为会是什么。我的意思是线程分离并且不与父进程/线程共享任何内存的情况。
我没有任何代码 sn-p 或 valgrind 输出,因为这是一个出于好奇而提出的问题
【问题讨论】:
标签: c linux memory memory-leaks valgrind
对于这段代码:
while (1) {
pthread_t thread;
struct sockaddr_in client;
socklen_t len = sizeof(client);
int newsock = accept(sock, (struct sockaddr *)&client, &len);
if (newsock == -1) {
perror("accept");
} else {
if (pthread_create(&thread, NULL, handle, &newsock) != 0) {
perror("pthread_create");
} else {
pthread_detach(thread);
}
}
if (count == 5) break;
}
close(sock);
exit(EXIT_SUCCESS);
如果一个线程仍在等待accept(),则显示可能丢失:
david@debian:~$ valgrind ./server
==24561== Memcheck, a memory error detector
==24561== Copyright (C) 2002-2011, and GNU GPL'd, by Julian Seward et al.
==24561== Using Valgrind-3.7.0 and LibVEX; rerun with -h for copyright info
==24561== Command: ./server
==24561==
0
1
2
3
4
5
==24561==
==24561== HEAP SUMMARY:
==24561== in use at exit: 272 bytes in 1 blocks
==24561== total heap usage: 3 allocs, 2 frees, 1,408 bytes allocated
==24561==
==24561== LEAK SUMMARY:
==24561== definitely lost: 0 bytes in 0 blocks
==24561== indirectly lost: 0 bytes in 0 blocks
==24561== possibly lost: 272 bytes in 1 blocks
==24561== still reachable: 0 bytes in 0 blocks
==24561== suppressed: 0 bytes in 0 blocks
==24561== Rerun with --leak-check=full to see details of leaked memory
==24561==
==24561== For counts of detected and suppressed errors, rerun with: -v
==24561== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 4 from 4)
【讨论】: