[Solved] Pointer Cannot be Written
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]
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?
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
I sense there is a problem in your code.
*shakes magic 8 ball*...*Outlook not so good*
edit: telepathically beaten
Lol, sorry, forgot to include the source.
Here's the relevant source for the class:
And my loadImage function:
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.
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.
Show how you declare the instance of "class SGE_UIElement".
Do you use it as pointer:
If so, then you're forgetting to allocate it.
The following:
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.
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:
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.
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.
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"
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".
The instruction at "0x004246dc" referenced memory at "0x00000014". The memory could not be "written".
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.
Then I would recommend that you follow through with the exception to determine which line in your code is throwing it.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement