Jump to content
  • Advertisement
Sign in to follow this  
YellowShadow

Win32 Heap Error with SDL App

This topic is 3323 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey guys, I'm getting a Win32 Heap Error when I quit my SDL application. Here is the exact error:
Windows has triggered a breakpoint in GunKnights.exe.

This may be due to a corruption of the heap, which indicates a bug in GunKnights.exe or any of the DLLs it has loaded.

This may also be due to the user pressing F12 while GunKnights.exe has focus.

The output window may have more diagnostic information.
I don't know how to fix this because I have never gotten this before. I can post the source code if needed. If anyone can help me fix this, that would be great. Thanks, YellowShadow

Share this post


Link to post
Share on other sites
Advertisement
You are probably trashing memory somewhere by writing past the end of an array or something like that. Try examining your code for errors, and perhaps try commenting out parts of your code to narrow down what is causing it. MS also provide some functions that help debug memory issues, such as _CrtCheckMemory.

Share this post


Link to post
Share on other sites
Okay, I found the error. If I comment out the line in SDL_FreeSurface(buffer) in my Game class's deconstructor I don't get that error.

Is there anyway to avoid that error and leave that line of code in the Game's deconstructor?

Share this post


Link to post
Share on other sites
Tell us more about your class. What is "buffer"? Is it allocated correctly? You aren't double-freeing it, are you?

Share this post


Link to post
Share on other sites
Here is the classes's code.

#include "Game.h"

Game::Game()
{
done = false;
fullScreen = false;
maxFPS = 60;
screenWidth = 1024;
screenHeight = 728;
screenBPP = 32;
SDL_Init(SDL_INIT_EVERYTHING);
SDL_WM_SetCaption("GunKnights Alpha Build", NULL);
buffer = NULL;
state = GameMenu;

if (fullScreen)
buffer = SDL_SetVideoMode(screenWidth, screenHeight, screenBPP, SDL_SWSURFACE | SDL_FULLSCREEN);
else
buffer = SDL_SetVideoMode(screenWidth, screenHeight, screenBPP, SDL_SWSURFACE);

if (buffer == NULL)
std::cerr << "Error initializing the buffer\n" << std::endl;
}

Game::~Game()
{
//SDL_FreeSurface(buffer);
}

void Game::ToggleFullScreen()
{
if (fullScreen)
{
fullScreen = false;
buffer = SDL_SetVideoMode(screenWidth, screenHeight, screenBPP, SDL_SWSURFACE);
}
else
{
fullScreen = true;
buffer = SDL_SetVideoMode(screenWidth, screenHeight, screenBPP, SDL_SWSURFACE | SDL_FULLSCREEN);
}
}

void Game::SetWindowCaption(std::string caption)
{
SDL_WM_SetCaption(caption.c_str(), NULL);
}

void Game::Update()
{
SDL_Flip(buffer);
}

void Game::Quit()
{
done = true;
}

void Game::Done(bool val)
{
done = val;
}

void Game::State(GameState val)
{
state = val;
}

int Game::ScreenWidth() { return screenWidth; }
int Game::ScreenHeight() { return screenHeight; }
int Game::ScreenBPP() { return screenBPP; }
int Game::FPS() { return maxFPS; }
bool Game::Done() { return done; }
GameState Game::State() { return state; }


I don't know what I'm doing wrong.

Share this post


Link to post
Share on other sites
The documentation for SDL_SetVideoMode() says:
Quote:

The returned surface is freed by SDL_Quit and must not be freed by the caller.


Also, you can rewrite the following:

if (fullScreen)
buffer = SDL_SetVideoMode(screenWidth, screenHeight, screenBPP, SDL_SWSURFACE | SDL_FULLSCREEN);
else

buffer = SDL_SetVideoMode(screenWidth, screenHeight, screenBPP, SDL_SWSURFACE);


as:

Uint32 flags = SDL_SWSURFACE;

if (fullScreen)
{
flags |= SDL_FULLSCREEN;
}

buffer = SDL_SetVideoMode(screenWidth, screenHeight, screenBPP, flags);

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!