Calling Image::magick() leads to debug assertion and crash

Post any defects you find in the released or beta versions of the ImageMagick software here. Include the ImageMagick version, OS, and any command-line required to reproduce the problem. Got a patch for a bug? Post it here.
Post Reply
eldiener

Calling Image::magick() leads to debug assertion and crash

Post by eldiener »

The code below, using ImageMagick 6.3.5.8 in VC6 leads to a debug assertion in the destruction of the string returned by Image::magick():

----------------------------------------------------------------------------

// TestIM.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <string>
#include <iostream>
#include <Magick++.h>

int main(int argc, char* argv[])
{
try
{
Magick::Image image("bluebells_darker.jpg");
image.magick();
} // Debug assertion occurs here, after which program crashes if I continue
catch (Magick::Exception & exc)
{
std::string m(exc.what());
std::cout << "Exception caught: " << m;
}
return 0;
}

-----------------------------------------------------------------------

I have also tested this out at home using VC8 and 6.3.5.10 and the same problem occurs.
My configuration when building for both versions is:

------------------------------------------------------------------------

// ImageMagick build feature configuration. Please note that
// disabling a feature via this header file may not be sufficient to
// remove any library dependencies from the build. The VisualMagick
// project files may need to be edited to remove libraries the feature
// depends on. When building a static ImageMagick, coder
// registrations are made via magick\static.c so if a format is
// removed, the call to its registration function should be commented
// out in static.c. Note that VisualMagick configure updates
// magick\static.c so re-running configure may cause local changes to
// be lost.
//
// Note that by default ImageMagick is configured with a QuantumDepth of 8
// and looks for all files in the directory where the executable is located.
// The installed configuration (i.e. the setup.exe-style installer) is
// modifying by defining "UseInstalledMagick". If you would like to install
// ImageMagick using hard-coded paths, or want to use the Windows registry to
// install ImageMagick, then "UseInstalledMagick" should be defined.
//
// Enabled options are of the form:
//
// #define option
//
// while disabled options are initially in the form
//
// // #undef option
//
// so it is necessary to remove the comment, and change "undef" to "define"
// in order for the option to be enabled.

// Specify size of PixelPacket color Quantums (8, 16, or 32).
// A value of 8 uses half the memory than 16 and typically runs 30% faster,
// but provides 256 times less color resolution than a value of 16.
//
#define QuantumDepth 16

// Define to build a ImageMagick which uses registry settings or
// hard-coded paths to locate installed components. This supports
// using the "setup.exe" style installer, or using hard-coded path
// definitions (see below). If you want to be able to simply copy
// the built ImageMagick to any directory on any directory on any machine,
// then do not use this setting.
//
// #undef UseInstalledMagick

// When building ImageMagick using DLLs, include a DllMain()
// function which automatically invokes InitializeMagick(NULL), and
// DestroyMagick() so that the user doesn't need to. This is enabled
// by default.
//
#define ProvideDllMain

// Define if MIT X11 is available (or stubbed). It is not actually
// necessary to use X11 or the X11 stubs library. The VisualMagick configure
// program assumes that X11 stubs is being used if X11 is not supported.
// To achieve a slimmer ImageMagick, undefine HasX11 and remove the 'xlib'
// project from the ImageMagick workspace.
//
#define HasX11

// Exclude deprecated methods in MagickCore API
//
//#under ExcludeMagickDeprecated

// Define to enable high dynamic range imagery (HDRI) (experimental)
//
// #undef UseHDRI

/////////////
//
// Optional packages
//
// All packages except autotrace are included by default in the build.

// Define to use the bzip2 compression library
// #undef HasBZLIB

// Define to use the FlashPIX library (fpx module/subdirectory)
// #undef HasFPX

// Define to use the FreeType (TrueType & Postscript font support) library
// #undef HasFREETYPE

// Define to use the JBIG library
// #Undef HasJBIG

// Define to use the Jasper JPEG v2 library
#define HasJP2

// Define to use the IJG JPEG v1 library
#define HasJPEG

// Define to use the "little" Color Management System (LCMS) library
// #undef HasLCMS

// Define to use the PNG library
// #undef HasPNG

// Define to use the TIFF library
// #undef HasTIFF

// Define to use the Windows GDI32 library (for clipboard and emf modules)
#define HasWINGDI32

// Define to use the libwmf WMF parsing library
// #undef HasWMFlite

// Define to use the GNOME XML library
// #undef HasXML

// Define to use the zlib ZIP compression library
// #undef HasZLIB

