看板 C_and_CPP 關於我們 聯絡資訊
開發平台(Platform): (Ex: Win10, Linux, ...) RP1 + Raspbian 編譯器(Ex: GCC, clang, VC++...)+目標環境(跟開發平台不同的話需列出) gcc 4.9.2 編譯參數:-Wall -pedantic -O3 -std=gnu11 -lpthread 額外使用到的函數庫(Library Used): (Ex: OpenGL, ...) pthread 問題(Question): 正在練習寫multithread的code 使用gcc編譯會有Warning(用clang不會) 跑起來也沒問題 可是valgrind顯示有memary leakage (我沒用malloc) 不知道如何改善 預期的正確結果(Expected Output): 無memory leak 錯誤結果(Wrong Output): gcc的Warning如下: atomic.c:30:9: warning: right-hand operand of comma expression has no effect [-Wunused-value] ++acnt; ^ atomic.c:32:9: warning: right-hand operand of comma expression has no effect [-Wunused-value] --acnt; ^ Valgrind在RPi的Error如下: ==1083== Memcheck, a memory error detector ==1083== Copyright (C) 2002-2011, and GNU GPL'd, by Julian Seward et al. ==1083== Using Valgrind-3.7.0 and LibVEX; rerun with -h for copyright info ==1083== Command: ./atomic ==1083== 0th Time: One 0th Time: Three 0th Time: 1th Time: Three 1th Time: One Two 2th Time: One 2th Time: Three 3th Time: One 1th Time: Two 4th Time: One 3th Time: Three 2th Time: Two 5th Time: One 4th Time: Three 3th Time: Two 6th Time: One 5th Time: Three 4th Time: Two 7th Time: One 6th Time: Three 5th Time: Two 8th Time: One 7th Time: Three 6th Time: Two 9th Time: One 8th Time: Three 7th Time: Two 9th Time: Three 8th Time: Two 9th Time: Two ==1083== Thread 2: ==1083== Invalid read of size 4 ==1083== at 0x40197E0: strlen (strlen.S:76) ==1083== by 0x400B6DB: _dl_new_object (dl-object.c:157) ==1083== by 0x4005A5B: _dl_map_object_from_fd (dl-load.c:1059) ==1083== by 0x4008D93: _dl_map_object (dl-load.c:2605) ==1083== by 0x40143F7: dl_open_worker (dl-open.c:235) ==1083== by 0x400FE93: _dl_catch_error (dl-error.c:187) ==1083== by 0x4013D47: _dl_open (dl-open.c:661) ==1083== by 0x4999DEB: do_dlopen (dl-libc.c:87) ==1083== by 0x400FE93: _dl_catch_error (dl-error.c:187) ==1083== by 0x4999ED3: dlerror_run (dl-libc.c:46) ==1083== by 0x4999F53: __libc_dlopen_mode (dl-libc.c:163) ==1083== by 0x4879A7F: pthread_cancel_init (unwind-forcedunwind.c:46) ==1083== Address 0x51d2274 is 36 bytes inside a block of size 39 alloc'd ==1083== at 0x4833970: malloc (vg_replace_malloc.c:263) ==1083== by 0x400579B: local_strdup (dl-load.c:162) ==1083== by 0x4008FEB: _dl_map_object (dl-load.c:2509) ==1083== by 0x40143F7: dl_open_worker (dl-open.c:235) ==1083== by 0x400FE93: _dl_catch_error (dl-error.c:187) ==1083== by 0x4013D47: _dl_open (dl-open.c:661) ==1083== by 0x4999DEB: do_dlopen (dl-libc.c:87) ==1083== by 0x400FE93: _dl_catch_error (dl-error.c:187) ==1083== by 0x4999ED3: dlerror_run (dl-libc.c:46) ==1083== by 0x4999F53: __libc_dlopen_mode (dl-libc.c:163) ==1083== by 0x4879A7F: pthread_cancel_init (unwind-forcedunwind.c:46) ==1083== by 0x4879BAF: _Unwind_ForcedUnwind (unwind-forcedunwind.c:145) ==1083== ==1083== ==1083== HEAP SUMMARY: ==1083== in use at exit: 948 bytes in 4 blocks ==1083== total heap usage: 8 allocs, 4 frees, 1,384 bytes allocated ==1083== ==1083== LEAK SUMMARY: ==1083== definitely lost: 0 bytes in 0 blocks ==1083== indirectly lost: 0 bytes in 0 blocks ==1083== possibly lost: 0 bytes in 0 blocks ==1083== still reachable: 948 bytes in 4 blocks ==1083== suppressed: 0 bytes in 0 blocks ==1083== Rerun with --leak-check=full to see details of leaked memory ==1083== ==1083== For counts of detected and suppressed errors, rerun with: -v ==1083== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0) 程式碼(Code):(請善用置底文網頁, 記得排版) https://gist.github.com/gnitnaw/84939b400ba3391cec188e5376dedaf9 補充說明(Supplement): 同樣的code我拿來筆電跑(core*4 + Lubuntu 16.04 + gcc 5.4 + Valgrind-3.11.0) 在Valgrind是沒有錯誤的,不過gcc的warning還是在。 改用clang就沒有warning了 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 86.209.148.33 ※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1476879857.A.E31.html ※ 編輯: wtchen (86.209.148.33), 10/19/2016 20:44:53
longlongint: 不知道欸 試試看改用 -O2 ? 10/19 20:45
wtchen: 用過了,一樣有Error 10/19 20:48
wtchen: 如果是RPi+clang去編譯,valgrind根本不讓我run... 10/19 20:50
wtchen: 看了一下gcc的bugzilla,似乎gcc6才修掉這bug 10/19 20:59
wtchen: 順便請教,以本case來說需要用volatile修飾i2c_stat? 10/19 21:41
Bencrie: 那個錯誤叫存取違規不叫 memory leak 10/19 22:04