Jump to content
  • Advertisement
Sign in to follow this  
Eddy999999

[Solved] Pointer Cannot be Written

This topic is 3778 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

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]

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
*harnesses telepathic ability*
I sense there is a problem in your code.

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

edit: telepathically beaten

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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"

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!