代码之家  ›  专栏  ›  技术社区  ›  HessianMad

在C中未正确释放线程

  •  2
  • HessianMad  · 技术社区  · 7 年前

    threads 我永远不知道他们什么时候结束,所以我用 pthread_detach()

    代码如下:

    pthread_t thread_id_Gruder, thread_id_Tavish;
    
    estado_threadGruder = pthread_create(&thread_id_Gruder,NULL,SERVER_McGruderDedicado,&td_Gruder);
    
    estado_threadTavish = pthread_create(&thread_id_Tavish,NULL,SERVER_McTavishDedicado,&td_Tavish);
    
    if (estado_threadGruder != 0 || estado_threadTavish != 0) {
                if (estado_threadGruder != 0) MSSG_error(THREAD_ERROR, "McGruder");
                else MSSG_error(THREAD_ERROR, "McTavish");
                raise(SIGINT);
                pause();
    }
    pthread_detach(thread_id_Gruder);
    pthread_detach(thread_id_Tavish);
    

    valgrind 为了检查内存泄漏,我发现以下输出:

    HEAP SUMMARY:
    ==19426==     in use at exit: 544 bytes in 2 blocks
    ==19426==   total heap usage: 15 allocs, 13 frees, 628 bytes allocated
    ==19426== 
    ==19426== Thread 1:
    ==19426== 272 bytes in 1 blocks are possibly lost in loss record 1 of 2
    ==19426==    at 0x4C31B25: calloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
    ==19426==    by 0x40134A6: allocate_dtv (dl-tls.c:286)
    ==19426==    by 0x40134A6: _dl_allocate_tls (dl-tls.c:530)
    ==19426==    by 0x4E44227: allocate_stack (allocatestack.c:627)
    ==19426==    by 0x4E44227: pthread_create@@GLIBC_2.2.5 (pthread_create.c:644)
    ==19426==    by 0x1097AA: main (in /users/home/alumnes/LS//PRACTICA/Lionel-S/Lionel)
    ==19426== 
    ==19426== 272 bytes in 1 blocks are possibly lost in loss record 2 of 2
    ==19426==    at 0x4C31B25: calloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
    ==19426==    by 0x40134A6: allocate_dtv (dl-tls.c:286)
    ==19426==    by 0x40134A6: _dl_allocate_tls (dl-tls.c:530)
    ==19426==    by 0x4E44227: allocate_stack (allocatestack.c:627)
    ==19426==    by 0x4E44227: pthread_create@@GLIBC_2.2.5 (pthread_create.c:644)
    ==19426==    by 0x1097CC: main (in /users/home/alumnes/LS//PRACTICA/Lionel-S/Lionel)
    ==19426== 
    ==19426== LEAK SUMMARY:
    ==19426==    definitely lost: 0 bytes in 0 blocks
    ==19426==    indirectly lost: 0 bytes in 0 blocks
    ==19426==      possibly lost: 544 bytes in 2 blocks
    ==19426==    still reachable: 0 bytes in 0 blocks
    ==19426==         suppressed: 0 bytes in 0 blocks
    ==19426== 
    ==19426== For counts of detected and suppressed errors, rerun with: -v
    ==19426== ERROR SUMMARY: 3 errors from 3 contexts (suppressed: 0 from 0)
    

    我试着在没有代码的情况下运行代码 phtread_detach() 功能但是 瓦尔格林 显示相同的内存泄漏,所以我假设我没有正确分离。。。

    谢谢

    1 回复  |  直到 7 年前
        1
  •  5
  •   Arthur Moraes Do Lago    7 年前

    问题很可能是你的线程。即使你没有用过 malloc 在他们里面。

    pthread_join . 然而,在某些情况下,您的代码中没有任何一点需要同步线程,而只是希望它们在同步完成后消失。这就是为什么会有 pthread_detach returns . 注意,当他们 返回 .

    如果程序完成时线程尚未完成,则主函数将正常结束,但线程仍在运行。因为他们还没有回来,所以还没有清理干净。

    如果您打算让这些线程一直运行到程序完成,或者希望在程序结束时等待它们不完成,那么您确实有一点需要同步它们(程序结束),pthread_detach可能不是方法。

    如果线程有无限循环,我建议创建一个变量 should_Terminate pthread_连接 等待线程优雅地结束工作。

    如果它们没有无限循环,并且一定会在某个点返回,那么在程序端加入它们就足够了。