// Define to use the autotrace library (obtain sources seperately)
//
// #undef HasAUTOTRACE

// Define to enable self-contained, embeddable, zero-configuration ImageMagick (experimental)
//
// #undef UseEmbeddableMagick
/////////////

//
// Hard Coded Paths
//
// If hard-coded paths are defined via the the following define
// statements, then they will override any values from the Windows
// registry. It is unusual to use hard-coded paths under Windows.

// Optional: Specify where X11 application resource files are installed
// #define ApplicationDefaults "c:\\ImageMagick\\"

// Optional: Specify where user-specific X11 application resource files are installed
// #define PreferencesDefaults "~\\."

// Optional: Specify where convert.exe and support executables are installed
// #define MagickBinPath "c:\\ImageMagick\\"

// Optional: Specify where operating system specific files are installed
// #define MagickLibPath "c:\\ImageMagick\\"

// Optional: Specify where operating system independent files are installed
// #define MagickSharePath "c:\\ImageMagick\\"

// Optional: Specify where coder modules (DLLs) are installed
// #define MagickCoderModulesPath "c:\\ImageMagick\\"

// Optional: Specify where filter modules (DLLs) are installed
// #define MagickFilterModulesPath "c:\\ImageMagick\\"

// Magick API method prefix. Define to add a unique prefix to all API methods.
// #undef MagickMethodPrefix

/////////////
//
// The remaining defines should not require user modification.
//

// Use Visual C++ C inline method extension to improve performance
#define inline __inline

// Visual C++ does not define ssize_t by default.
#if !defined(ssize_t)
# define ssize_t long
#endif

/* Define to 1 if you have the <ft2build.h> header file. */
#define HAVE_FT2BUILD_H 1

/* Define to 1 if you have the `ftruncate' function. */
#define HAVE_FTRUNCATE 1

// Define to support memory mapping files for improved performance
#define HAVE_MMAP_FILEIO 1

/* Define to 1 if you have the `raise' function. */
#define HAVE_RAISE 1

/* Define to 1 if you have the `memmove' function. */
#define HAVE_MEMMOVE 1

/* Define to 1 if you have the `sysconf' function. */
#define HAVE_SYSCONF 1

/* Define to 1 if you have the `vsnprintf' function. */
#define HAVE_VSNPRINTF 1

/* Define to 1 if you have the `popen' function. */
#define HAVE_POPEN 1

/* Define to if you have the `strcasecmp' function. */
#define HAVE_STRCASECMP 1

/* Define to 1 if you have the `strncasecmp' function. */
#define HAVE_STRNCASECMP 1

/* Define to 1 if you have the `tempnam' function. */
#define HAVE_TEMPNAM 1

// Define to include the <sys/types.h> header file
#define HAVE_SYS_TYPES_H 1

#define HAVE_VSNPRINTF 1
#define HAVE_TEMPNAM 1
#define HAVE_RAISE 1

/*
Tiff features.
*/

/* Define to 1 if you have the <tiffconf.h> header file. */
#define HAVE_TIFFCONF_H 1

/* Define to 1 if you have the `TIFFMergeFieldInfo' function. */
#define HAVE_TIFFMERGEFIELDINFO 1

/* Define to 1 if you have the `TIFFSetErrorHandlerExt' function. */
#define HAVE_TIFFSETERRORHANDLEREXT 1

/* Define to 1 if you have the `TIFFSetTagExtender' function. */
#define HAVE_TIFFSETTAGEXTENDER 1

/* Define to 1 if you have the `TIFFSetWarningHandlerExt' function. */
#define HAVE_TIFFSETWARNINGHANDLEREXT 1

--------------------------------------------------------------------------------

The DLLs in my bin directory are:

CORE_RL_jp2_.dll
CORE_RL_jpeg_.dll
CORE_RL_magick_.dll
CORE_RL_Magick++_.dll
CORE_RL_wand_.dll
CORE_RL_xlib_.dll
IM_MOD_RL_jp2_.dll
IM_MOD_RL_jpeg_.dll
IM_MOD_RL_xc_.dll

along with all of the configuration XML files.
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: Calling Image::magick() leads to debug assertion and crash

Post by magick »

We tried your program with ImageMagick 6.3.5-10, the current release and it ran without complaint. We compiled with VS 7.
eldiener

Re: Calling Image::magick() leads to debug assertion and crash

Post by eldiener »

