Magick++ Segmentation Fault

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
handra

Magick++ Segmentation Fault

Post by handra »

Hi there,

I am new to using ImageMagick as image processing library and really need your help regarding the segmentation fault error when using the Magick++ Library for C++.

Previously, I developed a simple application used to test the magick++ library, and it worked successfully. Below is the simple source code I created:

Code: Select all

#include <cstdlib>
#include <Magick++.h>

using namespace std ;
using namespace Magick ;

int main ( int argc , char** argv )
{
    Geometry g ( 100 , 100 ) ;
    Color c ( MaxRGB , MaxRGB , MaxRGB , 0 ) ;
    Magick::Image image ( g , c ) ;

    for ( int x = 20 ; x <= 80 ; x ++ )
        image.pixelColor ( x , 50 , Color ( 0 , 0 , 0 , 0 ) ) ;

    image.magick ( "png" ) ;
    image.write ( "/path_to_image/test.png" ) ;

    return 0 ;
}
However, when I tried to implement it in a bigger project, I got segmentation fault error. Below is the code that won't work on my computer.

Code: Select all

#include "signaturewriter.h"

namespace util
{
    SignatureWriter::SignatureWriter ( int width , int height , string pixel )
    {
        this -> _width = width ;
        this -> _height = height ;

        this -> _pixel = pixel ;
    }

    void SignatureWriter::writeToStorage ( string path )
    {
        Geometry g ( this -> _width , this -> _height ) ;
        Color c ( MaxRGB , MaxRGB , MaxRGB , 0 ) ;
        Magick::Image image ( g , c ) ;

        for ( int h = 0 ; h < this -> _height ; h ++ )
        {
            for ( int w = 0 ; w < this -> _width ; w ++ )
            {
                int index = ( ( this -> _width ) * h ) + w ;

                if ( ( this -> _pixel ) [ index ] == '1' )
                    image.pixelColor ( w , h , Color ( 0 , 0 , 0 , 0 ) ) ;
            }
        }

        image.magick ( "png" ) ;
        image.write ( path ) ;
    }
}
When I tried to trace through the source code, I found that the error occurred at

Code: Select all

        Magick::Image image ( g , c ) ;
I really don't know what is wrong with the source code, as the main logic is basically the same as the first source code that works perfectly. Is there any problem with putting the source code into the class?

Thanks in advance for your great help...

FYI, when I tried to move the source code of the magick++ in the bigger project one to the main function, it works as expected.
handra

Re: Magick++ Segmentation Fault

Post by handra »

I found that this problem occurs only when I call the source code using Thread. So, I assume that the problem is with the multi-threading and Magick++, however, I still can't find a way to get rid of the error message.

Anyone?

Thanks...
dproc
Posts: 19
Joined: 2010-09-14T20:39:15-07:00
Authentication code: 8675308

Re: Magick++ Segmentation Fault

Post by dproc »

I'm also very new to imageMagick and I am using Magick++. One lesson I recently learned is to enclose everything in try/catch blocks because the library makes extensive use of exceptions. For example:

Code: Select all

	int ReturnCode=-1;
        ...
	try
	{
		Image1=new Magick::Image(FileName);
		if(Image1)
		if(Image1->isValid())
			ReturnCode=0;
	}
	catch( exception &error_ )
	{
		cout << "Caught exception: " << error_.what() << endl;
	}

handra

Re: Magick++ Segmentation Fault

Post by handra »

I've tried to insert the source code into the try/catch block, unfortunately, it still didn't help much. I still got the same error message.
/Applications/NetBeans/NetBeans 6.9.app/Contents/Resources/NetBeans/ide/bin/nativeexecution/dorun.sh: line 33: 2498 Segmentation fault sh "${SHFILE}
Post Reply