Jump to content

  • Log In with Google      Sign In   
  • Create Account

0xC0000005: Access violation writing location 0x00000004.


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
15 replies to this topic

#1 storage   Members   -  Reputation: 175

Like
0Likes
Like

Posted 23 February 2005 - 04:32 PM

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]

Sponsor:

#2 kingnosis   Members   -  Reputation: 267

Like
0Likes
Like

Posted 23 February 2005 - 04:52 PM

First, I would check two things:

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

#3 storage   Members   -  Reputation: 175

Like
0Likes
Like

Posted 23 February 2005 - 04:55 PM

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;
};


#4 Scyllinice   Members   -  Reputation: 707

Like
0Likes
Like

Posted 23 February 2005 - 05:05 PM

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.

#5 Prototype   Members   -  Reputation: 1354

Like
0Likes
Like

Posted 23 February 2005 - 05:06 PM

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.

#6 storage   Members   -  Reputation: 175

Like
0Likes
Like

Posted 23 February 2005 - 05:17 PM

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!

#7 Drew_Benton   Crossbones+   -  Reputation: 1713

Like
0Likes
Like

Posted 23 February 2005 - 05:21 PM

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

"But I, being poor, have only my dreams. I have spread my dreams under your feet; tread softly, because you tread on my dreams." - William Butler Yeats

#8 storage   Members   -  Reputation: 175

Like
0Likes
Like

Posted 23 February 2005 - 05:26 PM

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:

[code=auto:0]
Sprite tile[4];
...
tile[n].Load(temp.str().c_str());
[code=auto:0]

- Drew


Thanks alot! :)



#9 Prototype   Members   -  Reputation: 1354

Like
0Likes
Like

Posted 23 February 2005 - 05:29 PM

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.

#10 storage   Members   -  Reputation: 175

Like
0Likes
Like

Posted 23 February 2005 - 05:31 PM

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 :)

#11 Prototype   Members   -  Reputation: 1354

Like
0Likes
Like

Posted 23 February 2005 - 05:34 PM

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).

#12 storage   Members   -  Reputation: 175

Like
0Likes
Like

Posted 23 February 2005 - 05:35 PM

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 :)

#13 Anonymous Poster_Anonymous Poster_*   Guests   -  Reputation:

0Likes

Posted 24 February 2005 - 04:29 AM

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


#14 Anonymous Poster_Anonymous Poster_*   Guests   -  Reputation:

0Likes

Posted 20 July 2006 - 01:13 PM

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 :(

#15 squashed_bug   Members   -  Reputation: 220

Like
0Likes
Like

Posted 20 July 2006 - 01:55 PM

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.

#16 Anonymous Poster_Anonymous Poster_*   Guests   -  Reputation:

0Likes

Posted 20 July 2006 - 02:09 PM

thx alot :) my mistake in main




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS