C ++线程中的Valgrind错误

时间:2017-08-15 00:36:27

标签: multithreading c++11 valgrind

我是Valgrind的新手。在内存检查期间,它会针对以下代码(http://en.cppreference.com/w/cpp/thread/lock_guard)引发2个错误。不确定如何解释这些错误。

#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';
}

我使用以下命令在Mac上编译了此代码:

clang++ simplethread.cpp -o simplethread -lpthread -std=c++11

代码按预期运行,但我从Valgrind获得以下输出。

$ valgrind --tool=memcheck ./simplethread
==34831== Memcheck, a memory error detector
==34831== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==34831== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==34831== Command: ./simplethread
==34831== 
==34831== Syscall param msg->desc.port.name points to uninitialised byte(s)
==34831==    at 0x10045F34A: mach_msg_trap (in         /usr/lib/system/libsystem_kernel.dylib)
==34831==    by 0x10045E796: mach_msg (in /usr/lib/system/libsystem_kernel.dylib)
==34831==    by 0x100458485: task_set_special_port (in  /usr/lib/system/libsystem_kernel.dylib)
==34831==    by 0x1005F410E: _os_trace_create_debug_control_port (in /usr/lib/system/libsystem_trace.dylib)
==34831==    by 0x1005F4458: _libtrace_init (in /usr/lib/system/libsystem_trace.dylib)
==34831==    by 0x1000A99DF: libSystem_initializer (in /usr/lib/libSystem.B.dylib)
==34831==    by 0x10001BA1A:    ImageLoaderMachO::doModInitFunctions(ImageLoader::LinkContext const&) (in  /usr/lib/dyld)
==34831==    by 0x10001BC1D:   ImageLoaderMachO::doInitialization(ImageLoader::LinkContext const&) (in  /usr/lib/dyld)
==34831==    by 0x1000174A9:   ImageLoader::recursiveInitialization(ImageLoader::LinkContext const&, unsigned int, char const*, ImageLoader::InitializerTimingList&,    ImageLoader::UninitedUpwards&) (in /usr/lib/dyld)
==34831==    by 0x100017440: ImageLoader::recursiveInitialization(ImageLoader::LinkContext const&, unsigned int, char const*, ImageLoader::InitializerTimingList&, ImageLoader::UninitedUpwards&) (in /usr/lib/dyld)
==34831==    by 0x100016523: ImageLoader::processInitializers(ImageLoader::LinkContext const&, unsigned int, ImageLoader::InitializerTimingList&, ImageLoader::UninitedUpwards&) (in /usr/lib/dyld)
==34831==    by 0x1000165B8: ImageLoader::runInitializers(ImageLoader::LinkContext const&, ImageLoader::InitializerTimingList&) (in /usr/lib/dyld)
==34831==  Address 0x10488ed4c is on thread 1's stack
==34831==  in frame #2, created by task_set_special_port (???:)
==34831== 
main: 0
==34831== Thread 2:
==34831== Invalid read of size 4
==34831==    at 0x1005BC899: _pthread_body (in /usr/lib/system/libsystem_pthread.dylib)
==34831==    by 0x1005BC886: _pthread_start (in /usr/lib/system/libsystem_pthread.dylib)
==34831==    by 0x1005BC08C: thread_start (in /usr/lib/system/libsystem_pthread.dylib)
==34831==  Address 0x18 is not stack'd, malloc'd or (recently) free'd
==34831== 
==34831== 
==34831== Process terminating with default action of signal 11 (SIGSEGV)
==34831==  Access not within mapped region at address 0x18
==34831==    at 0x1005BC899: _pthread_body (in /usr/lib/system/libsystem_pthread.dylib)
==34831==    by 0x1005BC886: _pthread_start (in /usr/lib/system/libsystem_pthread.dylib)
==34831==    by 0x1005BC08C: thread_start (in /usr/lib/system/libsystem_pthread.dylib)
==34831==  If you believe this happened as a result of a stack
==34831==  overflow in your program's main thread (unlikely but
==34831==  possible), you can try to increase the size of the
==34831==  main thread stack using the --main-stacksize= flag.
==34831==  The main thread stack size used in this run was 8388608.
--34831:0:schedule VG_(sema_down): read returned -4
==34831== 
==34831== HEAP SUMMARY:
==34831==     in use at exit: 22,494 bytes in 168 blocks
==34831==   total heap usage: 184 allocs, 16 frees, 28,638 bytes allocated
==34831== 
==34831== LEAK SUMMARY:
==34831==    definitely 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 with --leak-check=full to see details of leaked memory
==34831== 
==34831== For counts of detected and suppressed errors, rerun with: -v
==34831== Use --track-origins=yes to see where uninitialised values come from
==34831== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 1 from 1)
Segmentation fault: 11

这些错误意味着什么?

0 个答案:

没有答案