SDL Application crash

Hi all- first post…

Anyway, I’ve been following the Lazy Foo’ Productions SDL tutorials (I won’t link to it, might trigger the spam filter), and I’m stuck at lesson 3. It compiles fine (using Visual Studio Express 2010), but I’ve been getting a weird crash whenever I try and run the program, and I get these errors from the debug log:

Code:
First-chance exception at 0x68126030 in SDL_Projects.exe: 0xC0000005: Access violation reading location 0x00000004.
Unhandled exception at 0x68126030 in SDL_Projects.exe: 0xC0000005: Access violation reading location 0x00000004.
The program ‘[7944] SDL_Projects.exe: Native’ has exited with code -1073741819 (0xc0000005).

Now, I’ve deviated from the tutorial quite a bit, and since it won’t run at all, I’m not sure if other problems exist, like in my image loading functions, but it seems to me that it’s happening even when following the tutorial word-for-word. Any ideas as to what’s causing it? Also, one of my functions is attempting to load multiple images at once. Is there a better way to do that than with arrays? My code is below:

Code:

#include <SDL.h>
#include <SDL_image.h>

#define DEBUG
#define SX 640
#define SY 480
#define SBPP 32

#ifdef DEBUG
#include
#include
#endif

SDL_Surface *image = NULL;
SDL_Surface *screen = NULL;

#ifdef DEBUG
std::ofstream debugLog;
#endif

SDL_Surface loadImage(char filename) {
SDL_Surface* rawImg = NULL;
SDL_Surface* optImg = NULL;

rawImg = IMG_Load(filename);

if(rawImg != NULL)	{
	optImg = SDL_DisplayFormat(rawImg);
}

#ifdef _DEBUG_
	else	{
		debugLog << "Error: Could not load image. filepath: " << filename << std::endl;
	}
#endif

SDL_FreeSurface(rawImg);

return optImg;

}

void blitToPos(int x, int y, SDL_Surface* src, SDL_Surface* dest) {
SDL_Rect offset;

offset.x = x;
offset.y = y;

SDL_BlitSurface(src, NULL, dest, &offset);

}

bool init(SDL_Surface* screen, int screenX = 640, int screenY = 480, int bitsPerPixel = 32, char* windowName = “SDL Window”) {
if(SDL_Init(SDL_INIT_EVERYTHING) == -1) {
#ifdef DEBUG
debugLog << “Error initializing SDL.\n”;
#endif

	return false;
}

screen = SDL_SetVideoMode(screenX, screenY, bitsPerPixel, SDL_SWSURFACE);

if(screen = NULL)	{
	#ifdef _DEBUG_
		debugLog << "Error setting up screen.\n";
	#endif

	return false;
}

SDL_WM_SetCaption(windowName, NULL);

return true;

}

bool loadImgs(char* files, SDL_Surface* surfaces[]) {
char* fileToLoad = “”;
int i = 0;

fileToLoad = strtok(files, ",");

#ifdef _DEBUG_
	debugLog << "fileToLoad: " << fileToLoad << std::endl;
#endif

while(fileToLoad != NULL)	{
	surfaces[i] = IMG_Load(fileToLoad);
	fileToLoad = strtok(NULL, ",");
	i++;

	if(surfaces[i] == NULL)	{
		#ifdef _DEBUG_
			debugLog << "Error loading image at filepath \"" << fileToLoad << "\" to surface at pos " << i << ".\n";
		#endif
		return false;
	}
}

return true;

}

void quit(SDL_Surface* surfaces[], int surfaceNum, bool quitSDL = true) {
int i = 0;

while(i <= surfaceNum - 1)	{
	SDL_FreeSurface(surfaces[i]);
	i++;
}

if(quitSDL == true)	{
	SDL_Quit();
}

}

int main(int argc, char* args[]) {
#ifdef DEBUG
debugLog.open(“debugLog.txt”, std::ios::out);
debugLog << “-----BEGIN LOG-----\n”;
#endif

SDL_Surface *surfaces[1] = {image};

init(screen, SX, SY, BPP);

loadImgs("hello.bmp",surfaces);

blitToPos(160, 120, surfaces[0], screen);

if(SDL_Flip(screen) == -1)	{
	return 1;
}

SDL_Delay(2000);

quit(surfaces, 1);

#ifdef _DEBUG_
	debugLog << "-----END   LOG-----\n";
#endif
return 0;

}

Access Violation reading 0x00000004 means that you’ve got a pointer to a struct or object, and you’re attempting to read a member of it, but the pointer is NULL.? They’re usually trivial to find if your debugger is set up correctly; just run under a debugger and when the error occurs, the debugger will take you right to it.? (Sometimes.? For some bizarre reason, Visual Studio’s debugger has capturing this exception turned off by default!? So you might have to enable it.)? When you get there, that’s what to look for: access of a null pointer.________________________________
From: Dr. Quigley <joecool.17.3 at gmail.com>
To: sdl at lists.libsdl.org
Sent: Sunday, February 5, 2012 7:22 PM
Subject: [SDL] SDL Application crash…

Hi all- first post…

Anyway, I’ve been following the Lazy Foo’ Productions SDL tutorials (I won’t link to it, might trigger the spam filter), and I’m stuck at lesson 3. It compiles fine (using Visual Studio Express 2010), but I’ve been getting a weird crash whenever I try and run the program, and I get these errors from the debug log:

Code:
First-chance exception at 0x68126030 in SDL_Projects.exe: 0xC0000005: Access violation reading location 0x00000004.
Unhandled exception at 0x68126030 in SDL_Projects.exe: 0xC0000005: Access violation reading location 0x00000004.
The program ‘[7944] SDL_Projects.exe: Native’ has exited with code -1073741819 (0xc0000005).

Now, I’ve deviated from the tutorial quite a bit, and since it won’t run at all, I’m not sure if other problems exist, like in my image loading functions, but it seems to me that it’s happening even when following the tutorial word-for-word. Any ideas as to what’s causing it? Also, one of my functions is attempting to load multiple images at once. Is there a better way to do that than with arrays? My code is below:

Code:

#include
#include

#define DEBUG
#define SX 640
#define SY 480
#define SBPP 32

#ifdef DEBUG
#include
#include
#endif

SDL_Surface *image = NULL;
SDL_Surface *screen = NULL;

#ifdef DEBUG
std::ofstream debugLog;
#endif

SDL_Surface loadImage(char filename)? ?{
? ?SDL_Surface* rawImg = NULL;
? ?SDL_Surface* optImg = NULL;

? ?rawImg = IMG_Load(filename);

? ?if(rawImg != NULL)? ?{
? ?? ?optImg = SDL_DisplayFormat(rawImg);
? ?}

? ?#ifdef DEBUG
? ?? ?else? ?{
? ?? ?? ?debugLog << "Error: Could not load image. filepath: " << filename << std::endl;
? ?? ?}
? ?#endif

? ?SDL_FreeSurface(rawImg);

? ?return optImg;
}

void blitToPos(int x, int y, SDL_Surface* src, SDL_Surface* dest)? ?{
? ?SDL_Rect offset;? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?

? ?offset.x = x;
? ?offset.y = y;

? ?SDL_BlitSurface(src, NULL, dest, &offset);
}

bool init(SDL_Surface* screen, int screenX = 640, int screenY = 480, int bitsPerPixel = 32, char* windowName = “SDL Window”)? ?{
? ?if(SDL_Init(SDL_INIT_EVERYTHING) == -1)? ?{
? ?? ?#ifdef DEBUG
? ?? ?? ?debugLog << “Error initializing SDL.\n”;
? ?? ?#endif

? ?? ?return false;
? ?}

? ?screen = SDL_SetVideoMode(screenX, screenY, bitsPerPixel, SDL_SWSURFACE);

? ?if(screen = NULL)? ?{
? ?? ?#ifdef DEBUG
? ?? ?? ?debugLog << “Error setting up screen.\n”;
? ?? ?#endif

? ?? ?return false;
? ?}

? ?SDL_WM_SetCaption(windowName, NULL);

? ?return true;
}

