Code error please help!

Everytime I try to compile my program it gives me an error I'm not familiar with, I will post the relevant part of my code and what the error says.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
void CMain::GameLoop(void)
{

	while (!quit && mainEvent -> type != SDL_QUIT) 
	{
		SDL_PollEvent(mainEvent); 
		SDL_RenderClear(renderer);

		SDL_RenderCopy(renderer, grass_image, NULL, &grass_rect); 
		SDL_RenderCopy(renderer, bob_image, NULL, &bob_rect); 
		SDL_RenderPresent(renderer);
	}

}


I'm not sure why but when it fails, this is the bit of code it points too. Hoever I don't see anything wrong with it. and it doesn't give me any warnings or anything. the error i get is

Unhandled exception at 0x00b213d3 in firstRPG.exe: 0xC0000005: Access violation reading location 0xcccccccc.

I appreciate your help on this, thank you!
Please post all of your code. even if that is the function causing the trouble we have no way of knowing.
Unhandled exception at 0x00b213d3 in firstRPG.exe: 0xC0000005: Access violation reading location 0xcccccccc.
If you're getting this error then it's not when you try to compile, silly.

Best guess: mainEvent is uninitialized at line 4.
Correction:
1
2
3
4
5
6
7
8
//mainEvent must be an 'SDL_Event', not an 'SDL_Event *'!
while (!quit){
    //More than one event may have accumulated since the last call to SDL_PollEvent()!
    while (SDL_PollEvent(&mainEvent) && mainEvent.type != SDL_QUIT);
    if (mainEvent.type == SDL_QUIT)
        break;
    //...
}
Thanks, I what is odd is that I was following a tutorial video and matched my code perfectly with his and even eventually used his source code and it still didn't work for me, but worked for him. but i got frustrated and just stopped that project. its a little over my head at this point anyways.
tutorial video
Well, there's your problem.
Oftentimes, the compiler may "point" to a function stating that it is causing an error, when in fact, the actual problem is somewhere else.

I wouldn't actually give up on this, but sometimes printing your code onto paper instead of staring at the screen will help. I do this all the time, when I need to refactor my code, or fix hard-to-find problems.

Joe
Topic archived. No new replies allowed.