Visual C++ 2008 Debugging error

So today my friend asked me to help him make an introduction to a 2D game we might make at some point. So I started reading up on SDL; which we're going to use to create a window with a sprite which moves around the screen for a bit.

The code compiles fine, but I get the following error from visual studio:
"Unhandled exception at 0x681295f1 in Game.exe: 0xC0000005: Access violation reading location 0x00000000."

I have googled this error, but I couldn't find anything that solved it. In fact there was only one relevant result.

So if anyone knows what this error is and how to fix it, please advise.

In short, trying to write a game intro with SDL, got "Unhandled exception at 0x681295f1 in Game.exe: 0xC0000005: Access violation reading location 0x00000000" error, don't know how to fix it.

Thanks.

Here is the output from the debugger:

'Game.exe': Loaded 'C:\Users\Chris S\Documents\Projects\Game\Debug\Game.exe', Symbols loaded.
'Game.exe': Loaded 'C:\Windows\SysWOW64\ntdll.dll'
'Game.exe': Loaded 'C:\Windows\SysWOW64\kernel32.dll'
'Game.exe': Loaded 'C:\Users\Chris S\Documents\Projects\Game\Debug\SDL.dll', Binary was not built with debug information.
'Game.exe': Loaded 'C:\Windows\SysWOW64\advapi32.dll'
'Game.exe': Loaded 'C:\Windows\SysWOW64\rpcrt4.dll'
'Game.exe': Loaded 'C:\Windows\SysWOW64\secur32.dll'
'Game.exe': Loaded 'C:\Windows\SysWOW64\gdi32.dll'
'Game.exe': Loaded 'C:\Windows\SysWOW64\user32.dll'
'Game.exe': Loaded 'C:\Windows\SysWOW64\msvcrt.dll'
'Game.exe': Loaded 'C:\Windows\SysWOW64\winmm.dll'
'Game.exe': Loaded 'C:\Windows\SysWOW64\ole32.dll'
'Game.exe': Loaded 'C:\Windows\SysWOW64\oleaut32.dll'
'Game.exe': Loaded 'C:\Windows\SysWOW64\oleacc.dll'
'Game.exe': Loaded 'C:\Windows\winsxs\x86_microsoft.vc90.debugcrt_1fc8b3b9a1e18e3b_9.0.30729.1_none_bb1f6aa1308c35eb\msvcp90d.dll'
'Game.exe': Loaded 'C:\Windows\winsxs\x86_microsoft.vc90.debugcrt_1fc8b3b9a1e18e3b_9.0.30729.1_none_bb1f6aa1308c35eb\msvcr90d.dll'
'Game.exe': Loaded 'C:\Windows\winsxs\x86_microsoft.vc90.crt_1fc8b3b9a1e18e3b_9.0.30729.1_none_e163563597edeada\msvcr90.dll'
'Game.exe': Loaded 'C:\Windows\SysWOW64\shimeng.dll'
'Game.exe': Loaded 'C:\Windows\SysWOW64\apphelp.dll'
'Game.exe': Loaded 'C:\Windows\AppPatch\AcLayers.dll'
'Game.exe': Loaded 'C:\Windows\SysWOW64\shell32.dll'
'Game.exe': Loaded 'C:\Windows\SysWOW64\shlwapi.dll'
'Game.exe': Loaded 'C:\Windows\SysWOW64\userenv.dll'
'Game.exe': Loaded 'C:\Windows\SysWOW64\winspool.drv'
'Game.exe': Loaded 'C:\Windows\SysWOW64\mpr.dll'
'Game.exe': Loaded 'C:\Windows\SysWOW64\imm32.dll'
'Game.exe': Loaded 'C:\Windows\SysWOW64\msctf.dll'
'Game.exe': Loaded 'C:\Windows\SysWOW64\lpk.dll'
'Game.exe': Loaded 'C:\Windows\SysWOW64\usp10.dll'
'Game.exe': Loaded 'C:\Windows\winsxs\x86_microsoft.windows.common-controls_6595b64144ccf1df_6.0.6001.18000_none_5cdbaa5a083979cc\comctl32.dll'
'Game.exe': Loaded 'C:\Windows\SysWOW64\ddraw.dll'
'Game.exe': Loaded 'C:\Windows\SysWOW64\dciman32.dll'
'Game.exe': Loaded 'C:\Windows\SysWOW64\setupapi.dll'
'Game.exe': Loaded 'C:\Windows\SysWOW64\dwmapi.dll'
'Game.exe': Unloaded 'C:\Windows\SysWOW64\ddraw.dll'
'Game.exe': Unloaded 'C:\Windows\SysWOW64\dwmapi.dll'
'Game.exe': Unloaded 'C:\Windows\SysWOW64\setupapi.dll'
'Game.exe': Unloaded 'C:\Windows\SysWOW64\dciman32.dll'
'Game.exe': Loaded 'C:\Windows\SysWOW64\uxtheme.dll'
'Game.exe': Loaded 'C:\Windows\SysWOW64\KBDUS.DLL'
'Game.exe': Unloaded 'C:\Windows\SysWOW64\KBDUS.DLL'
'Game.exe': Loaded 'C:\Windows\SysWOW64\KBDUK.DLL'
'Game.exe': Unloaded 'C:\Windows\SysWOW64\KBDUK.DLL'
First-chance exception at 0x681295f1 in Game.exe: 0xC0000005: Access violation reading location 0x00000000.
Unhandled exception at 0x681295f1 in Game.exe: 0xC0000005: Access violation reading location 0x00000000.


