SDL_Image causing heap corruption


I’ve been taking my first forays into OpenGL programming, and have been using SDL2 to this end. Yesterday, I installed SDL_Image and replaced my call to SDL_LoadBMP() with IMG_Load():

//SDL_Surface* NewSurface = SDL_LoadBMP(;
SDL_Surface* NewSurface = IMG_Load(;
// NewSurface->pixels is copied to VRAM and its format is checked, but no modifications are made to the data.

However, on the call to SDL_FreeSurface(), Visual Studio gives me a heap corruption error. Debugging as best as I could manage (even substituting the default memory management functions with my own to track when/where allocations were made), there didn’t appear to be any obvious mismanagement of memory. However, without failure, the program will freeze up with the error message every time. I’ve narrowed down the source of the failure to line 635 of SDL_pixels.c:

if (format->palette) {
SDL_free(format); // Here is where it crashes.

The most vexing part of this problem is that I can’t reproduce it elsewhere; using a new solution with a minimalistic image-loading setup, SDL_image works as intended.

SDL and SDL_Image are both compiled as x64 dynamic libraries from vcpkg under MSVC2019. The exact error message given is Unhandled exception at 0x00007FFEFCCB9229 (ntdll.dll) in <Project>.exe: 0xC0000374: A heap has been corrupted (parameters: 0x00007FFEFCD227F0). No multithreading is in use, and the project is set to compile as a 64-bit executable in Debug mode.

Any input would be appreciated, as I’ve exhausted all my usual debugging tricks trying to find a reason for this. Thank you.

I found the solution.

vcpkg creates separate folders for debug and release versions of installed libraries. I was linking to the release libraries, which somehow worked for SDL2, but not SDL2_image. Linking to the appropriate libraries solved the heap corruption.