• Advertisement
Sign in to follow this  

0xC0000005: Access violation writing location 0x00000004.

This topic is 4203 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

No, I am not that experienced in C++/SDL programming, but I've tried everything I can think of, but it doesn't seem to help. This is the error I get in VS .NET 2003:
Unhandled exception at 0x0040163f in Scarinum Client.exe: 0xC0000005: Access violation writing location 0x00000004.

And it occurs here:
int Sprite::Load(const char *filename)
{
	// Make sure a filename was given
	if (filename == NULL)
	{
		// No filename given, exit
		return 0;
	}

	// Set loaded flag to false
	loaded = false;

	// Load bitmap into memory
	img = SDL_LoadBMP(filename);

	// Make sure bitmap was loaded successfully
	if (!img)
	{
		// Bitmap wasn't loaded, exit
		return 0;
	} else {
		// Bitmap was loaded, set loaded flag to true
		loaded = true;
	}

	// Bitmap was loaded successfully
	return 1;
}

Any help is appreciated! [Edited by - storage on February 23, 2005 10:55:49 PM]

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by kingnosis
First, I would check two things:

Is img an SDL_Surface *?
Is filename != NULL?


Thanks for your help! :)

Yep, img is an SDL_Surface *
Added filename != NULL

Still same problem tho :(

sprite.h:

#include <SDL.h>

class Sprite
{
public:
int Load(const char *filename);
int DrawSprite(SDL_Surface *img, int x, int y);
SDL_Surface *img;
bool loaded;
};

Share this post


Link to post
Share on other sites
Are you *sure* it occurs there?

Perhaps your sprite class isn't getting constructed correctly, or perhaps you're accessing it incorrectly.

I see nothing wrong with what you have posted. Perhaps it's elsewhere.

Share this post


Link to post
Share on other sites
You need to post more code I guess.
On a side note, returning 0 for failure is kinda non-standard and will confuse you when you are to use 3rd party libraries and such. Better get off that habit.

Share this post


Link to post
Share on other sites
Quote:
Original post by Prototype
You need to post more code I guess.
On a side note, returning 0 for failure is kinda non-standard and will confuse you when you are to use 3rd party libraries and such. Better get off that habit.


Okay, thanks, mind telling me what to return? :)

This is all the code in my main.cpp that has something to do with the sprite class:
// Includes
#include <SDL.h>
#include <stdio.h>
#include <sstream>
#include "logging.h"
#include "sprite.h"

int main(int argc, char *argv[])
{
Sprite *tile[4];

// ... logging, SDL initialization and such here ...

// Transparent color
alpha_r = 255; // Red
alpha_g = 0; // Green
alpha_b = 255; // Blue

// Load tiles
for (int n = 0; n <= 4; n++)
{
std::stringstream temp;

temp << "data\\tiles\\" << n << ".bmp";

tile[n]->Load(temp.str().c_str());

// transparency
SDL_SetColorKey(tile[n]->img, SDL_SRCCOLORKEY, SDL_MapRGB(tile[n]->img->format, alpha_r, alpha_g, alpha_b));
}

// ... event handling and more logging etc ...
}





Tell me if you need more :)

Thanks for all the help!

Share this post


Link to post
Share on other sites
You have:

Sprite *tile[4];


Which is an array of pointers to Sprites. You will either need to allocate new memory or use:


Sprite tile[4];
...
tile[n].Load(temp.str().c_str());


Once you do that, you should not be crashing anymore. Right now you are using NULL pointers, which is making your program do what it is doing.

- Drew

Share this post


Link to post
Share on other sites
Quote:
Original post by Drew_Benton
You have:

Sprite *tile[4];


Which is an array of pointers to Sprites. You will either need to allocate new memory or use:


Sprite tile[4];
...
tile[n].Load(temp.str().c_str());


- Drew


Thanks alot! :)

Share this post


Link to post
Share on other sites
You create an array of 4 sprites, while you are trying to initialize 5:


Sprite *tile[4]; // only creates 4 elements [0..3]
...
for (int n = 0; n <= 4; n++) // tries to access tile[4]

So change n<=4 into n<4.

Share this post


Link to post
Share on other sites
Quote:
Original post by Prototype
You create an array of 4 sprites, while you are trying to initialize 5:


Sprite *tile[4]; // only creates 4 elements [0..3]
...
for (int n = 0; n <= 4; n++) // tries to access tile[4]

So change n<=4 into n<4.


Thanks :)

It works! :) Thanks everyone, for helping me and learning me about pointers and such :)

Share this post


Link to post
Share on other sites
Oh yeah, BTW, usually a function returns 0 for success, and any other value to denote an error (which you can conveniently put into an enum for readability).

Share this post


Link to post
Share on other sites
Quote:
Original post by Prototype
Oh yeah, BTW, usually a function returns 0 for success, and any other value to denote an error (which you can conveniently put into an enum for readability).


Thanks thanks :)

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
It is perhaps worth noting the following point.

If you get an error such as: "Access violation writing location 0x00000000."
Then it usually means that you have dereferenced a NULL pointer.

However you got: "Access violation writing location 0x00000004."
When you get an error like this, with a small non-zero value, then
it often (but not always) means that your "this" pointer is NULL.

In this case you had "loaded = true;". Notice that loaded is exactly 4 bytes
into the class Sprite as it follows a pointer (4 bytes) in the declaration.

So given the error message the first thing to do was to check the "this"
pointer in the debugger, or just read the code that called the function
and check whether the pointer could be NULL.

Martin

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Hi everyone, I think I have a similar problem with VS.NET 2003. I get
Unhandled exception at 0x0041821a in test.exe: 0xC0000005: Access violation writing location 0x004436ec.

message with this code:
#include <conio.h>
#include <iostream>

using namespace std;

void revert(char *str)
{
char *last_ch = str;
while (*++last_ch) //*pluspluslast_ch
;
--last_ch;

while (str < last_ch)
{
char tmp;
tmp = *str;
*str++ = *last_ch; //buggy line (*strplusplus)
*last_ch-- = tmp;
}
}
int main()
{
char *zaq = "abcdefgh";
cout<<zaq<<endl;
revert(zaq);
cout<<endl<<zaq<<endl;

getch();
return 0;
}


It comes from "Pointers on C" by Kenneth A. Reek (chapter 6, ex. 3). The function should put chars in reverse order in a given char array without using indices and local temp arrays (by using pointers only). I don't know what is wrong with this code - it comes from the spoiler at the end of the book :(

Share this post


Link to post
Share on other sites
Your problem is:

char *zaq = "abcdefgh";

the fix:

char zaq[] = "abcdefgh";

zaq was pointing pointing to a memory address inside the text of your program, which cannot be changed.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
thx alot :) my mistake in main

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement