multithreading - Valgrind errors in c++ threads -
i new valgrind. throws 2 errors during memory check following code (http://en.cppreference.com/w/cpp/thread/lock_guard). not sure how interpret errors.
#include <thread> #include <mutex> #include <iostream> int g_i = 0; std::mutex g_i_mutex; // protects g_i void safe_increment() { std::lock_guard<std::mutex> lock(g_i_mutex); ++g_i; std::cout << std::this_thread::get_id() << ": " << g_i << '\n'; } int main() { std::cout << __func__ << ": " << g_i << '\n'; std::thread t1(safe_increment); std::thread t2(safe_increment); t1.join(); t2.join(); std::cout << __func__ << ": " << g_i << '\n'; }
i compiled code on mac using following command:
clang++ simplethread.cpp -o simplethread -lpthread -std=c++11
the code runs expected, following output valgrind.
$ valgrind --tool=memcheck ./simplethread ==34831== memcheck, memory error detector ==34831== copyright (c) 2002-2017, , gnu gpl'd, julian seward et al. ==34831== using valgrind-3.13.0 , libvex; rerun -h copyright info ==34831== command: ./simplethread ==34831== ==34831== syscall param msg->desc.port.name points uninitialised byte(s) ==34831== @ 0x10045f34a: mach_msg_trap (in /usr/lib/system/libsystem_kernel.dylib) ==34831== 0x10045e796: mach_msg (in /usr/lib/system/libsystem_kernel.dylib) ==34831== 0x100458485: task_set_special_port (in /usr/lib/system/libsystem_kernel.dylib) ==34831== 0x1005f410e: _os_trace_create_debug_control_port (in /usr/lib/system/libsystem_trace.dylib) ==34831== 0x1005f4458: _libtrace_init (in /usr/lib/system/libsystem_trace.dylib) ==34831== 0x1000a99df: libsystem_initializer (in /usr/lib/libsystem.b.dylib) ==34831== 0x10001ba1a: imageloadermacho::domodinitfunctions(imageloader::linkcontext const&) (in /usr/lib/dyld) ==34831== 0x10001bc1d: imageloadermacho::doinitialization(imageloader::linkcontext const&) (in /usr/lib/dyld) ==34831== 0x1000174a9: imageloader::recursiveinitialization(imageloader::linkcontext const&, unsigned int, char const*, imageloader::initializertiminglist&, imageloader::uninitedupwards&) (in /usr/lib/dyld) ==34831== 0x100017440: imageloader::recursiveinitialization(imageloader::linkcontext const&, unsigned int, char const*, imageloader::initializertiminglist&, imageloader::uninitedupwards&) (in /usr/lib/dyld) ==34831== 0x100016523: imageloader::processinitializers(imageloader::linkcontext const&, unsigned int, imageloader::initializertiminglist&, imageloader::uninitedupwards&) (in /usr/lib/dyld) ==34831== 0x1000165b8: imageloader::runinitializers(imageloader::linkcontext const&, imageloader::initializertiminglist&) (in /usr/lib/dyld) ==34831== address 0x10488ed4c on thread 1's stack ==34831== in frame #2, created task_set_special_port (???:) ==34831== main: 0 ==34831== thread 2: ==34831== invalid read of size 4 ==34831== @ 0x1005bc899: _pthread_body (in /usr/lib/system/libsystem_pthread.dylib) ==34831== 0x1005bc886: _pthread_start (in /usr/lib/system/libsystem_pthread.dylib) ==34831== 0x1005bc08c: thread_start (in /usr/lib/system/libsystem_pthread.dylib) ==34831== address 0x18 not stack'd, malloc'd or (recently) free'd ==34831== ==34831== ==34831== process terminating default action of signal 11 (sigsegv) ==34831== access not within mapped region @ address 0x18 ==34831== @ 0x1005bc899: _pthread_body (in /usr/lib/system/libsystem_pthread.dylib) ==34831== 0x1005bc886: _pthread_start (in /usr/lib/system/libsystem_pthread.dylib) ==34831== 0x1005bc08c: thread_start (in /usr/lib/system/libsystem_pthread.dylib) ==34831== if believe happened result of stack ==34831== overflow in program's main thread (unlikely ==34831== possible), can try increase size of ==34831== main thread stack using --main-stacksize= flag. ==34831== main thread stack size used in run 8388608. --34831:0:schedule vg_(sema_down): read returned -4 ==34831== ==34831== heap summary: ==34831== in use @ exit: 22,494 bytes in 168 blocks ==34831== total heap usage: 184 allocs, 16 frees, 28,638 bytes allocated ==34831== ==34831== leak summary: ==34831== lost: 16 bytes in 1 blocks ==34831== indirectly lost: 56 bytes in 2 blocks ==34831== possibly lost: 72 bytes in 3 blocks ==34831== still reachable: 4,368 bytes in 10 blocks ==34831== suppressed: 17,982 bytes in 152 blocks ==34831== rerun --leak-check=full see details of leaked memory ==34831== ==34831== counts of detected , suppressed errors, rerun with: -v ==34831== use --track-origins=yes see uninitialised values come ==34831== error summary: 2 errors 2 contexts (suppressed: 1 1) segmentation fault: 11
what these errors mean ?
Comments
Post a Comment