By the way I added a "watch" to the return 0 line.

Edit: the window is created, and seemingly starts to load, and then crashes with the above error.
Sorry for the huge post but it's a fine line between not enough and too much information.

If anyone wants/needs the code it'll have to be in a different post because it's 139 lines long and I've already used over 4000 characters.
Last edited on
I'll need to see your code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#include <ctime>
#include "SDL.h"
#include <windows.h>

#define SCREEN_WIDTH  1024
#define SCREEN_HEIGHT 768
#define SPRITE_SIZE   256


int main(int argc, char *argv[]) {
    srand((unsigned)time(0)); 
    SDL_Surface *screen, *temp, *sprite, *floor;
    SDL_Rect rcSprite, rcFloor;
    Uint8 *keystate;
    SDL_Event msg;  // Create an event handler where events/messages are stored.
    int quit = 0; // If this becomes 1 (true/yes) the game will exit. While false, the game will play.
    int colorkey;
// Initialize SDL:
    SDL_Init(SDL_INIT_VIDEO);
// Set the title text:
    SDL_WM_SetCaption("SDL Test2 (with sprite)", "SDL Test2");
// Create the window, load the background image:
    screen = SDL_SetVideoMode (1024, 768, 0, 0);
    temp   = SDL_LoadBMP("logo.bmp");
// Convert the bitmap so it can be used as display:
//    SDL_Surface* bg = SDL_DisplayFormat(temp);
// Free the temporary surface from memory:
    SDL_FreeSurface(temp);

// Load the sprite to temp, load it to display format to sprite, then free temp.
    temp   = SDL_LoadBMP("logo.bmp");
    sprite = SDL_DisplayFormat(temp);
	SDL_FreeSurface(temp);

// Set up sprite color key:
    colorkey = SDL_MapRGB(screen->format, 0, 0, 0); // Black will be treated as transparent
    SDL_SetColorKey(sprite, SDL_SRCCOLORKEY|SDL_RLEACCEL, colorkey);
    
// Load the 'floor'
    temp  = SDL_LoadBMP("floor.bmp");
    floor = SDL_DisplayFormat(temp);
    SDL_FreeSurface(temp);

// Set the position of the sprite:
    rcSprite.x = 0;
    rcSprite.y = 0;
    
// Event handling:
    int timer = 0; // When this reaches roughly 20 seconds, we will exit and load the game instead.
    while (!quit) { // While quit is false; while we aren't being asked to exit. So while quit = 0.
        Sleep(100);
        timer++;
        if (timer == 100) {
            quit = 1;
        }
// Check for events/messages:
        if (SDL_PollEvent(&msg)) { // if an event/message needs to be handled:
            switch (msg.type) {
                case SDL_QUIT: // A destroy/exit message/event was found
                    quit = 1;
                    break;
                case SDL_KEYDOWN: // A keyboard event was found:
                    switch(msg.key.keysym.sym) {
                        case SDLK_ESCAPE:
                             quit = 1;
                             break;
			default:
			    continue;
			    break;
                    } // switch(event.key.keysym.sym)
                    break;
                } // switch (msg.type)
            } // if (SDL_PollEvent(&msg))

// Handle sprite movement:
        
        keystate = SDL_GetKeyState(NULL);
        /*
		if (keystate[SDLK_LEFT]) {
            rcSprite.x -= 2;
        }
        if (keystate[SDLK_RIGHT]) {
            rcSprite.x += 2;
        }
        if (keystate[SDLK_UP]) {
            rcSprite.y -= 2;
        }
        if (keystate[SDLK_DOWN]) {
            rcSprite.y += 2;
        }
        */
// The commented out lines above allow the user to move the logo. The following, uncommented lines allow the program to move the logo
// To random positions:
        
// First create the x and y positions:
        int rndx = 0, rndy = 0, min = 0, max = 1024, range =(max - min) + 1;
        rndx = min + int (range * rand() / (RAND_MAX + 1.0));
        if (rndx > 768) {
            rndy = rndx - 256;
        } else {
            rndy = rndx;
        }
// Then set the sprite to them:
        rcSprite.x = rndx;
        rcSprite.y = rndy;
// Handle collision with edges of screen.
        if (rcSprite.x < 0) {
            rcSprite.x = 0;
        }
        else if (rcSprite.x > SCREEN_WIDTH-SPRITE_SIZE) {
            rcSprite.x = SCREEN_WIDTH-SPRITE_SIZE;
        }
        if (rcSprite.y < 0) {
            rcSprite.y = 0;
        }
        else if (rcSprite.y > SCREEN_HEIGHT-SPRITE_SIZE) {
            rcSprite.y = SCREEN_HEIGHT-SPRITE_SIZE;
        }

// Draw the floor:
        for (int x = 0; x < SCREEN_WIDTH / SPRITE_SIZE; x++) {
            for (int y = 0; y < SCREEN_HEIGHT / SPRITE_SIZE; y++) {
                rcFloor.x = x * SPRITE_SIZE;
                rcFloor.y = y * SPRITE_SIZE;
                SDL_BlitSurface(floor, NULL, screen, &rcFloor);
            }
        }
// Draw the sprite:
        SDL_BlitSurface(sprite, NULL, screen, &rcSprite);

// Update the screen:
        SDL_UpdateRect(screen, 0, 0, 0, 0);
    } // while (!quit)
// Clean up SDL and then exit:
    SDL_FreeSurface(sprite);
    SDL_FreeSurface(floor);
    SDL_Quit();
    return 0;
} // int main([...]) 
Last edited on
Aside from the structure of your code, I don't see any errors that jump out at me. Are you sure you don't have your BMP files named something incorrect like logobmp.bmp or something. Your code seems fine so it must be a problem with the files you are attempting to load. A couple of things would make this code considerably easier to read and debug. You should break out your program and use 3 or 4 functions to handle loading, blitting, etc...

