Search code examples
cmemoryfreeallocationcorruption

C "double free or corruption" error


This the short code, that produces a double free or corruption error.

SDL_Surface *surface;
SDL_Surface *surface2;
surface = NULL;
surface2 = SDL_LoadBMP("someImg.bmp");
surface = surface2;
SDL_FreeSurface(surface);
SDL_FreeSurface(surface2);

I don't understand, why I can't free second surface before I free first.


Solution

  • Both variables surface and surface2 point to the same object. You are effectively freeing twice the same object.