[Solved] Pointer Cannot be Written

Started by
36 comments, last by Eddy999999 16 years, 2 months ago
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?
God is not all-powerful, as he cannot build a wall he cannot jump.Stelimar Website: eddy999999.ed.funpic.org/Stelimar/index.html
Advertisement
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.
NextWar: The Quest for Earth available now for Windows Phone 7.
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."
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.
My rating perfectly reflects the pathetic yes-men in-crowd attitude of this forum.
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.
My rating perfectly reflects the pathetic yes-men in-crowd attitude of this forum.
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.
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.
Ah Mushu/Steve, so sure you're right...

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

kthxbai.
My rating perfectly reflects the pathetic yes-men in-crowd attitude of this forum.
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.
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.

This topic is closed to new replies.

Advertisement