bool loadImgs(char* files, SDL_Surface* surfaces[])? ?{
? ?char* fileToLoad = “”;
? ?int i = 0;

? ?fileToLoad = strtok(files, “,”);

? ?#ifdef DEBUG
? ?? ?debugLog << "fileToLoad: " << fileToLoad << std::endl;
? ?#endif

? ?while(fileToLoad != NULL)? ?{
? ?? ?surfaces[i] = IMG_Load(fileToLoad);
? ?? ?fileToLoad = strtok(NULL, “,”);
? ?? ?i++;

? ?? ?if(surfaces[i] == NULL)? ?{
? ?? ?? ?#ifdef DEBUG
? ?? ?? ?? ?debugLog << "Error loading image at filepath “” << fileToLoad << “” to surface at pos " << i << “.\n”;
? ?? ?? ?#endif
? ?? ?? ?return false;
? ?? ?}
? ?}

? ?return true;
}

void quit(SDL_Surface* surfaces[], int surfaceNum, bool quitSDL = true)? ?{
? ?int i = 0;

? ?while(i <= surfaceNum - 1)? ?{
? ?? ?SDL_FreeSurface(surfaces[i]);
? ?? ?i++;
? ?}

? ?if(quitSDL == true)? ?{
? ?? ?SDL_Quit();
? ?}
}

int main(int argc, char* args[])? ?{? ?
? ?#ifdef DEBUG
? ?? ?debugLog.open(“debugLog.txt”, std::ios::out);
? ?? ?debugLog << “-----BEGIN LOG-----\n”;
? ?#endif

? ?SDL_Surface *surfaces[1] = {image};

? ?init(screen, SX, SY, BPP);

? ?loadImgs(“hello.bmp”,surfaces);

? ?blitToPos(160, 120, surfaces[0], screen);
? ?
? ?if(SDL_Flip(screen) == -1)? ?{
? ?? ?return 1;
? ?}

? ?SDL_Delay(2000);

? ?quit(surfaces, 1);

? ?#ifdef DEBUG
? ?? ?debugLog << “-----END? ?LOG-----\n”;
? ?#endif
? ?return 0;
}


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Mason Wheeler wrote:

Access Violation reading 0x00000004 means that you’ve got a pointer to a struct or object, and you’re attempting to read a member of it, but the pointer is NULL. They’re usually trivial to find if your debugger is set up correctly; just run under a debugger and when the error occurs, the debugger will take you right to it. (Sometimes. For some bizarre reason, Visual Studio’s debugger has capturing this exception turned off by default! So you might have to enable it.) When you get there, that’s what to look for: access of a null pointer.

Thank you! I’ll poke around, see if I can find it.

Dunno if this is your issue or not, but you’ve got an assignment inside of
a comparison here:

if(screen = NULL) {
#ifdef DEBUG
debugLog << “Error setting up screen.\n”;
#endif

  return false;

}

Which is setting screen to NULL all of the time.On Mon, Feb 6, 2012 at 9:22 AM, Dr. Quigley <joecool.17.3 at gmail.com> wrote:

**

Mason Wheeler wrote:

Access Violation reading 0x00000004 means that you’ve got a pointer to a
struct or object, and you’re attempting to read a member of it, but the
pointer is NULL. They’re usually trivial to find if your debugger is set up
correctly; just run under a debugger and when the error occurs, the
debugger will take you right to it. (Sometimes. For some bizarre reason,
Visual Studio’s debugger has capturing this exception turned off by
default! So you might have to enable it.) When you get there, that’s what
to look for: access of a null pointer.

