Sign in to follow this  

[Solved] Pointer Cannot be Written

This topic is 3586 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
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
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
I pretty much already know what the problem is - I can't write any data to the background in my class - I just don't know how to fix it. I did some debugging prior to posting and found out that the line that was causing the error was this:
background=loadImage(filename);

I also found out that the loadImage function was returning the image surface as it was supposed to, and that the address of background was 0x00000014 (as stated in the error). As I stated above, I also figured out that I couldn't even set background to NULL.

So basically, my question is: What could be causing me to not be able to write any data to background?

Share this post


Link to post
Share on other sites
From that error message, it is very likely that the class that background is contained in is set to NULL, as per Antheus's post.

When memory addresses are close to 0, it is exceedingly likely that you're trying to access a NULL pointer. I'm guessing that background is offset by 14 or so bytes in the class.

Share this post


Link to post
Share on other sites
Quote:
Original post by Eddy999999
I pretty much already know what the problem is

No, you know a symptom that the problem is causing. You have yet to determine the actual problem, or you would have fixed it already.

You've got a NULL pointer somewhere. Pop open your debugger and step through your code to find out where you're losing your handle on the class instance (since you've stated that you're actually allocating it). Or, better yet, give us the code where you're actually calling loadBackground, so we can smack you with a hefty "told you so."

Share this post


Link to post
Share on other sites
Quote:
Original post by Eddy999999
class SGE_UIElement {
public:
SDL_Surface *background;

void loadBackground(string filename) {
background=loadImage(filename);
}
};


Ugh, why is background public, and while it is public the loadBackground member function is unnecessary.

Quote:
Original post by Eddy999999
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;
}


This leaks memory.

Share this post


Link to post
Share on other sites
Also, develop on Linux and use Valgrind to track down errors before asking online. It's an incredible tool that no other platform has, and it makes developing on any other platform an embarrassing and painful experience. You'll find way more bugs than you could hope to find otherwise.

Share this post


Link to post
Share on other sites
Quote:
Original post by truthsayer
Quote:
Original post by Eddy999999
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;
}



This leaks memory.


No it doesn't.

Share this post


Link to post
Share on other sites
Quote:
Original post by truthsayer
Quote:
Original post by Eddy999999
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;
}


This leaks memory.
Where? There's no memory allocated that is lost in that function. Therefore not a memory leak.

Share this post


Link to post
Share on other sites
Quote:
Original post by truthsayer
Ah Mushu/Steve, so sure you're right...

1. IMG_Load returns a surface
2. SDL_DisplayFormat fails to convert and returns null
3. loadImage returns NULL, surface from #1 leaked

kthxbai.

1. Yes, which is freed on the SDL_FreeSurface() call
2. If SDL_DisplayFormat fails, it returns NULL, then loadedImage is freed with SDL_FreeSurface().
3. loadedImage is the surface that was returned in #1. If it's null, then it doesn't exist.

EDIT: Oh, you edited your post. Why would SDL_FreeSurface fail to free? That's like saying you have a memory leak if delete[] fails. While it's true, it's a moot point, since you can't free memory if your cleanup function doesn't work for some reason.

Share this post


Link to post
Share on other sites
Quote:
Original post by Eddy999999
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;
}


Quote:
Original post by truthsayer
Ah Mushu/Steve, so sure you're right...

You, sir, are an ass.

>loadedImage=IMG_Load(filename.c_str());
Quote:
1. IMG_Load returns a surface

loadedImage = <handle>
optimizedImage = NULL


>optimizedImage=SDL_DisplayFormat(loadedImage);
Quote:
2. SDL_DisplayFormat fails to convert and returns null

loadedImage = <handle>
optimizedImage = NULL

Quote:
3. loadImage returns NULL, surface from #1 leaked

WHOOPS IT LOOKS LIKE YOU MISSED THIS LINE SDL_FreeSurface(loadedImage);
Quote:
kthxbai.

