Page 1 of 1
Ah... the joy of segfaults...
Posted: Thu May 05, 2011 9:33 pm
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
Re: Ah... the joy of segfaults...
Posted: Thu May 05, 2011 9:59 pm
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
Re: Ah... the joy of segfaults...
Posted: Thu May 05, 2011 11:51 pm
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.
Re: Ah... the joy of segfaults...
Posted: Fri May 06, 2011 5:35 am
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
Re: Ah... the joy of segfaults...
Posted: Fri May 06, 2011 6:02 am
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
Re: Ah... the joy of segfaults...
Posted: Fri May 06, 2011 6:05 am
by ajtgarber
Okay... I messed up last time, the methods are no longer inline with the #ifndef guards
Re: Ah... the joy of segfaults...
Posted: Fri May 06, 2011 8:39 am
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.
Re: Ah... the joy of segfaults...
Posted: Fri May 06, 2011 10:03 am
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.
Re: Ah... the joy of segfaults...
Posted: Fri May 06, 2011 1:02 pm
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.
Re: Ah... the joy of segfaults...
Posted: Fri May 06, 2011 1:43 pm
by ajtgarber
I've said a lot dumber things before
(sadly some of them are on this forum :P)