This project has moved. For the latest updates, please go here.

System.AccessViolationException when combining two images

Jul 7, 2014 at 4:49 PM
Edited Jul 7, 2014 at 4:53 PM
I am using the Q16-x64 nuget package.

I am trying to overlay one image on top of another image.
(I am printing out checks and need to take the check detail and be able to print it over top of a few different styles of check stock).

Here is some test code I wrote that causes the error:
 using (var collection = new MagickImageCollection())
            {
                var settingsBottom = new MagickReadSettings();
                var settingsTop = new MagickReadSettings();;

                using (MagickImage bottomLayer = new MagickImage(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "checkStock.gif"), settingsBottom),
                                      topLayer = new MagickImage(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "1.png"), settingsTop))
                {
                    collection.Add(bottomLayer);
                    // replace white with transparency
                    topLayer.Transparent(new MagickColor("#FFFFFF"));
                    topLayer.Lower(2);
                    collection.Add(topLayer);

                    var finalImage = collection.Mosaic(); 

                    finalImage.Write(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "output.png"));
                }
            }
On the line
var finalImage = collection.Mosaic(); 
I receive the error:
System.AccessViolationException
Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

Stack Trace:
at std.list<Magick::Image,std::allocator<Magick::Image> >.clear(list<Magick::Image\,std::allocator<Magick::Image> > )
at ImageMagick.MagickImageCollection.Merge(Image
image, LayerMethod method)
at ImageMagick.MagickImageCollection.Mosaic()
at MagickTester.Program.Main(String[] args) in c:\Users\jeffn\Documents\Visual Studio 2013\Projects\MagickTest\MagickTester\Program.cs:line 41


The odd thing is, if I continue past the error, the code seems to still work. The image "output.png" is still created and the overlay functions correctly.

Any idea what is going on?

Here are the images I was using to test with.
https://dl.dropboxusercontent.com/u/78912/checkStock.gif
https://dl.dropboxusercontent.com/u/78912/1.png

ps:
I would include the MagickNET Log output, but when I attempt to turn on the logging, I receive the error:
"System.TypeInitializationException"
"The type initializer for 'ImageMagick.MagickNET' threw an exception."
Stack Trace:
at Magick.EnableOpenCL(Boolean )
at ImageMagick.MagickNET.SetUseOpenCL(Boolean value)
at ImageMagick.MagickNET..cctor()
Coordinator
Jul 7, 2014 at 6:26 PM
I cannot reproduce the access violation with your code. It might be related to the crash inside the 'EnableOpenCL' method. Can you post the InnerException of the TypeInitializationException?
Jul 7, 2014 at 6:42 PM
You bet.
Internal Exception is:

"External component has thrown an exception."
Stack Trace:
at Magick.EnableOpenCL(Boolean )
at ImageMagick.MagickNET.SetUseOpenCL(Boolean value)
at ImageMagick.MagickNET..cctor()


Some more info regarding the first exception ("System.AccessViolationException").

I only receive that once per launch of my application.
The first time I make that call to collection.Mosaic() I receive the error, but if I execute that same code immediately afterward, without restarting my app, then I do not receive any errors.

Thanks!
Coordinator
Jul 7, 2014 at 7:00 PM
Your problem might be related to this issue: https://magick.codeplex.com/discussions/542550. Do you have the same '13-12_mobility_win7_win8_64_dd_ccc_whql' driver?

Can you get me a crashdump (http://msdn.microsoft.com/en-us/library/d5zhxt22.aspx)? You can contact me trough CodePlex if you don't want to share it publicly.
Jul 7, 2014 at 7:17 PM
I do not have that driver. (I have an nvidia video card)

I'll send you a link to the dump file via CodePlex.

Thanks!
Jul 7, 2014 at 7:29 PM
After reading through the other discussion, I noted your suggestion to turn on "native code debugging" to get a better dump file. (I'll send you that dump file in a sec).

When I did that, I now receive another error prior to the TypeInitializationException.

This error is : "First-chance exception at 0x000007FDD9024C39 (ntdll.dll) in MagickTester.exe: 0xC0000008: An invalid handle was specified.

If I continue after that error, then I get the TypeInitializationException, and the app crashes.

I'll send you that dump file.
Thanks!
Coordinator
Jul 14, 2014 at 9:34 PM
After continuing this conversation by e-mail we figured out that the video card driver only supported OpenCL 1.0. ImageMagick needs at least 1.1 but there was no check for it. I have added a patch to ImageMagick that checks the OpenCL version.