mercredi 24 août 2016

std::thread throws Access violation exception when created with arguments?

I'm using VS2015 and encounter a extremely strange problem when using std::thread.

void Klass::myfunc(int a, int b) { std::cout << a << ' ' << b << std::endl; }
...
auto t = std::thread(&Klass::myfunc, this, 100, 200); 

It works well on Debug mode, but throws an "Access violation exception" when I turn to Release mode.

What's more, if I try to modify "myfunc" to this:

void Klass::myfunc() { std::cout << "foo" << std::endl; }
...
auto t = std::thread(&Klass::myfunc, this);

it works well again.

I'm guarantee that "&Klass::myfunc" and "this" pointers are not NULL.

I guess it might be some kind of "undefined behavior" but I have no idea what is it exactly.

The call stack is something like this:

    000000c83a4ffd40()  Unknown
>   distributed_word_embedding.exe!std::_LaunchPad<std::unique_ptr<std::tuple<void (__cdecl multiverso::Communicator::*)(void) __ptr64,multiverso::Communicator * __ptr64>,std::default_delete<std::tuple<void (__cdecl multiverso::Communicator::*)(void) __ptr64,multiverso::Communicator * __ptr64> > > >::_Run(std::_LaunchPad<std::unique_ptr<std::tuple<void (__cdecl multiverso::Communicator::*)(void),multiverso::Communicator *>,std::default_delete<std::tuple<void (__cdecl multiverso::Communicator::*)(void),multiverso::Communicator *> > > > * _Ln) Line 247    C++
    distributed_word_embedding.exe!std::_Pad::_Call_func(void * _Data) Line 210 C++
    ucrtbase.dll!00007ffabdc7be1d() Unknown
    kernel32.dll!00007ffabfae8102() Unknown
    ntdll.dll!00007ffac26bc5b4()    Unknown

Aucun commentaire:

Enregistrer un commentaire