Home > Software engineering >  0xC0000005: Access violation writing location 0x0000000000000008
0xC0000005: Access violation writing location 0x0000000000000008

Time:10-09

I have this simple project that uses imagemagick on windows:

#include <Magick  .h>
#include <iostream>

using namespace std;
using namespace Magick;

int main() {
    cout << "Hello, World!" << endl;
    string inputFileName =
        "D:\\magick\\pic2.jpg";
    string outputFileName = "D:\\magick\\pic_resized.png";
    int width = 800;
    int height = 600;

    try{
        Image image;
        image.read(inputFileName);
        Geometry geometry(width, height);
        geometry.aspect(false);
        image.resize(geometry);
        image.write(outputFileName);
        Blob b;
        image.write(&b);
        cout << "data size is " << b.length() << endl;
    }
    catch (...) {
        cout << "error occurred";
    }
    return 0;
}

When I run it, I get an exception at the line image.read(inputFileName);:

Exception thrown at 0x00007FFC98B7A59D (ntdll.dll) in demo.exe: 0xC0000005: Access violation writing location 0x0000000000000008.

I read online that this exception is due to missing dlls, but I don't understand what is this missing dll?

I am using cmake and already the project builds normally.

my CMakeLists.txt:

cmake_minimum_required(VERSION 3.23)

project(demo-app)

set(CMAKE_CXX_STANDARD 23)

set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

