Pre-Main() stack-overflow with SDL

Started by
34 comments, last by Twiggy 19 years ago
A'right. What the hell just happened? A minute ago everything was all right on my shiny, well-progressing SDL project. Suddenly, even before the first line of code in the main() scope, I get an 'illegal action' windows thingy. If i try to debug the project, it says that I have a stack overflow. I tried tracing back and returning to the state when everything worked fine, and the problem still occurs. I tried that on two different computers, with VS6 and VS2003\VC++ 2003. I can't find the source of the problem, and I don't even understand where does the stack overflow comes from. Can somebody please help me? :(
___________________________"Peg, is there a certain reason that cactus is standing in the place where my alarm clock should be?"
Advertisement
It probably happens in the constructor of a global variable, since they are called before the main function is called. It might be in one of the sdl libraries, but I doubt that, it's most likely in one of your own global variables, so check them.
Are you using global variables? Are those variables classes, and do you call recursive functions during construction, or are you "just" creating a huge number of objects?
Here is the code of the whole main.cpp file.

#include <SDL.h>//#include "InfoBlock.h"#include "GameCore.h"//#include "Art//font.h"int main(int argc, char *argv[]){	SDL_Event event;		/*InfoBlock *info = new InfoBlock;	info->SetResolutionParamaters(640,480,24);	info->SetMapRect(0,640,0,480);	info->SetControlsRect(0,0,0,0);	info->SetTileSize(16);	info->SetMapSize(100,100);	info->SetFLTile( 1 << 0 , 19 << 0 );	delete info;*/	GameCore GameEngine;		GameEngine.LoadDatFile("Art\\maptiles.dat");			GameEngine.LoadMap(); 	if( GameEngine.SetVisibleArea(0,2, true) == 0)	{		printf("Error setting visible area.");		exit(0);	}	/*	// ****	GameEngine.LoadDatFile("Art\\font.dat");	KrFontResource *font = GameEngine.engine->Vault()->GetFontResource("CONSOLE");		KrTextBox *text = new KrTextBox(font, 160, 15, 2);	GameEngine.engine->Tree()->AddNode(0,text);		text->SetPos(480,450);	text->SetVisible(true);	text->SetTextChar("Charlie. - v0.4 Beta  :)",0);	// ****** */


GameEngine.Draw();

SDL_Delay(1000);

SDL_PollEvent(&event);
do
{
while(event.type!=SDL_KEYDOWN && event.type!=SDL_MOUSEMOTION)
SDL_PollEvent(&event);

GameEngine.ScrollMap(&event);
GameEngine.Draw();

event.type = 0;
}while(event.key.keysym.sym != SDLK_ESCAPE);

SDL_Delay(1000);

return 1;
}


FYI, everything in the main used to work until suddenly the god of war bumped into my pc, so don't try to search for the errors inside the scope. The overflow even hints it's outside of it.
___________________________"Peg, is there a certain reason that cactus is standing in the place where my alarm clock should be?"
There aren't any globals in this file. Is this the only file of your project? If not, you should look in the other files for global variables, try to remark them out, and see if you still get the error.
And then I sat there, wondering: "Jeez, I think they would also like to see what the hell 'GameCore.h' is?"

