Ah... the joy of segfaults...

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
ajtgarber
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 97
Joined: Wed Jun 10, 2009 8:56 am

Ah... the joy of segfaults...

Post by ajtgarber »

I'm working on a small level editor for a school project, and I've come across a problem.
When I try to load a previously saved map and render it, the program segfaults.
So far every value I've seen has been exactly what it was supposed to be, no null pointers, but everytime
my little tile is rendered KABOOM! The program creates a map first, saves it to a file, then reloads it and attempts to render it.
As far as I'm aware everything has loaded correctly. Any help would be appreciated.

Likely suspects would be utils.h, or main.cpp, though again I haven't found anything

main.cpp: http://pastebin.com/Kq1b54zS
tile.h: http://pastebin.com/0uQuLnbF
tile.cpp: http://pastebin.com/83ayrxJn
map.h: http://pastebin.com/Kp6PxqFa
map.cpp: http://pastebin.com/MwLVpxtU
utils.h: http://pastebin.com/GP63bBC9
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: Ah... the joy of segfaults...

Post by avansc »

If I had to take a guess, you are saving pointers to file, the loading them up again. wont work

http://elysianshadows.com/phpBB3/viewto ... ary#p28647
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
User avatar
short
ES Beta Backer
ES Beta Backer
Posts: 548
Joined: Thu Apr 30, 2009 2:22 am
Current Project: c++, c
Favorite Gaming Platforms: SNES, PS2, SNES, SNES, PC NES
Programming Language of Choice: c, c++
Location: Oregon, US

Re: Ah... the joy of segfaults...

Post by short »

First of all, why are you (trying) to inline everything?

Second:

Code: Select all

inline SDL_Surface* loadImage(char* filename) {
        SDL_Surface* loadedImage = IMG_Load(filename);
        SDL_Surface* optimizedImage = NULL;
        if(loadedImage) {
                optimizedImage = SDL_DisplayFormat(loadedImage);
                SDL_FreeSurface(loadedImage);
                SDL_SetColorKey(optimizedImage, SDL_SRCCOLORKEY, SDL_MapRGB(optimizedImage->format, 255, 0, 255));     
                cout << optimizedImage << endl;
        }
        return optimizedImage;
}
Double check that the pointer your returning isn't getting clobbered (ie is SDL_DisplayFormat creating memory on the heap?)

I think your stacked based pointer (optimizedImage) being returned may be incorrect. I'm not positive though. Post back what you find out.
My github repository contains the project I am currently working on,
link: https://github.com/bjadamson
ajtgarber
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 97
Joined: Wed Jun 10, 2009 8:56 am

Re: Ah... the joy of segfaults...

Post by ajtgarber »

Thanks for the reples

Avansc, instead of saving the SDL_Surface* to the file, I'm saving the image path to the file and trying to load that up again, the input looks right, but before this I was having some issues getting it to read correctly so there might be something messed up with the string, although the loadImage isn't returning null.

Short, I don't think its the pointer being garbled, when I directly load the image and try to render it through the Map it works just fine, it only breaks when I try to read the map from a file. The loadImage code is mainly from LazyFoo's tutorials with the only change being the SDL_SetColorKey.

From what I've seen from the output is that its segfaullting within Tile:;render, but neither pointer is null.

Here is the file format its trying to save/load (or should be anyway, everything is binary though so types are included):

Code: Select all

int width
int height
int size

int numSheets
int sheetNameSize;
char[sheetNameSize] sheetName
... more sheet entries

int sheetNum
int sheetX
int sheetY

... more tiles
ajtgarber
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 97
Joined: Wed Jun 10, 2009 8:56 am

Re: Ah... the joy of segfaults...

Post by ajtgarber »

Also the reason why I inlined all of the methods in there is because when I tried doing the #ifdef guards and the #pragma my compiler would always complain about multiple definitions of the methods there, I'll need to retry them to make sure I didn't just mess up last time
ajtgarber
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 97
Joined: Wed Jun 10, 2009 8:56 am

Re: Ah... the joy of segfaults...

Post by ajtgarber »

Okay... I messed up last time, the methods are no longer inline with the #ifndef guards
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: Ah... the joy of segfaults...

Post by dandymcgee »

ajtgarber wrote:From what I've seen from the output is that its segfaullting within Tile:;render, but neither pointer is null.
I don't think a NULL pointer is what you want to be looking for, but rather a garbage pointer, one that points to memory you should not be accessing. That's what a segmentation fault is (I believe Visual Studio actually has a more specific error for accessing a NULL pointer). This is usually an unintialized pointer, or a pointer to a location in memory that has been freed. You can differentiate the latter if compiling in debug mode, because Visual Studio will assign a magic number to the pointer (ie. 0xFEEEFEEE). Look out for interesting values like this, they are there to tell you something has gone wrong.
Magic Number Reference

Keep in mind, if you have two pointers to the same location and free that location, chances are Visual Studio will only assign a magic number to the pointer used to free the location, the other will become a dangling pointer.
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
ajtgarber
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 97
Joined: Wed Jun 10, 2009 8:56 am

Re: Ah... the joy of segfaults...

Post by ajtgarber »

Yup it was a garbage pointer, in my Map destructor it calls SDL_FreeSurface on every SDL_Surface* it loaded, so since my loadMap function doesn't return a pointer, I'm guessing the destructor is called at the end of the method so the surface was freed.
User avatar
short
ES Beta Backer
ES Beta Backer
Posts: 548
Joined: Thu Apr 30, 2009 2:22 am
Current Project: c++, c
Favorite Gaming Platforms: SNES, PS2, SNES, SNES, PC NES
Programming Language of Choice: c, c++
Location: Oregon, US

Re: Ah... the joy of segfaults...

Post by short »

Short, I don't think its the pointer being garbled, when I directly load the image and try to render it through the Map it works just fine, it only breaks when I try to read the map from a file. The loadImage code is mainly from LazyFoo's tutorials with the only change being the SDL_SetColorKey.
Yeah ignore that. IDK wtf was going through my head, a case of trying to find something wrong.
My github repository contains the project I am currently working on,
link: https://github.com/bjadamson
ajtgarber
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 97
Joined: Wed Jun 10, 2009 8:56 am

Re: Ah... the joy of segfaults...

Post by ajtgarber »

I've said a lot dumber things before :lol:
(sadly some of them are on this forum :P)
Post Reply