Go back to Linux, where the ``advanced features" make it apparently so you don't even have to understand the fundamental concepts of program flow.

inb4 YHBT. YHL. HAND.

Share this post


Link to post
Share on other sites
Quote:
Original post by Mushu
Quote:
Original post by Eddy999999
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;
}


Quote:
Original post by truthsayer
Ah Mushu/Steve, so sure you're right...

You, sir, are an ass.

>loadedImage=IMG_Load(filename.c_str());
Quote:
1. IMG_Load returns a surface

loadedImage = <handle>
optimizedImage = NULL


>optimizedImage=SDL_DisplayFormat(loadedImage);
Quote:
2. SDL_DisplayFormat fails to convert and returns null

loadedImage = <handle>
optimizedImage = NULL

Quote:
3. loadImage returns NULL, surface from #1 leaked

WHOOPS IT LOOKS LIKE YOU MISSED THIS LINE SDL_FreeSurface(loadedImage);
Quote:
kthxbai.

Go back to Linux, where the ``advanced features" make it apparently so you don't even have to understand the fundamental concepts of program flow.

inb4 YHBT. YHL. HAND.


So I made a mistake? Speaking of being an ass, look who's talking. "Go back to Linux", wow. You're a real nice guy.

Share this post


Link to post
Share on other sites
Quote:
Original post by truthsayer
Quote:
Original post by Mushu
Quote:
Original post by Eddy999999
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;
}


Quote:
Original post by truthsayer
Ah Mushu/Steve, so sure you're right...

You, sir, are an ass.

>loadedImage=IMG_Load(filename.c_str());
Quote:
1. IMG_Load returns a surface

loadedImage = <handle>
optimizedImage = NULL


>optimizedImage=SDL_DisplayFormat(loadedImage);
Quote:
2. SDL_DisplayFormat fails to convert and returns null

loadedImage = <handle>
optimizedImage = NULL

Quote:
3. loadImage returns NULL, surface from #1 leaked

WHOOPS IT LOOKS LIKE YOU MISSED THIS LINE SDL_FreeSurface(loadedImage);
Quote:
kthxbai.

Go back to Linux, where the ``advanced features" make it apparently so you don't even have to understand the fundamental concepts of program flow.

inb4 YHBT. YHL. HAND.


So I made a mistake? Speaking of being an ass, look who's talking. "Go back to Linux", wow. You're a real nice guy.


I just wanted to quote my awesome post where I stomp on your e-ponos.

Share this post


Link to post
Share on other sites
Quote:
Original post by Mushu
Quote:
Original post by truthsayer
Quote:
Original post by Mushu
Quote:
Original post by Eddy999999
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;
}


Quote:
Original post by truthsayer
Ah Mushu/Steve, so sure you're right...

You, sir, are an ass.

>loadedImage=IMG_Load(filename.c_str());
Quote:
1. IMG_Load returns a surface

loadedImage = <handle>
optimizedImage = NULL


>optimizedImage=SDL_DisplayFormat(loadedImage);
Quote:
2. SDL_DisplayFormat fails to convert and returns null

loadedImage = <handle>
optimizedImage = NULL

Quote:
3. loadImage returns NULL, surface from #1 leaked

WHOOPS IT LOOKS LIKE YOU MISSED THIS LINE SDL_FreeSurface(loadedImage);
Quote:
kthxbai.

Go back to Linux, where the ``advanced features" make it apparently so you don't even have to understand the fundamental concepts of program flow.

inb4 YHBT. YHL. HAND.


So I made a mistake? Speaking of being an ass, look who's talking. "Go back to Linux", wow. You're a real nice guy.


I just wanted to quote my awesome post where I stomp on your e-ponos.


You rule and stuff?

Share this post


Link to post
Share on other sites
Quote:
Original post by truthsayer
I didn't realise people were so jealous of Linux having Valgrind. It's open source guys, you could port it to Windows/OS X if you knew how.
What on earth does this have to do with valgrind?

Share this post


Link to post
Share on other sites

This topic is 3586 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this