ctinka
ctinka

Reputation: 413

bad access in a simple multi-thread programm on MAC OS X Yosemite

ImageMagick lib crashes when convert large image (more than 1Mb) in a separate thread. My simple test program crashes too with the same message:

#include <thread>
void foo() 
{
    const int size = 0x7FCC9; //program crashes when size is equal or more than this value
    char buff[size];
    for(int i = 0; i < size; ++i)
    {
        buff[i] = i;
    }
}

int main(int argc, char *argv[])
{
    foo(); //passed!
    std::thread thr(foo);
    thr.join(); //got error :(
    return 0;
}

i == 58736 :)

Thread2: EXC_BAD_ACCESS (code=2, address = 0x103512000)

Why my simple code produces this error? How i can increase memory size for thread on MAC OS?

Upvotes: 1

Views: 293

Answers (1)

leanid.chaika
leanid.chaika

Reputation: 2432

Stack size in main thread more then in child thread, so in main thread your buffer stay inside stack size, but in child buffer pass outside and you got Thread2: EXC_BAD_ACCESS (code=2, address = 0x103512000)

I suggest you use boost threads:

boost::thread::attributes attrs;
attrs.set_size(4096*10);
boost::thread myThread(attrs, fooFunction, 42);

http://www.boost.org/doc/libs/1_51_0/doc/html/thread/thread_management.html#thread.thread_management.tutorial.attributes

Upvotes: 1

Related Questions