Magick ++段错误在线程中

时间:2013-06-21 14:59:58

标签: c++ segmentation-fault pthreads imagemagick magick++

编写扫描程序。在读取图像后,它会调用convertToPDF()然后读取下一张图像。当在线程中声明Image时,程序会出现故障(RUN FINISHED; Segmentation fault: 11;)。在主线程中运行时,相同的代码工作正常,我将其从thrPDF移动到convertToPDF以确保。所以我认为这与Magick ++的内存分配有关。任何帮助将不胜感激。

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);
}

这是调用堆栈:
omp_get_max_threads(?)
GetOpenMPMaximumThreads内联
AcquirePixelCache(?)
AcquireImage(?)
Magick :: ImageRef :: ImageRef(?)
Magick ::图像::图像(?)
thrPDF(?)
_pthread_start(?)
thread_start(?)

1 个答案:

答案 0 :(得分:1)

如果尚未完成,则应在使用其余API之前在主/原始线程中调用InitializeMagick(NULL)(或InitializeMagick(* argv))。这可能有助于解决与线程相关的一些问题。随着GraphicsMagick中包含Magick ++,这是现代版本中的绝对要求。