Thank you! I’ll poke around, see if I can find it.


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


http://www.blar.net/kurt/blog/

Well, after poking around with the debugger, it seems that the call SDL_Flip() fails, returning -1. And after copying and pasting the tutorial code, just to see if it would work, it failed to run, signaling that I probably did something like install the Image library wrong. After doing that, the tutorial code successfully ran. It seems I forgot to copy over the library files. Whoops. [Embarassed]

Well, now I’m still getting an error when I try my code, this time it’s a linker error:

Code:

1>SDLmain.lib(SDL_win32_main.obj) : error LNK2019: unresolved external symbol _SDL_main referenced in function _main
1>C:\Users[withheld]\Documents\Visual Studio 2010\Projects\OpenGL+SDL\SDL_Projects\Debug\SDL_Projects.exe : fatal error LNK1120: 1 unresolved externals

I couldn’t find anything useful after scouting around. Any idea how to fix this?

are you linking against SDLmain ?On Tue, Feb 7, 2012 at 7:58 PM, Dr. Quigley <joecool.17.3 at gmail.com> wrote:

**
Well, now I’m still getting an error when I try my code, this time it’s a
linker error:

Code:

1>SDLmain.lib(SDL_win32_main.obj) : error LNK2019: unresolved external
symbol _SDL_main referenced in function _main
1>C:\Users[withheld]\Documents\Visual Studio
2010\Projects\OpenGL+SDL\SDL_Projects\Debug\SDL_Projects.exe : fatal error
LNK1120: 1 unresolved externals

I couldn’t find anything useful after scouting around. Any idea how to fix
this?


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

MrOzBarry wrote:

are you linking against SDLmain ?

   	Well, now I'm still getting an error when I try my code, this time it's a linker error:



Code:

1>SDLmain.lib(SDL_win32_main.obj) : error LNK2019: unresolved external symbol _SDL_main referenced in function _main
1>C:\Users[withheld]\Documents\Visual Studio 2010\Projects\OpenGL+SDL\SDL_Projects\Debug\SDL_Projects.exe : fatal error LNK1120: 1 unresolved externals

I couldn’t find anything useful after scouting around. Any idea how to fix this?

Sorry, I know a thing or two about C++, but I’m no expert. Could you clarify what you mean?> On Tue, Feb 7, 2012 at 7:58 PM, Dr. Quigley <@Dr_Quigley (@Dr_Quigley)> wrote:

MrOzBarry wrote:

are you linking against SDLmain ?

   	Well, now I'm still getting an error when I try my code, this time it's a linker error:



Code:

1>SDLmain.lib(SDL_win32_main.obj) : error LNK2019: unresolved external symbol _SDL_main referenced in function _main
1>C:\Users[withheld]\Documents\Visual Studio 2010\Projects\OpenGL+SDL\SDL_Projects\Debug\SDL_Projects.exe : fatal error LNK1120: 1 unresolved externals

I couldn’t find anything useful after scouting around. Any idea how to fix this?

Sorry, I know a thing or two about C++, but I’m no expert. Could you clarify what you mean?> On Tue, Feb 7, 2012 at 7:58 PM, Dr. Quigley <@Dr_Quigley (@Dr_Quigley)> wrote:

Well, before I explain, what platform are you on, and are you using an IDE,
and if so, which one?On Wed, Feb 8, 2012 at 5:56 PM, Dr. Quigley <joecool.17.3 at gmail.com> wrote:

**

MrOzBarry wrote:

are you linking against SDLmain ?

On Tue, Feb 7, 2012 at 7:58 PM, Dr. Quigley <> wrote:

Quote:

Well, now I’m still getting an error when I try my code, this time it’s
a linker error:

Code:

