2011-04-10 102 views
14

我正在使用pthread庫編寫程序。當我用命令的valgrind --leak檢查運行我的程序=完全,我得到下面的錯誤描述:使用pthread_create時valgrind內存泄漏錯誤

==11784== 
==11784== **HEAP SUMMARY:** 
==11784==  in use at exit: 4,952 bytes in 18 blocks 
==11784== total heap usage: 1,059 allocs, 1,041 frees, 51,864 bytes allocated 
==11784== 
==11784== **288 bytes** in 1 blocks are possibly lost in loss record 2 of 3 
==11784== at 0x4C2380C: calloc (vg_replace_malloc.c:467) 
==11784== by 0x4010D2E: _dl_allocate_tls (dl-tls.c:300) 
==11784== by 0x55DC218: **pthread_create**@@GLIBC_2.2.5 (allocatestack.c:570) 
==11784== by 0x401BC0: initdevice(char*) (in /a/fr-01/vol/home/stud/lim/workspace /Ex3/l) 
==11784== by 0x406D05: main (in /a/fr-01/vol/home/stud/lim/workspace/Ex3/l) 
==11784== 
==11784== **4,608 bytes** in 16 blocks are possibly lost in loss record 3 of 3 
==11784== at 0x4C2380C: calloc (vg_replace_malloc.c:467) 
==11784== by 0x4010D2E: _dl_allocate_tls (dl-tls.c:300) 
==11784== by 0x55DC218: **pthread_create**@@GLIBC_2.2.5 (allocatestack.c:570)  
==11784== by 0x40268F: write2device(char*, int) (in /a/fr-01/vol/home/stud/lim/workspace/Ex3/l) 
==11784== by 0x406D7B: main (in /a/fr-01/vol/home/stud/lim/workspace/Ex3/l) 
==11784== 
==11784== **LEAK SUMMARY:** 
==11784== definitely lost: 0 bytes in 0 blocks 
==11784== indirectly lost: 0 bytes in 0 blocks 
==11784==  possibly lost: 4,896 bytes in 17 blocks 
==11784== still reachable: 56 bytes in 1 blocks 
==11784==   suppressed: 0 bytes in 0 blocks 
==11784== Reachable blocks (those to which a pointer was found) are not shown. 
==11784== To see them, rerun with: --leak-check=full --show-reachable=yes 
==11784== 
==11784== For counts of detected and suppressed errors, rerun with: -v 
==11784== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 4 from 4) 

每次我調用pthread_create,具有一定的功能 - 我所說的功能了pthread_exit在函數的最後。所以,經過驗證,這不是問題,可能是什麼問題?

+2

那麼你是否試圖編寫一個只執行pthread_create/pthread_exit(+ join)並在valgrind下運行的程序? – 2011-04-10 08:53:45

+1

嘗試使用「--leak-check = full --show-reachable = yes」運行它。猜測是重複的結構可能不會被刪除。但沒有代碼來自己測試,我不能說太多。 – 2011-04-10 08:54:23

+0

你是否加入了使用'pthread_join'的線程?否則會出現泄漏。 – Hasturkun 2011-04-10 09:52:42

回答

2

OT可以使螺紋in detached state,以避免內存泄漏如果線程不應該被加入(或只是過期它自己)

明確創建一個線程作爲可連接或分離時,ATTR說法在pthread_create()例程中使用。典型的4個步驟是:

  • 聲明一個並行線程屬性pthread_attr_t數據類型
  • 的變量初始化與pthread_attr_init()
  • 設置與pthread_attr_setdetachstate屬性分離狀態()的屬性變量
  • 完成後,由pthread_attr_destroy()使用的免費庫資源
+0

我檢查了一下,發現當我調用'pthread_detach'**而不是** pthread_exit時 - 沒有像我上面提到的那樣的內存泄漏問題。可以這樣做,而不是寫'pthread_exit'嗎? – lim 2011-04-10 10:23:15

+0

手冊頁說:'應該最終爲每個創建的線程調用pthread_join()或pthread_detach()函數,以便與線程關聯的存儲可以被回收' – sehe 2011-04-10 10:29:03

+0

我已經閱讀過此手冊頁。它仍然不回答我的問題。 – lim 2011-04-10 10:32:55

29

線程的資源除非 線程創建時detach state屬性設置爲 PTHREAD_CREATE_DETACHED,或者如果pthread_detach被調用爲 其pthread_t

undetached線程將保持終止狀態,直到其標識符傳遞到pthread_joinpthread_detach

概括起來講,你有三種選擇:

  1. 創建(PTHREAD_CREATE_DETACHED屬性)分離屬性設爲您的線程
  2. 卸下你的線程創建後(通過調用pthread_detach),或
  3. 加入與終止的線程回收它們(通過調用pthread_join)。

Hth。

+0

請看我的問題http://stackoverflow.com/questions/41057644/memory-leakage-in-windows-pthread-pthread-join-does不釋放內存。這個答案很有用,但沒有幫助我。 – kyb 2016-12-09 10:26:59

4

當不與可接合的線程退出的線程需要調用以釋放 工作所有的資源。

+2

pthread_detach(pthread_self())適用於我(停止報告內存泄漏的valgrind)。這種方法也很方便,因爲始終跟蹤所有線程並在線程動態創建時執行pthread_join()非常困難。 – 2015-03-22 00:17:27

0

請注意,默認的pthread_create行爲是「可連接的」NOT NOT DETACHED。 因此,在pthread完成後,一些OS資源仍然會保留在進程中,這會導致殭屍pthread並導致虛擬/駐留內存使用率增加。

上述四個解決方案將解決此問題。

但是,如果你的線程是一個長期的線程,這可能不是真的需要。例如,如果pthread貫穿整個過程的整個生命週期。