file(GLOB SOURCE_FILES src/*.cpp)

add_executable(demo ${SOURCE_FILES})

include_directories(src)

#ImageMagick
add_definitions( -DMAGICKCORE_QUANTUM_DEPTH=16 )
add_definitions( -DMAGICKCORE_HDRI_ENABLE=0 )
include_directories(deps/ImageMagick/include)
target_link_directories(demo PRIVATE deps/ImageMagick/lib)
target_link_libraries(demo 
    "CORE_RL_Magick  _"
    "CORE_RL_MagickCore_"
    "CORE_RL_MagickWand_"
)

and the project structure is very simple:

enter image description here

And this is the log when I run the app and get the error ( I forced it to quit as it breaks on the exception):

-------------------------------------------------------------------
You may only use the C/C   Extension for Visual Studio Code
with Visual Studio Code, Visual Studio or Visual Studio for Mac
software to help you develop and test your applications.
-------------------------------------------------------------------
Loaded 'D:\dev_haidar\c  \demo_old\build\demo.exe'. Module was built without symbols.
Loaded 'C:\Windows\System32\ntdll.dll'.
Loaded 'C:\Windows\System32\kernel32.dll'.
Loaded 'C:\Program Files\Avast Software\Avast\aswhook.dll'.
Loaded 'C:\Windows\System32\KernelBase.dll'.
Loaded 'C:\Windows\System32\ucrtbase.dll'.
Loaded 'C:\Windows\System32\msvcp140.dll'.
Loaded 'C:\Windows\System32\vcruntime140.dll'.
Loaded 'C:\Windows\System32\vcruntime140_1.dll'.
Loaded 'C:\Program Files\ImageMagick-7.1.0-Q16\CORE_RL_Magick  _.dll'. Module was built without symbols.
Loaded 'C:\Program Files\ImageMagick-7.1.0-Q16\CORE_RL_MagickCore_.dll'. Module was built without symbols.
Loaded 'C:\Program Files\ImageMagick-7.1.0-Q16\CORE_RL_MagickWand_.dll'. Module was built without symbols.
Loaded 'C:\Windows\System32\user32.dll'.
Loaded 'C:\Windows\System32\vcomp140.dll'.
Loaded 'C:\Windows\System32\win32u.dll'.
Loaded 'C:\Windows\System32\gdi32.dll'.
Loaded 'C:\Windows\System32\gdi32full.dll'.
Loaded 'C:\Windows\System32\msvcp_win.dll'.
Loaded 'C:\Windows\System32\advapi32.dll'.
Loaded 'C:\Windows\System32\msvcrt.dll'.
Loaded 'C:\Windows\System32\sechost.dll'.
Loaded 'C:\Windows\System32\rpcrt4.dll'.
Loaded 'C:\Windows\System32\ws2_32.dll'.
Loaded 'C:\Program Files\ImageMagick-7.1.0-Q16\CORE_RL_freetype_.dll'. Module was built without symbols.
Loaded 'C:\Program Files\ImageMagick-7.1.0-Q16\CORE_RL_bzlib_.dll'. Module was built without symbols.
Loaded 'C:\Program Files\ImageMagick-7.1.0-Q16\CORE_RL_lcms_.dll'. Module was built without symbols.
Loaded 'C:\Program Files\ImageMagick-7.1.0-Q16\CORE_RL_lqr_.dll'. Module was built without symbols.
Loaded 'C:\Program Files\ImageMagick-7.1.0-Q16\CORE_RL_xml_.dll'. Module was built without symbols.
Loaded 'C:\Program Files\ImageMagick-7.1.0-Q16\CORE_RL_zlib_.dll'. Module was built without symbols.
Loaded 'C:\Program Files\ImageMagick-7.1.0-Q16\CORE_RL_harfbuzz_.dll'. Module was built without symbols.
Loaded 'C:\Program Files\ImageMagick-7.1.0-Q16\CORE_RL_fribidi_.dll'. Module was built without symbols.
Loaded 'C:\Program Files\ImageMagick-7.1.0-Q16\CORE_RL_glib_.dll'. Module was built without symbols.
Loaded 'C:\Windows\System32\shell32.dll'.
Loaded 'C:\Windows\System32\ole32.dll'.
Loaded 'C:\Windows\System32\combase.dll'.
Loaded 'C:\Windows\System32\dnsapi.dll'.
Loaded 'C:\Windows\System32\IPHLPAPI.DLL'.
Loaded 'C:\Windows\System32\imm32.dll'.
Loaded 'C:\Windows\System32\nsi.dll'.
Hello, World!
Exception thrown at 0x00007FFC98B7A59D (ntdll.dll) in demo.exe: 0xC0000005: Access violation writing location 0x0000000000000008.
Exception thrown at 0x00007FFC98B7A59D (ntdll.dll) in demo.exe: 0xC0000005: Access violation writing location 0x0000000000000008.
Exception thrown at 0x00007FFC98B7A59D (ntdll.dll) in demo.exe: 0xC0000005: Access violation writing location 0x0000000000000008.
Exception thrown at 0x00007FFC98B7A59D (ntdll.dll) in demo.exe: 0xC0000005: Access violation writing location 0x0000000000000008.
Exception thrown at 0x00007FFC98B7A59D (ntdll.dll) in demo.exe: 0xC0000005: Access violation writing location 0x0000000000000008.
Exception thrown at 0x00007FFC98B7A59D (ntdll.dll) in demo.exe: 0xC0000005: Access violation writing location 0x0000000000000008.
Exception thrown at 0x00007FFC98B7A59D (ntdll.dll) in demo.exe: 0xC0000005: Access violation writing location 0x0000000000000008.
Exception thrown at 0x00007FFC98B7A59D (ntdll.dll) in demo.exe: 0xC0000005: Access violation writing location 0x0000000000000008.
Exception thrown at 0x00007FFC98B7A59D (ntdll.dll) in demo.exe: 0xC0000005: Access violation writing location 0x0000000000000008.
Exception thrown at 0x00007FFC98B7A59D (ntdll.dll) in demo.exe: 0xC0000005: Access violation writing location 0x0000000000000008.
Exception thrown at 0x00007FFC98B7A59D (ntdll.dll) in demo.exe: 0xC0000005: Access violation writing location 0x0000000000000008.
Exception thrown at 0x00007FFC98B7A59D (ntdll.dll) in demo.exe: 0xC0000005: Access violation writing location 0x0000000000000008.
The program '[8620] demo.exe' has exited with code 0 (0x0).

Can someone tell me how to solve this?

Note: the include and lib folders under deps/ImageMagick were copy/pasted from the installation of imagemagick ImageMagick-7.1.0-49-Q16-x64-dll.exe from the official site.

CodePudding user response:

According to the Documentation over on the site you linked to, the suggested usage is:

Note, under Windows (and possibly the Mac) it may be necessary to initialize the ImageMagick library prior to using the Magick library. This initialization is performed by passing the path to the ImageMagick DLLs (assumed to be in the same directory as your program) to the InitializeMagick() function call. This is commonly performed by providing the path to your program.

So you'll have to properly initialize the library before using it, to get your program to run correctly. This initialization can be accomplished like so:

int main(int argc, char* argv[]) {
    InitializeMagick(*argv);

    //Your code with Magick   operations here.

    return 0;
}
  • Related