我正在创建一个服务器,每次客户端连接到服务器时都会创建一个分离线程。

    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.
===============================================================

I keep getting the same leak summary no matter how many threads are created.since the file the leak is in is a system file and not one of my own, i have implemented something in correctly. What could it be ?

I've run the same file in valgrind on another linux server now i'm getting this:

==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调用。我的create通话有问题吗?

最佳答案

我认为您无需担心。泄漏摘要说:

LEAK SUMMARY:
definitely lost: 0 bytes in 0 blocks.
possibly lost: 0 bytes in 0 blocks.
still reachable: 16 bytes in 1 blocks.


并且“仍然可到达”的内存并没有被确定地丢失,而且正如您所确定的,它似乎已存在于系统代码中,并且不会随着线程数的增加而增加,所有这些加起来在我的书中“无须担心” 。

除非内存量急剧增加,或者您无法确定代码可能是在泄漏内存,否则将您的时间花在其他问题上,而不是花在这个问题上。

关于c - 使用C中的多个分离线程的内存泄漏,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8291917/

10-11 23:15
查看更多