作者wtchen (没有存在感的人)
看板C_and_CPP
标题[问题] gcc 在 atomic 跟valgrind在Rpi上的bug
时间Wed Oct 19 20:24:15 2016
开发平台(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://webptt.com/cn.aspx?n=bbs/C_and_CPP/M.1476879857.A.E31.html
※ 编辑: wtchen (86.209.148.33), 10/19/2016 20:44:53
1F:推 longlongint: 不知道欸 试试看改用 -O2 ? 10/19 20:45
2F:→ wtchen: 用过了,一样有Error 10/19 20:48
3F:→ wtchen: 如果是RPi+clang去编译,valgrind根本不让我run... 10/19 20:50
4F:→ wtchen: 看了一下gcc的bugzilla,似乎gcc6才修掉这bug 10/19 20:59
5F:→ wtchen: 顺便请教,以本case来说需要用volatile修饰i2c_stat? 10/19 21:41
6F:推 Bencrie: 那个错误叫存取违规不叫 memory leak 10/19 22:04