#ifndef __GAMECORE_H#define __GAMECORE_H#pragma comment(lib, "sdl.lib")#pragma comment(lib, "sdlmain.lib")#pragma comment(lib, "sdl_image.lib")#pragma comment(lib, "opengl32.lib")#pragma comment(lib, "glu32.lib")#pragma comment(lib, "engine.lib")#pragma warning( disable : 4530 )#pragma warning( disable : 4786 )#include <stdlib.h>#include <string>#include <windows.h>#include <iostream>#include <fstream>#include <SDL.h>#include <engine/kyra.h>#include "MapTile.h"#include "General.h"//#include "InfoBlock.h"// Art *.dat files:#include "art\\maptiles.h"// ******using namespace std;const int ResolutionX = 640;const int ResolutionY = 480;const int ResolutionBPP = 24; // Bits per pixel (color depth).const int Screen_Map_Rect_X1 = 0;const int Screen_Map_Rect_X2 = 640;const int Screen_Map_Rect_Y1 = 0;const int Screen_Map_Rect_Y2 = 480;const int Screen_Controls_Rect_X1 = 0;const int Screen_Controls_Rect_X2 = 640;const int Screen_Controls_Rect_Y1 = 336;const int Screen_Controls_Rect_Y2 = 480;const int TileSize = 16; // Tiles have (x,x) size (Simetrical square).const int Map_Size_X = 100;const int Map_Size_Y = 100;const int Visible_Map_Size_X = Map_Size_X / TileSize;const int Visible_Map_Size_Y = Map_Size_Y / TileSize;const U32 FirstTile = MapTile_Grass;const U32 LastTile = MapTile_Forest_North_West;enum TerrainType { M,F };enum ScrollDirection { North, NorthEast, East, SouthEast,						South, SouthWest, West, NorthWest };class GameCore // Master-class for handling and executing the major graphic and gameplay parts of the project's code.{public:		// Variables:	MapTile *Map[2000][2000]; // The map tiles array.	MapTile *VisibleMap[300][300];			// Functions:		GameCore(); // Constructor();			~GameCore(); // ~ Destructor :]	// Graphic Functions:	void LoadDatFile(const char *dat_filename); // Loads an image (Sprite, tile, etc.) resource to the engine's vault.		KrSpriteResource *GetKrSpriteResource(U32 Resource); // Fetches a sprite resource from the *.dat file.	KrTileResource *GetKrTileResource(U32 Resource); // Fetches a sprite resource from the *.dat file.		KrSprite *GetKrSprite(KrSpriteResource *Resource); // Creates a ready-to-use sprite variable.	KrTile *GetKrTile(KrTileResource *Resource); // Creates a ready-to-use tile variable.	//  ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,//  *** Make sure to #include the header file (*.h) of the sprite at the head of the page! ***//  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^		// Map handling functions:	void LoadMap(); // Loads the map to the MapTile 'Map' buffer.	void Draw(); // Draws everything to the screen (Graphic execution procedure).	void ScrollMap(SDL_Event *event); // Scrolls the map according to the given movement event.	int SetVisibleArea(int x, int y, bool firsttime); // firsttime = false		private:	// Variables:	SDL_Surface *screen; // the surface to be drawn into. This is handled by the Kyra (tm) engine.		KrEngine *engine; // Our beloved, almighty engine :) This is the core of the graphics stuff.			// Importnant graphic and in-game handling paramaters:	/*int ResolutionX; // Resolution of the screen.	int ResolutionY;	int ResolutionBPP; // Bits per pixel (color depth).	int Screen_Map_Rect_X1; // X,Y paramaters for the map part of the screen.	int Screen_Map_Rect_X2;	int Screen_Map_Rect_Y1;	int Screen_Map_Rect_Y2;		int Screen_Controls_Rect_X1; // X,Y paramaters for the controls part of the screen.	int Screen_Controls_Rect_X2;	int Screen_Controls_Rect_Y1;	int Screen_Controls_Rect_Y2;	int TileSize; // Tiles have (x,x) size (Simetrical square).	int Map_Size_X; // Size of the map, in tiles.	int Map_Size_Y;	int Visible_Map_Size_X; // The actual amount of tiles seen on the screen, since the..	int Visible_Map_Size_Y; // .. original map is too large to be blitted all at once.	U32 FirstTile; // Map handling paramater.	U32 LastTile; // Map handling paramater.*/




// Functions:

// Screen initialization functions:

void Init_SDL_Port(); // Returns a ready-to-use SDL_Surface pointer.


// Map initialization functions:

U32 DecideTerrain(int x, int y, TerrainType type); // Returns the tile need to be drawn at a specific coordinate (in U32 form).


// Map handling functions:

void ScrollNorth();
void ScrollWest();
void ScrollEast();
void ScrollSouth();

bool AnyTerrain(int x, int y, TerrainType type);

bool NorthWest(int x, int y, TerrainType type);
bool North(int x, int y, TerrainType type);
bool NorthEast(int x, int y, TerrainType type);
bool East(int x, int y, TerrainType type);
bool SouthEast(int x, int y, TerrainType type);
bool South(int x, int y, TerrainType type);
bool SouthWest(int x, int y, TerrainType type);
bool West(int x, int y, TerrainType type);
bool Center(int x, int y, TerrainType type);

};

#endif




General.h and MapTile.h aren't the problem. They haven't been touched for over a month, so they're not the problem.

Also, could someone please explain to me what are those 'pragma' stuff? Someone told me to put them there just cause it helps to make stuff work. I would also like to know how does it make stuff work. Thanks! :)
___________________________"Peg, is there a certain reason that cactus is standing in the place where my alarm clock should be?"


MapTile *Map[2000][2000]; // The map tiles array.

MapTile *VisibleMap[300][300];

it's these lines. Those arrays will both be created on the stack, but they are way too big for that.
2000 * 2000 * sizeof(Map*) = 16MB, while the stack is only 1 MB by default. You will have to allocate the memory with new or use a std::vector or so.
#pragma comment (lib, file) tells the compiler to include certain libraries.
#pragma warning (disable, nr) tells the compiler not to show certain warnings.

More information can be found here
Your Map array alone is about 16MB big, that's definitely to big for the stack. You should use new[]/delete[] to put on the heap.
And those #pragma's are sepcial compiler options.
#pragma comment(lib, "sdl.lib" ) i.e. tells VC++ to link your program with sdl.lib.
MapTile *Map[2000][2000]; // The map tiles array.

MapTile *VisibleMap[300][300];

Are you sure you need a 3 dimensional array?

This topic is closed to new replies.

Advertisement