Sign in to follow this  
Robin04

SDL Memory Leak

Recommended Posts

Robin04    142

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);
}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this