magick wrote:We tried your program with ImageMagick 6.3.5-10, the current release and it ran without complaint. We compiled with VS 7.
Did you build from scratch or install a binary release ? If the former, did your configuration match mine ? Finally, if the former did you have just those ImageMagick DLLs in your bin directory which I had ?
eldiener

Re: Calling Image::magick() leads to debug assertion and crash

Post by eldiener »

magick wrote:We tried your program with ImageMagick 6.3.5-10, the current release and it ran without complaint. We compiled with VS 7.
I downloaded 6.3.5-10 for Windows, built everything with my configuration options as explained in my OP, rebuilt my program with VC6, and the same bug occurs. Since I moved all the necessary IM header and lib files under my program's VC6 project directory, and put all my IM dlls as well as the config XML files in the program's output directory, I can zip it all to you and you can duplicate it yourself under VC6. All you need to do is step through with the debugger to see the error occurring.

If you like I can try this with VC8 also, but when I did it at home over the weekend, the same problem occurred. However if you like I can download the free Visual Studio 2005 express and duplicate it for you here at work, and then zip up that project and send it to you also.

I really would like you to be able to duplicate this for yourself. I am using Windows and the Windows IM distribution which has VisualMagick to build everything.

I just need some e-mail address where I can send you the zipped projects which show this bug. Please tell me what you want me to do since this bug is really impeding my development efforts here at work, given that I can not use Image::magick() to find out the file type of the image because of the bug, and it is necessary in the project On which I am working to restrict the type of image the end user can choose. BTW other Image:: functionality is working without problems so far. My guess is that Image::magick() is not returning a valid std::string object, since the debug assertion is occurring when the std::string() returned by Image::magick() is destructed.

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

Re: Calling Image::magick() leads to debug assertion and crash

Post by magick »

Cut and paste your code into the button.cpp source module typically at c:/Program Files/ImageMagick-6.3.5-Q16/Magick++_demos . Next build and run the button workspace and see if the problem still occurs. We've seen problems like you are describing in the past because the workspaces were not set with compatible compile/linker options. If the button tests works, use the button workspace as a model for your own projects' workspace.
eldiener

Re: Calling Image::magick() leads to debug assertion and crash

Post by eldiener »

magick wrote:Cut and paste your code into the button.cpp source module typically at c:/Program Files/ImageMagick-6.3.5-Q16/Magick++_demos . Next build and run the button workspace and see if the problem still occurs. We've seen problems like you are describing in the past because the workspaces were not set with compatible compile/linker options. If the button tests works, use the button workspace as a model for your own projects' workspace.
My code is working properly when inserted into button.c so I will use that to see what I am doing differently which is causing my problem. I will report back to you, on this same thread, when I find out what is the cause of the problem I am having. It may take a day or two to discover the problem as I work on other things.
eldiener

Re: Calling Image::magick() leads to debug assertion and crash

Post by eldiener »

magick wrote:Cut and paste your code into the button.cpp source module typically at c:/Program Files/ImageMagick-6.3.5-Q16/Magick++_demos . Next build and run the button workspace and see if the problem still occurs. We've seen problems like you are describing in the past because the workspaces were not set with compatible compile/linker options. If the button tests works, use the button workspace as a model for your own projects' workspace.
The problem occurred because the program I was using to test the bug was compiled with debugging turned on and yet was linking to the non-debug version of the ImageMagick libraries. Once I had linked to the debug version of the ImageMagick libraries, the problem went away.

Similarly in my actual case I was writing a DLL which was being tested in debug mode so as to be able to test code, and yet I was linking to the non-debug version of the ImageMagick libraries. Once I linked my debug mode DLL to the debug version of the ImageMagick libraries, everything worked fine.

What I think is happening is that mixing debug and non-debug version in a link brings in both the debug and non-debug versions of the VC run-time library DLLs and this does not work well when passing dynamically allocated pointers, which a std::string holds, between two different versions of the VC RTLs.

You might want to tell developers to be careful always to link with the correct release or debug versions of ImageMagick depending if they are compiling their code in non-debug or debug mode.

I also noticed that in your binary releases you only distribute the release versions of ImageMagick. This will almost definitely lead to problems if developers use the binary release to link with the debug version of their own code while testing, as my situation demonstrates. Essentially developers always need to build the release themselves to do programming with it, and not use the binary release.
manojmka

Re: Calling Image::magick() leads to debug assertion and crash

Post by manojmka »

I am using VC6. Changed the Code Generation option to "Multithreaded DLL". It is working with VC2005 but not with 6.0. Please help
Post Reply