ryanpf
ryanpf

Reputation: 136

Magick++ segfaults in thread

Writing a scanning program. After it reads an image, it calls convertToPDF() and then reads the next image. The program seg faults (RUN FINISHED; Segmentation fault: 11;) when the Image is declared in the thread. The same code works fine when run in the main thread, I moved it from thrPDF to convertToPDF to make sure. So I'm thinking it's something to do with Magick++'s memory allocation that is over my head. Any help would be much appreciated.

void ScanWindow::convertToPDF(string fileName)
{
   pthread_t convert;
   string* args = new string(fileName);
   void *thrPDF(void*);
   pthread_create(&convert,NULL,thrPDF,args);
}

void *thrPDF(void* a)
{
   string* fName = (string*) a;
   string newFile = fName->substr(0,fName->length()-3) + "pdf";

   Magick::Image img(*fName);  // this is the line that seg faults
   img.magick("pdf");
   img.write(newFile);

   pthread_exit(0);
}

Here is the call stack:
omp_get_max_threads(?)
GetOpenMPMaximumThreads inlined
AcquirePixelCache(?)
AcquireImage(?)
Magick::ImageRef::ImageRef(?)
Magick::Image::Image(?)
thrPDF(?)
_pthread_start(?)
thread_start(?)

Upvotes: 3

Views: 556

Answers (1)

Bob Friesenhahn
Bob Friesenhahn

Reputation: 306

If it is not already being done, you should be invoking InitializeMagick(NULL) (or InitializeMagick(*argv)) in your main/original thread prior to using the rest of the API. This may help cure some issues related to threading. With Magick++ included with GraphicsMagick, this is an absolute requirement in modern releases.

Upvotes: 1

Related Questions