1>SDLmain.lib(SDL_win32_main.obj) : error LNK2019: unresolved external
symbol _SDL_main referenced in function _main
1>C:\Users[withheld]\Documents\Visual Studio
2010\Projects\OpenGL+SDL\SDL_Projects\Debug\SDL_Projects.exe : fatal error
LNK1120: 1 unresolved externals

I couldn’t find anything useful after scouting around. Any idea how to fix
this?

Sorry, I know a thing or two about C++, but I’m no expert. Could you
clarify what you mean?


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

MrOzBarry wrote:

Well, before I explain, what platform are you on, and are you using an IDE, and if so, which one?

I’m on Windows with VC++ 2010 express.

Check out this tutorial:
https://sites.google.com/site/sdlinvc2010/installation-1 with special
attention to Step 4 :slight_smile:

Take care,
-AlexOn Wed, Feb 8, 2012 at 8:52 PM, Dr. Quigley <joecool.17.3 at gmail.com> wrote:

**

MrOzBarry wrote:

Well, before I explain, what platform are you on, and are you using an
IDE, and if so, which one?

I’m on Windows with VC++ 2010 express.


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

MrOzBarry wrote:

Check out this tutorial:
https://sites.google.com/site/sdlinvc2010/installation-1 (https://sites.google.com/site/sdlinvc2010/installation-1)?with special attention to Step 4 :slight_smile:

Take care,
-Alex

Thank you very much for the help, but those steps were part of the tutorial. I followed the step you mentioned, just to be safe, but it didn’t help, sadly. (I suppose I’ve made enough posts that the spam filter won’t be triggered, if any, so I’ve edited my OP to add it.)

I still like to use msv 6.0 but I do look at the new ones and install them
from time to time for testing. I would be sure to install the service pack
and be sure to start from a new location, new project, and check the
compiler enviroment and see that the directories and object/library modules
are listed correctly and such. One simple stupid thing can mess u up. I
wish they would make it more simple with a set of standards so that libs
would simply install themselves and then fully test themselves and run a
diag.On Fri, Feb 10, 2012 at 3:33 PM, Dr. Quigley <joecool.17.3 at gmail.com> wrote:

**

MrOzBarry wrote:

Check out this tutorial:
https://sites.google.com/site/sdlinvc2010/installation-1 with special
attention to Step 4 [image: Smile]

Take care,
-Alex

Thank you very much for the help, but those steps were part of the
tutorial. I followed the step you mentioned, just to be safe, but it didn’t
help, sadly. (I suppose I’ve made enough posts that the spam filter won’t
be triggered, if any, so I’ve edited my OP to add it.)


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

R Manard wrote:

I still like to use?msv 6.0 but I do look at the new ones and install them from time to time for testing. I would be sure to install the service pack and be sure to start from?a new location, new project, and?check the compiler enviroment and see that the directories and object/library modules are listed correctly and such. One simple stupid thing can mess u up. I wish they would make it more simple with a set of standards so that libs would simply install themselves and then fully test themselves and run a diag.

That worked, thanks. :slight_smile:
Yeah, I wish it would work a little easier, like in Python, you just have to move libraries to a specific path, and it’ll work with an import statement, rather than going through all this with the IDE.

okay then, remove everything and then add one include and function at a
time. When it gives you the stink eye you’ll have a good lead on thte issue.On Wed, Feb 22, 2012 at 8:24 PM, Dr. Quigley <joecool.17.3 at gmail.com> wrote:

**

R Manard wrote:

I still like to use msv 6.0 but I do look at the new ones and install them
from time to time for testing. I would be sure to install the service pack
and be sure to start from a new location, new project, and check the
compiler enviroment and see that the directories and object/library modules
are listed correctly and such. One simple stupid thing can mess u up. I
wish they would make it more simple with a set of standards so that libs
would simply install themselves and then fully test themselves and run a
diag.

That worked, thanks. [image: Smile]
Yeah, I wish it would work a little easier, like in Python, you just have
to move libraries to a specific path, and it’ll work with an import
statement, rather than going through all this with the IDE.


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org