[Solved] Pointer Cannot be Written

Started by
36 comments, last by Eddy999999 16 years, 2 months ago
For some reason, I can't seem to write to a pointer I have in a class. The pointer is supposed to be pointing to a SDL_Surface, and I am using a function to load an image into it. However, when I try to load the image, I get a runtime error saying that the memory could not be written. I even get the error when trying to set the pointer to NULL. I haven't been able to figure out what the problem was, and google didn't help much. Can anyone please help me? [Edited by - Eddy999999 on February 16, 2008 10:59:05 AM]
God is not all-powerful, as he cannot build a wall he cannot jump.Stelimar Website: eddy999999.ed.funpic.org/Stelimar/index.html
Advertisement
Let me focus on my telepathic powers to peek into your source code...

Is pointer to SDL_Surface inside a class? Is this enclosing class properly allocated?
*harnesses telepathic ability*
I sense there is a problem in your code.

*shakes magic 8 ball*...*Outlook not so good*

edit: telepathically beaten
"When you die, if you get a choice between going to regular heaven or pie heaven, choose pie heaven. It might be a trick, but if it's not, mmmmmmm, boy."
How to Ask Questions the Smart Way.
Lol, sorry, forgot to include the source.

Here's the relevant source for the class:

class SGE_UIElement {public:	SDL_Surface *background;	...	void loadBackground(string filename)  {		background=loadImage(filename);	}	...};


And my loadImage function:

SDL_Surface *loadImage(string filename) {	SDL_Surface* loadedImage=NULL;	SDL_Surface* optimizedImage=NULL;		loadedImage=IMG_Load(filename.c_str());	if (loadedImage!=NULL) {		optimizedImage=SDL_DisplayFormat(loadedImage);		SDL_FreeSurface(loadedImage);	}	return optimizedImage;}


The image I'm trying to load DOES exist, and the loadImage function is returning the image as it's supposed to. When I change background to a pointer outside of the class, it works perfectly. As I said above, I can't even set background to NULL.
God is not all-powerful, as he cannot build a wall he cannot jump.Stelimar Website: eddy999999.ed.funpic.org/Stelimar/index.html
Show how you declare the instance of "class SGE_UIElement".

Do you use it as pointer:
SGE_UIElement * myElement;


If so, then you're forgetting to allocate it.

The following:
SGE_UIElement * myElement = NULL;myElement->loadImage("Hello World");


Will compile perfectly - but it'll crash. Even worse, you'll be able to call all the functions that do not access members, as long as functions aren't virtual.
I am declaring it as a pointer:
ele=new SGE_UIElement();

But I don't think that's my problem. All the other functions of the class work fine, including the ones that access other members of the class.
God is not all-powerful, as he cannot build a wall he cannot jump.Stelimar Website: eddy999999.ed.funpic.org/Stelimar/index.html
What's the exact error? Access violations give an address. Is it near 0x00000000, near 0xcccccccc, near 0xcdcdcdcd, or some other address?

Example: "Unhandled exception: Access violation (0xc0000005) writing 0x00000000"
The exact error message was:

The instruction at "0x004246dc" referenced memory at "0x00000014". The memory could not be "written".
God is not all-powerful, as he cannot build a wall he cannot jump.Stelimar Website: eddy999999.ed.funpic.org/Stelimar/index.html
Any ideas anyone?
God is not all-powerful, as he cannot build a wall he cannot jump.Stelimar Website: eddy999999.ed.funpic.org/Stelimar/index.html
First and foremost I would recommend reading this.

Then I would recommend that you follow through with the exception to determine which line in your code is throwing it.

Richard "Superpig" Fine - saving pigs from untimely fates - Microsoft DirectX MVP 2006/2007/2008/2009
"Shaders are not meant to do everything. Of course you can try to use it for everything, but it's like playing football using cabbage." - MickeyMouse

This topic is closed to new replies.

Advertisement