Hope this helps.
Last edited on
By the error you got I'm going to assume your code is trying to dereference a null pointer
That's what leads me to believe he's not successfully loading the bitmap.
I have been, in a sense, learning this as I go along; I found a different tutorial on doing this; it's much better. Using it I've not only learnt better practice but also how to organise code into several files. I'd never done that before. So that's good.

Looking over that code it does look pretty untidy. I think it's the comments mostly; for some reason I didn't indent them. Looks kinda horrible. One thing I noticed is that I recently always put else/else if statements on the same line as the end of the corresponding if statement; like this:
1
2
3
4
5
6
7
if ([...]) {
// code
} else if ([...]) {
// code
} else {
// code
}


But for some reason some of them seem to have gone on different lines. Strange.


You were right; though, I think I was loading the file incorrectly. But the other tutorial is much better; also I'm using a compiler named Code::Blocks, which personally I think is excellent.

On the second tutorial, things are explained better; and I can actually get to the point where I can see my badly photoshopped images floating around :3

Thank you for the help :)
Last edited on
Where is the new tutorial that you're now using? Can you post a link to it.
Sure; http://www.sdltutorials.com/sdl-tutorial-basics/

It's all of the "SDL-tutorials" in the side navigation. I'm on events at the moment.

It's also helped by the fact that the person who wrote the tutorial has a similar style to mine.

It's a good tutorial, although there are a couple of acknowledged bugs (which are addressed in the comments on the page).
Last edited on
Thanks! That looks like a good place for me to explore as well.
Topic archived. No new replies allowed.