2017-07-08 33 views
6

私はC++でプログラムを作成していて、非常に奇妙なことに気付きました。新しいスレッドを起動したときにValgrindがエラーになる理由

Xcodeでプログラムを実行すると、すべてうまく動作しますが、Valgrindでそれを行うと、数秒後にsegmentation faultと表示されます。

私は私にそのエラーを与える非常に簡単なコードを抽出するために管理:

#include <thread> 

void exec_1() {} 

int main(int argc, const char * argv[]) { 

    std::thread simulator_thread; 
    simulator_thread = std::thread(exec_1); 
    simulator_thread.join(); 

    return 0; 
} 

私は何をやっていることは、単にこれらのフラグでXcodeの下に私の実行可能ファイルを構築します

CFLAGS:

-I/usr/local/lib/python3.6/site-packages/numpy/core/include 
-I/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/include/python3.6m 
-Wno-unused-result -Wsign-compare -Wunreachable-code 
-fno-common -dynamic -DNDEBUG -g -fwrapv -Wall -Wstrict-prototypes 

LDFLAGS:

-L/usr/local/opt/python3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/config-3.6m-darwin 
-lpython3.6m -ldl -framework CoreFoundation 

を実行し、Valgrindの実行可能ファイルを実行してメモリリークを検出します。私がmainコードで使用しているので、私はPython C APIと呼んでいますが、このコードではsegfaultを使用せずに私にスローします。

とにかくValgrindのは、いくつかの他のものと一緒に、私に次のような出力が得られます。

Thread 2: 
==41660== Invalid read of size 4 
==41660== at 0x1016FA899: _pthread_body (in /usr/lib/system/libsystem_pthread.dylib) 
==41660== by 0x1016FA886: _pthread_start (in /usr/lib/system/libsystem_pthread.dylib) 
==41660== by 0x1016FA08C: thread_start (in /usr/lib/system/libsystem_pthread.dylib) 
==41660== Address 0x18 is not stack'd, malloc'd or (recently) free'd 
==41660== 
==41660== 
==41660== Process terminating with default action of signal 11 (SIGSEGV) 
==41660== Access not within mapped region at address 0x18 
==41660== at 0x1016FA899: _pthread_body (in /usr/lib/system/libsystem_pthread.dylib) 
==41660== by 0x1016FA886: _pthread_start (in /usr/lib/system/libsystem_pthread.dylib) 
==41660== by 0x1016FA08C: thread_start (in /usr/lib/system/libsystem_pthread.dylib) 
==41660== If you believe this happened as a result of a stack 
==41660== overflow in your program's main thread (unlikely but 
==41660== possible), you can try to increase the size of the 
==41660== main thread stack using the --main-stacksize= flag. 
==41660== The main thread stack size used in this run was 8388608. 
--41660:0:schedule VG_(sema_down): read returned -4 

はそれがValgrindの下のスレッドを生成すると、エラーの原因であることは可能ですか?

P.S:
私のOSはMacOS 10.12.5であると私はXcode 8.3.3Valgrind 3.13.0を使用しています。

+0

CFLAGSとLDFLAGSに-pthreadフラグを追加しても違いはありますか? – nos

+0

あなたの問題に関係はありませんが、ほとんどの場合、割り当ての初期化を推奨する必要があります。 'std :: thread simulator_thread(exec_1);' –

+0

@NeilButterworthそうですね。しかし、私の主なプログラムでは、 'std :: thread'の空の配列を作成し、それに実オブジェクトに' for'ループを割り当てることで、複数のスレッドを生成します。ここでは、私のメインコードに可能な限り近い構造を維持しようとしました – jackscorrow

答えて

5

Is it possible that spawning a thread under Valgrind is the cause of the error?

pthreadのを使用するバイナリを実行している場合、このは、Mac OS X上で実際にValgrindのに問題があることが表示されます:_pthread_find_threadでマッピングされた領域内

アクセスできません(OS X 10.11) https://bugs.kde.org/show_bug.cgi?id=349128

Valgrindの失敗は、ここに報告されたものと似ています:

std::thread.join() SIGSEGV on Mac OS under Valgrind

+0

私はかなり古いバグです。 。 ありがとうございました! – jackscorrow

関連する問題