我正在创建一个服务器,每次客户端连接到服务器时都会创建一个分离的线程。
TRACE(DETAILED_TRACE,("Entered infinite loop of server.\n"));
printf("\nThread counter = %d\n", thread_counter);
printf("Waiting for connection...\n");
len=sizeof(cliaddr);
connfd=accept(sd,(struct sockaddr*)&cliaddr,&len);
if (connfd < 0)
{
if (errno == EINTR)
printf("Interrupted system call ??");
else
error_exit(SYSTEM_ERROR, "Connection");
}
if(FLAG_UNSET == server_stop_flag)
{
printf("Connection from %s\n",
inet_ntop(AF_INET,&cliaddr.sin_addr,buf,sizeof(buf)));
thread_return = pthread_create((th+thread_counter), NULL
,thread_func,(void*)&connfd);
if(thread_return)
{
error_exit(SYSTEM_ERROR, "Thread Creation");
}
else
{
thread_return = pthread_detach(th[thread_counter
]);
if(thread_return)
{
printf("\nError code: %d\n", thread_retu
rn);
error_exit(SYSTEM_ERROR, "Detatch error"
);
}
}
thread_counter++;
thread_counter = thread_counter%MAX_THREADS;
在运行valgrind时,我一直这样做:
=================================================================== 16 bytes in 1 blocks are still reachable in loss record 1 of 2 at 0x1B905301: calloc (vg_replace_malloc.c:176) by 0x9E7364: _dlerror_run (in /lib/libdl-2.3.4.so) by 0x9E6E3B: dlsym (in /lib/libdl-2.3.4.so) by 0x1B9106EE: open (vg_libpthread.c:2339) LEAK SUMMARY: definitely lost: 0 bytes in 0 blocks. possibly lost: 0 bytes in 0 blocks. still reachable: 16 bytes in 1 blocks. ===============================================================
无论创建多少个线程,我都会得到相同的泄漏摘要。 因为泄漏所在的文件是一个系统文件,而不是我自己的文件,我已经正确实现了一些东西。它可能是什么?
我在另一个Linux服务器上的valgrind中运行相同的文件,现在我得到了这个:
==12599== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 4 from 1)
==12599== malloc/free: in use at exit: 136 bytes in 1 blocks.
==12599== malloc/free: 6 allocs, 5 frees, 184 bytes allocated.
==12599== For counts of detected errors, rerun with: -v
==12599== searching for pointers to 1 not-freed blocks.
==12599== checked 10,580,680 bytes.
==12599==
==12599== 136 bytes in 1 blocks are possibly lost in loss record 1 of 1
==12599== at 0x4905D27: calloc (vg_replace_malloc.c:279)
==12599== by 0x358500D332: _dl_allocate_tls (in /lib64/ld-2.3.4.so)
==12599== by 0x3585F066EE: pthread_create@@GLIBC_2.2.5 (in /lib64/tls/libpthr
ead-2.3.4.so)
==12599== by 0x401222: main (test36.c:81)
==12599==
==12599== LEAK SUMMARY:
==12599== definitely lost: 0 bytes in 0 blocks.
==12599== possibly lost: 136 bytes in 1 blocks.
==12599== still reachable: 0 bytes in 0 blocks.
==12599== suppressed: 0 bytes in 0 blocks.
这是服务器的问题吗? 仅供参考:行号test.c中的81是pthread_create调用。我的创建电话有问题吗?
答案 0 :(得分:2)
我认为你没有什么可担心的。泄漏摘要说:
LEAK SUMMARY:
definitely lost: 0 bytes in 0 blocks.
possibly lost: 0 bytes in 0 blocks.
still reachable: 16 bytes in 1 blocks.
和'仍然可以访问'内存并没有明确丢失,并且它似乎在您识别的系统代码中,并且它不会随着线程数量的增加而增加,所有这些都加起来“无需担心”在我的书中。
除非内存量急剧增加,或者您可以确定代码可能泄漏内存的方式,否则请花时间处理其他问题而不是这个问题。
答案 1 :(得分:2)
当我看到这个时,有些东西引起了我的注意:
thread_return = pthread_create((th+thread_counter), NULL
,thread_func,(void*)&connfd);
它将connfd
的地址传递给另一个线程。如何分配connfd
?它不是局部变量,是吗?
答案 2 :(得分:-3)
这只是意味着在结束程序之前没有释放所有已分配的内存。释放所有的alloc指针,这个警告应该消失。