Sign in to follow this  
Mizipzor

Access violation

Recommended Posts

Mizipzor    247
Ive created a SdlWrapper for myself to use. From times to times it works but sometimes I just gets access violation when I blit from a surface to the screen. But it have always solved itself somehow. :S But this time I want to find out whats wrong and fix it once and for all. This how my draw image function looks like:
void SdlWrapper::DrawImg(SDL_Surface* img, int x, int y) {	// draws the image at those coordinates on the screen
	SDL_Rect dest;
	dest.x = x;
	dest.y = y;
	SDL_BlitSurface(img, NULL, screen, &dest);
}

Debugger says that img is defined 0x00397a58, so there should be some image there. X and y are both 0. Now I tested something, and I found out that the exact same code works in main but not in a class's member function. I ran:
SDL_Surface* temp;
temp = Sdl->LoadBmp("0");
Sdl->DrawImg(temp, 50, 50);

In main, everything was fine. But if I ran it in the gameclass render world function, it got a access violation. The function where I get the access violation now looks likes this:
void Soduko::Game() {
	SDL_Surface* temp;
	temp = Sdl->LoadBmp("0");
	Sdl->DrawImg(temp, 50, 50);
	return;

	for(int y = 0; y <= 9; y++) {
		for(int x = 0; x <= 9; y++) {
			cout << slot[(y*9)+x] << endl << numberBmp[ slot[(y*9)+x] ] << endl;
			Sdl->DrawImg(numberBmp[ slot[(y*9)+x] ], x*32, y*32);
		}
	}
}

Im trying to make a Soduko game btw. :) But there you see I try to do the exact same thing as in main. One succeds one fails. Although the access violation comes at the blitsurface line in the sdlwrapper member class. Whats causing all this? If you want to see more code, tell me and I post the entire classes. Thanks in advance.

Share this post


Link to post
Share on other sites
hh10k    589
I don't think it's possible to find the error with what you've supplied. My guess would be that the error is in LoadBmp.

Share this post


Link to post
Share on other sites
Mizipzor    247
But loadbmp works in main? And the surface pointer I have points to something so there should be something there. :P

[edit] And by loading the same image in the same way again should make it impossible for them to differ.

[Edited by - Mizipzor on November 26, 2005 6:07:18 AM]

Share this post


Link to post
Share on other sites
Mizipzor    247
Haha, how could I miss that one? :P But that isnt where the problem is, since I return above it the code is never reached. I still gets access violation when I blit the surface.

Share this post


Link to post
Share on other sites
Spoonbender    1258
Quote:
Original post by Mizipzor
But loadbmp works in main?

That doesn't prove *anything*
Access violations *may* occur when you write to unallocated memory. There's no guarantee. It might depend on where you're calling from, the exact code you compiled (if you change a few things, or run on a different computer even, it might decide to use a different address, where you won't get an access violation.

The OS doesn't keep track of which specific bytes you own. It just allocates a number of pages, so if you allocate, say, 8 byte, you might end up actually getting 8k. And if you hit within those 8k, no access violation will occur, even though the memory hasn't been properly allocated in your program.

So you're going to have to assume the worst when dealing with these kinds of errors. The problem may be in virtually any function, at virtually any point in the code, depending on a dozen external factors, up to and including, the phases of the moon.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this