SDL returning status 1

Started by
4 comments, last by BigFreak 17 years, 4 months ago
I'm making a Tetris clone in SDL and have most of it working. However it always returns 1 when I try to exit. My main() function never returns 1, so I assume it's SDL doing it. Is there any way to find out why the 1 was returned (I'd paste my code but it's several hundred lines long). Also after about a minute of the program running it starts to intermitently slow down. I first assumed this would be a memory problem but I'm not dynamically allocating memory anywhere. Any ideas? Thanks a lot.
Advertisement
Quote:Is there any way to find out why the 1 was returned...
You might try the SDL function SDL_GetError().
Is there a file in the same directory called "stdout/stderr".txt that has the line "segfault, SDL parachute deployed" in it?

If so you need to run your program with a debugger to find where its crashing.
Rightyoh. There was nothing in either text file, but I think I figured it out. I was returning out of the main loop before my Game object went out of scope thus its destructor was not being called, thus no clean up was being performed, so I just allocated it with new and am calling delete for the clean up. It's still running really slow after it's been open for a while so...would that be a sign of a memory leak?
Um, you must be doing something wrong, or have some conceptual problem, or both.

First of all, what's your evidence for "the program returning 1"?

Second, if the object is scoped to some loop within the function, then breaking out of that loop (even with a 'return' or by throwing an exception) will still cause the destructor to be called. That's a large part of the reason why we use destructors in the first place.

Third, again, if it's scoped to some loop within the function, and you switch it to a pointer, then you almost certainly are allocating it every time through the loop, which certainly will cause a memory leak (unless you 'delete' the pointer at the end of the loop, and also before any "early exits": the "destructor is still called", but it's the "destructor" *of the pointer*, which does nothing. Although I believe primitives are explicitly treated as special cases instead...). And yes, a slowdown over time would be good evidence for having created such a problem.

Any chance we could see some code?
Quote:First of all, what's your evidence for "the program returning 1"?

"Process terminated with status 1" in Code::Blocks' build log.

I went from:

int main(int argc, char* args[])
{
Game game;
game.run();
return 0;
}

to:

int main(int argc, char* args[])
{
Game* game = new Game();
game->run();
delete game;
return 0;
}

...which no longer returns a 1. This is the main run() function for my Game class (yeah, I know it's sloppy and I need to make a few more functions out of it, but I was gonna get round to that):
void Game::run(){	bool running = true;	int time = m_window.getTicks();	m_window.loadFont("arial.ttf", 20);	std::stringstream text;	bool leftButton = false;	bool rightButton = false;	bool downButton = false;	while (running)	{		while (m_window.pollEvent())		{			switch (m_window.getEvent()->type)			{			case SDL_QUIT:				running = false;				break;			case SDL_KEYDOWN:				switch (m_window.getEvent()->key.keysym.sym)				{				case SDLK_UP:					m_state.rotatePiece();					break;				case SDLK_DOWN:					downButton = true;					break;				case SDLK_LEFT:					leftButton = true;					break;				case SDLK_RIGHT:					rightButton = true;					break;				default:					break;				}				break;			case SDL_KEYUP:				switch (m_window.getEvent()->key.keysym.sym)				{				case SDLK_DOWN:					downButton = false;					break;				case SDLK_LEFT:					leftButton = false;					break;				case SDLK_RIGHT:					rightButton = false;					break;				default:					break;				}			}			if (!running)			{				break;			}		}		if (downButton && m_window.getTicks() >= time + 50)		{			m_state.lowerPiece();			time = m_window.getTicks();		}		if (leftButton && m_window.getTicks() >= time + 100)		{			m_state.movePieceLeft();			time = m_window.getTicks();		}		if (rightButton && m_window.getTicks() >= time + 100)		{			m_state.movePieceRight();			time = m_window.getTicks();		}		m_state.advance();		m_window.clear(255, 255, 255);		drawPiece();		drawBoard();		drawGrid();		text << "Level: " << m_state.getLevel();		m_window.drawText(text.str(), 0, 0);		text.str(std::string());		text << "Lines: " << m_state.getLines();		m_window.drawText(text.str(), 0, 20);		m_window.flip();		text.str(std::string());	}}

This topic is closed to new replies.

Advertisement