[Magick++] Multi-threading in OS X: bus error
Posted: 2010-04-13T06:33:21-07:00
I use Magick++ to load images in a C++ program. The usage of Magick++ is limited to the following chunk of code:
Of course this piece of code is simplified and would be useless as-is. In reality imgWidth, imgHeight and imageData are members of a class.
The global structure of the program is as follows:
1. Load a reference image and store its pixels in a buffer.
2. Start multiple independent threads (through pthreads) that each do the following:
a. Load an image (using the above chunk of code) from a semaphore-protected queue.
b. Do some operations on the image image pixels and compare them with the reference buffer.
c. Report the comparison score in a semaphore-protected score array.
d. Back to a. or exit if the queue is empty.
This program has been running perfectly for years in both Linux and Mac OS X. It still runs perfectly in Linux, but since I moved from ImageMagick 6.4.1 to 6.5.8 in OS X (installed through Fink), it crashes with a bus error whenever I try to run the program with more than one thread. The crash occurs when the second thread tries to execute the Image() call in the chunk of code given above:
Now, I haven't looked at Magick++ in years and I have noticed that a lot has changed. I found that I'm supposed to call InitializeMagick() before doing anything, but the bus error still occurs when I do that (and in Linux the program happily runs without InitializeMagick). Do I need to do some other kind of initialization or is this 'simply' a bug somewhere?
Code: Select all
try {
Image image( sFileName.c_str() );
image.type( GrayscaleType );
int imgWidth = image.columns();
int imgHeight = image.rows();
unsigned char *imageData = new unsigned char[imgWidth*imgHeight];
image.write(0,0, imgWidth, imgHeight, "I", MagickCore::CharPixel, imageData);
}
catch( Exception e ) { ...
The global structure of the program is as follows:
1. Load a reference image and store its pixels in a buffer.
2. Start multiple independent threads (through pthreads) that each do the following:
a. Load an image (using the above chunk of code) from a semaphore-protected queue.
b. Do some operations on the image image pixels and compare them with the reference buffer.
c. Report the comparison score in a semaphore-protected score array.
d. Back to a. or exit if the queue is empty.
This program has been running perfectly for years in both Linux and Mac OS X. It still runs perfectly in Linux, but since I moved from ImageMagick 6.4.1 to 6.5.8 in OS X (installed through Fink), it crashes with a bus error whenever I try to run the program with more than one thread. The crash occurs when the second thread tries to execute the Image() call in the chunk of code given above:
Code: Select all
(gdb) backtrace
#0 gomp_resolve_num_threads (specified=0) at /var/tmp/gcc/gcc-5646.1~2/src/libgomp/parallel.c:48
#1 0x00276c22 in omp_get_max_threads () at /var/tmp/gcc/gcc-5646.1~2/src/libgomp/parallel.c:96
#2 0x000f1c15 in AcquirePixelCache ()
#3 0x001aec2c in AcquireImage ()
#4 0x0005adc6 in Magick::ImageRef::ImageRef ()
#5 0x00056219 in Magick::Image::Image ()