crash while declaring Image in a second thread

Questions and postings pertaining to the development of ImageMagick, feature enhancements, and ImageMagick internals. ImageMagick source code and algorithms are discussed here. Usage questions which are too arcane for the normal user list should also be posted here.
Post Reply
eric_vi
Posts: 23
Joined: 2010-03-08T20:16:39-07:00
Authentication code: 8675308

crash while declaring Image in a second thread

Post by eric_vi »

Hello,

thank you in advance for your help...

my code seems to work fine when i run it through the main thread. But now i want to open my image in a thread different than the main one and when i am trying to declare Image it crashes!

here is the track line from the debugger...

Code: Select all

 

my declaration on 

        Image           magickImage;


that calls

// Default constructor
Magick::Image::Image( void )
  : _imgRef(new ImageRef)
{
}

that calls

Magick::ImageRef::ImageRef ( void )
  : _image(0),
    _options(new Options),
    _id(-1),
    _refCount(1),
    _mutexLock()
{
  // Allocate default image
  _image = AcquireImage( _options->imageInfo() );



that calls

  image->cache=AcquirePixelCache(0);



that calls


 if (number_threads == 0)
    cache_info->number_threads=GetOpenMPMaximumThreads();


that calls

gomp_resolve_num_threads



that calls in atomicity.h

#ifdef _GLIBCXX_ATOMIC_BUILTINS
  static inline _Atomic_word 
  __exchange_and_add(volatile _Atomic_word* __mem, int __val)
  { return __sync_fetch_and_add(__mem, __val); }

where it crashes



 
just above there is a comment saying
// To abstract locking primatives across all thread policies, use:
// __exchange_and_add_dispatch
// __atomic_add_dispatch

i am not sure if it is related to my problem, but if it is how do you use that, or what can be done to solve the issue.

I tried to lock the mutex when declaring image but does not seem to work


Thanks again
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: crash while declaring Image in a second thread

Post by magick »

Can you post the smallest subset of code that compiles and fails. We need to download your code and reproduce the problem before we can comment. Threading issues are often difficult to track unless we can see the failure ourselves.
eric_vi
Posts: 23
Joined: 2010-03-08T20:16:39-07:00
Authentication code: 8675308

Re: crash while declaring Image in a second thread

Post by eric_vi »

Well it is a huge program it is impossible to provide...

i have isolated the issue in a way that if i remove the function the code works.. now i agree that it does not guarantee that something before makes that happen

i am on Mac OS X.6 it is in 64bit.

within the function that calls the imageMagick.read i have absolutely nothing before it looks like that ...

Code: Select all

extern short MG_GetImage(QString QfileName)
{

    int32_t		 nWi, nHi, e, tmp;
    Geometry        g;
    Image 		 magickImage;
    PixelPacket    *pixel_cache,
                         *pixel;


    try
    {
        magickImage.read( qPrintable(QfileName) );
        magickImage.modifyImage();
    }
    catch(Exception &error_)
    {

        return -1;
    }





.....  code after
}

i do not even reach the 'try' it crashes at the

Image magickImage;

step
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: crash while declaring Image in a second thread

Post by magick »

OpenMP and Mac threads may not be compatible. Try building and installing ImageMagick without OpenMP support. Add --disable-openmp to the configure script command line and then recompile and reinstall ImageMagick. The -version option should not return the OpenMP feature:
  • -> convert -version
    Version: ImageMagick 6.6.7-4 2011-01-31 Q16 http://www.imagemagick.org
    Copyright: Copyright (C) 1999-2011 ImageMagick Studio LLC
    Features:
We did try your code snippet with a threaded framework of 5 workers and 5 iterations for a total of 25 image reads and it ran without complaint with OpenMP and Posix threads on our Linux system.
buti
Posts: 1
Joined: 2011-04-06T00:54:02-07:00
Authentication code: 8675308

Re: crash while declaring Image in a second thread

Post by buti »

I can confirm your suggested work-around works fine. Is this something that should be fixed in imagemagick?
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: crash while declaring Image in a second thread

Post by magick »

All we can do is issue a warning. See http://www.imagemagick.org/script/archi ... hp#threads:
The OpenMP committee has not defined the behavior of mixing OpenMP with other threading models such as Posix threads. However, using modern releases of Linux, OpenMP and Posix threads appear to interoperate without complaint. If you want to use Posix threads from a program module that calls one of the ImageMagick application programming interfaces (e.g. MagickCore, MagickWand, Magick++, etc.) from Mac OS X or an older Linux release, you may need to disable OpenMP support within ImageMagick. Add the --disable-openmp option to the configure script command line and rebuild and reinstall ImageMagick.
Post Reply