SDL Memory Leak

Started by
1 comment, last by Robin04 9 years, 7 months ago

Hi!

I've been developing my Pong game in SDL for about a week or two. Now i checked if there is any memory leak in my program, and I realized that, there is. I figured out that the leak is in the Draw function of the ScoreBoard class that is used to handle the points of the players. I tried several solutions like double-checking that all my SDL_Surfaces are freed, but nothing helped. I paste the Draw function here for you, but if you can't find the problem I can put the whole class as well.


void ScoreBoard::Draw(SDL_Renderer* p_Renderer)
{
	m_Surface = TTF_RenderText_Solid(m_Font, std::to_string(m_Point[LEFT]).c_str(), m_Color);
	m_Texture = SDL_CreateTextureFromSurface(p_Renderer, m_Surface);
	SDL_FreeSurface(m_Surface);

	SDL_QueryTexture(m_Texture, 0, 0, &m_srcRect.w, &m_srcRect.h);
	m_destRect.w = m_srcRect.w;
	m_destRect.h = m_srcRect.h;
	m_destRect.x = WINDOW_WIDTH / 4;
	m_destRect.y = 50;
	SDL_RenderCopy(p_Renderer, m_Texture, &m_srcRect, &m_destRect);

	m_Surface = TTF_RenderText_Solid(m_Font, std::to_string(m_Point[RIGHT]).c_str(), m_Color);
	m_Texture = SDL_CreateTextureFromSurface(p_Renderer, m_Surface);
	SDL_FreeSurface(m_Surface);

	SDL_QueryTexture(m_Texture, 0, 0, &m_srcRect.w, &m_srcRect.h);
	m_destRect.w = m_srcRect.w;
	m_destRect.h = m_srcRect.h;
	m_destRect.x = WINDOW_WIDTH / 2 + WINDOW_WIDTH / 4;
	m_destRect.y = 50;
	SDL_RenderCopy(p_Renderer, m_Texture, &m_srcRect, &m_destRect);

	SDL_DestroyTexture(m_Texture);
}
Advertisement

You have two calls to SDL_CreateTextureFromSurface but only one call to SDL_DestroyTexture.

C++: A Dialog | C++0x Features: Part1 (lambdas, auto, static_assert) , Part 2 (rvalue references) , Part 3 (decltype) | Write Games | Fix Your Timestep!

It works! Thank you very much! :)

This topic is closed to new replies.

Advertisement