Segfault (Segmentation fault) for no reason

Pages: 1234
Damn you're right.Ithink that's exactly what's going on.
Any idea on how to fix it?
Ok i'll send you the code in private messages but get ready couse it's a whole lot of it.
Any advise would be rethink your design and get rid of globals. Or at least minimize their usage.


But I'm not 100% convinced this is actually the problem. You mentioned you could PM me the code -- is that offer still on the table? I'll definitely give some time to check it out if you can send it.

EDIT: ninja'd. cool I'll look for your PM.
Last edited on
damn .. it's to big the maximum length is 8192 and only my main.cpp is over 35000
see my PM. Don't send me the source in an actual message. zip it up, throw it online somewhere (if you have dropbox, you can use that), and then PM me a link to the zip.
oh.. right .. i didn't think of that =))
I sent it via DropBox.Thx dude.
Okay so I gave it an honest look.

You do have a lot of interdependent global objects... BUT none of them do anything with other globals in their ctor (as far as I can tell). So even if they are constructed out of order, that wouldn't be a problem.

So I don't see anything here that would cause a pre-main crash. I hate to ask again, but are you absolutely sure this crash is happening pre-main?



So while I don't see a crash, I do see some other problems, such as a lot of wasted resources and memory leaks. IE, each time you create a new Item, you reload the global 'font' font. On top of this being incredibly wasteful because you're loading the same font numerous times, it also leaks memory because you never destroy the original font. In fact, I don't think you destroy anything in this program. None of your classes have dtors and I didn't notice any cleanup code anywhere.

But while all that is certainly bad in that it is inefficient and will hog memory and resources as the program runs --- none of it would cause a crash per se. Especially not a pre-main one.



So I'm stumped. Without being able to compile and run to debug myself (which I'm not willing to do because I don't have the SDL libs nor am I willing to install them right now), there isn't much more I can see here. =( Sorry.

I already said that main works fine...I never said it's pre-main.. It's the chests that bug it out.Look:
This works:
1
2
3
4
5
6
if(ROOM_TYPE == R_CHEST)
    {
        int chestX = X + 71 + (rand()%(W-4))*32;
        int chestY = Y + 71 + (rand()%(H-4))*32;
        //mapA.add_chest(chestX,chestY,22,22,"./resources/chest.png");
    }


And this doesn't:
1
2
3
4
5
6
if(ROOM_TYPE == R_CHEST)
    {
        int chestX = X + 71 + (rand()%(W-4))*32;
        int chestY = Y + 71 + (rand()%(H-4))*32;
        mapA.add_chest(chestX,chestY,22,22,"./resources/chest.png");
    }
Doh! You totally DID say it got to main. I need to learn how to read.

Sorry sorry. Taking another look.
Also : 1. I have no idea what a dtor is.
2.I am going to optimize it after I make it work , since it's a small game and it doesn't use to manu resources.
Also I'm not sure if it's a memory issue , the debugger says so and i take it for granted.
okay wait... so if it's post-main ... then when it crashes the debugger should snap on the exact line of code that is crashing.

Debugger tools are great here. C::B should have a window that has what's called a "call stack", which is basically a list of all the functions that you have called, down to main. If the crash is happening in library code and not code you wrote, you can use that window to go up the call stack to find the code of yours that is calling the lib function.


Once you have your line of code that is the culprit, with the debugger still snapped, you can "watch" specific variables to see what their contents are at runtime. You probably have a bad pointer or something.
It doesn't say the line but it does refer to my Player initialization , but that's fine I already checked..and earlier it just said something about stdstr() and function ?? ().
I think it hust behaves randomly...
Also Item.h caused me troble before.. I added some more variables to the class and one of them was auto-assigning a value like 55 for no reason..
So either it's a Segmentation Fault either I was cursed.
Now it stops executing here for some reason:

In Player.cpp,line 40
1
2
3
    SDL_Surface* loadedImg = IMG_Load(imgAddr);
    surface = SDL_DisplayFormatAlpha(loadedImg);
    SDL_FreeSurface(loadedImg);
It stops on line 43 , but i use those 3 lines in every single object and they work fine.
It must tell you what line. This is what debuggers are designed for.

Can you dropbox/imgur a screenshot of your IDE immediately after you trigger a crash?

EDIT: (Code::Blocks is your IDE. Sorry if I'm throwing out a bunch of obscure terms)
Last edited on
Here's the debug log (without the debug initializing itself):

Child process PID: 1208

[debug][New Thread 1208.0xe80]
[debug][New Thread 1208.0x3f8]
[debug][New Thread 1208.0x24c]
[debug][New Thread 1208.0x9e0]
[debug][New Thread 1208.0xd0c]
[debug][New Thread 1208.0xcd0]
[debug][New Thread 1208.0xbac]
[debug][New Thread 1208.0xfc]
[debug]Program received signal SIGSEGV, Segmentation fault.
[debug]0x681259e0 in SDL_DisplayFormatAlpha () from C:\DOCUME~1\0hidden0\Desktop\PN01\bin\Debug\SDL.dll
[debug]>>>>>>cb_gdb:

Program received signal SIGSEGV, Segmentation fault.
In SDL_DisplayFormatAlpha () (C:\DOCUME~1\0hidden0\Desktop\PN01\bin\Debug\SDL.dll)

[debug]> bt 30
[debug]#0 0x681259e0 in SDL_DisplayFormatAlpha () from C:\DOCUME~1\0hidden0\Desktop\PN01\bin\Debug\SDL.dll
[debug]#1 0x0040c26d in Player::Player (this=0x131e40, x=320, y=240, w=32, h=32, img_addr=0x4815d8 <_ZStorSt13_Ios_OpenmodeS_+4724184> "./resources/player.png") at C:\Documents and Settings\0hidden0\Desktop\PN01\Player.cpp:43
[debug]#2 0x00409d11 in init () at C:\Documents and Settings\0hidden0\Desktop\PN01\main.cpp:950
[debug]#3 0x004056f9 in SDL_main (argc=1, argv=0x3e3e20) at C:\Documents and Settings\0hidden0\Desktop\PN01\main.cpp:141
[debug]#4 0x0040e6c9 in console_main (argc=1, argv=0x3e3e20) at ./src/main/win32/SDL_win32_main.c:315
[debug]#5 0x0040e78b in WinMain@16 (hInst=0x400000, hPrev=0x0, szCmdLine=0x241f15 "", sw=10) at ./src/main/win32/SDL_win32_main.c:398
[debug]#6 0x0047e1eb in main ()
[debug]>>>>>>cb_gdb:
[debug]> frame 1
[debug]#1 0x0040c26d in Player::Player (this=0x131e40, x=320, y=240, w=32, h=32, img_addr=0x4815d8 <_ZStorSt13_Ios_OpenmodeS_+4724184> "./resources/player.png") at C:\Documents and Settings\0hidden0\Desktop\PN01\Player.cpp:43
[debug]

C:\Documents and Settings\0hidden0\Desktop\PN01\Player.cpp:43:723:beg:0x40c26d
[debug]>>>>>>cb_gdb:

#1 0x0040c26d in Player::Player (this=0x131e40, x=320, y=240, w=32, h=32, img_addr=0x4815d8 <_ZStorSt13_Ios_OpenmodeS_+4724184> "./resources/player.png") at C:\Documents and Settings\0hidden0\Desktop\PN01\Player.cpp:43


C:\Documents and Settings\0hidden0\Desktop\PN01\Player.cpp:43:723:beg:0x40c26d
At C:\Documents and Settings\0hidden0\Desktop\PN01\Player.cpp:43

[debug]> bt 30
[debug]#0 0x681259e0 in SDL_DisplayFormatAlpha () from C:\DOCUME~1\0hidden0\Desktop\PN01\bin\Debug\SDL.dll
[debug]#1 0x0040c26d in Player::Player (this=0x131e40, x=320, y=240, w=32, h=32, img_addr=0x4815d8 <_ZStorSt13_Ios_OpenmodeS_+4724184> "./resources/player.png") at C:\Documents and Settings\0hidden0\Desktop\PN01\Player.cpp:43
[debug]#2 0x00409d11 in init () at C:\Documents and Settings\0hidden0\Desktop\PN01\main.cpp:950
[debug]#3 0x004056f9 in SDL_main (argc=1, argv=0x3e3e20) at C:\Documents and Settings\0hidden0\Desktop\PN01\main.cpp:141
[debug]#4 0x0040e6c9 in console_main (argc=1, argv=0x3e3e20) at ./src/main/win32/SDL_win32_main.c:315
[debug]#5 0x0040e78b in WinMain@16 (hInst=0x400000, hPrev=0x0, szCmdLine=0x241f15 "", sw=10) at ./src/main/win32/SDL_win32_main.c:398
[debug]#6 0x0047e1eb in main ()
[debug]>>>>>>cb_gdb:
[debug]> frame 1
[debug]#1 0x0040c26d in Player::Player (this=0x131e40, x=320, y=240, w=32, h=32, img_addr=0x4815d8 <_ZStorSt13_Ios_OpenmodeS_+4724184> "./resources/player.png") at C:\Documents and Settings\0hidden0\Desktop\PN01\Player.cpp:43
[debug]

C:\Documents and Settings\0hidden0\Desktop\PN01\Player.cpp:43:723:beg:0x40c26d
[debug]>>>>>>cb_gdb:

#1 0x0040c26d in Player::Player (this=0x131e40, x=320, y=240, w=32, h=32, img_addr=0x4815d8 <_ZStorSt13_Ios_OpenmodeS_+4724184> "./resources/player.png") at C:\Documents and Settings\0hidden0\Desktop\PN01\Player.cpp:43


C:\Documents and Settings\0hidden0\Desktop\PN01\Player.cpp:43:723:beg:0x40c26d
Continuing...

[debug]> cont
[debug]Continuing.
[debug]Program received signal SIGSEGV, Segmentation fault.
[debug]0x681259e0 in SDL_DisplayFormatAlpha () from C:\DOCUME~1\0hidden0\Desktop\PN01\bin\Debug\SDL.dll
[debug]>>>>>>cb_gdb:

Program received signal SIGSEGV, Segmentation fault.
In SDL_DisplayFormatAlpha () (C:\DOCUME~1\0hidden0\Desktop\PN01\bin\Debug\SDL.dll)

[debug]> bt 30
[debug]#0 0x681259e0 in SDL_DisplayFormatAlpha () from C:\DOCUME~1\0hidden0\Desktop\PN01\bin\Debug\SDL.dll
[debug]#1 0x0040c26d in Player::Player (this=0x131e40, x=320, y=240, w=32, h=32, img_addr=0x4815d8 <_ZStorSt13_Ios_OpenmodeS_+4724184> "./resources/player.png") at C:\Documents and Settings\0hidden0\Desktop\PN01\Player.cpp:43
[debug]#2 0x00409d11 in init () at C:\Documents and Settings\0hidden0\Desktop\PN01\main.cpp:950
[debug]#3 0x004056f9 in SDL_main (argc=1, argv=0x3e3e20) at C:\Documents and Settings\0hidden0\Desktop\PN01\main.cpp:141
[debug]#4 0x0040e6c9 in console_main (argc=1, argv=0x3e3e20) at ./src/main/win32/SDL_win32_main.c:315
[debug]#5 0x0040e78b in WinMain@16 (hInst=0x400000, hPrev=0x0, szCmdLine=0x241f15 "", sw=10) at ./src/main/win32/SDL_win32_main.c:398
[debug]#6 0x0047e1eb in main ()
[debug]>>>>>>cb_gdb:
[debug]> frame 1
[debug]#1 0x0040c26d in Player::Player (this=0x131e40, x=320, y=240, w=32, h=32, img_addr=0x4815d8 <_ZStorSt13_Ios_OpenmodeS_+4724184> "./resources/player.png") at C:\Documents and Settings\0hidden0\Desktop\PN01\Player.cpp:43
[debug]

C:\Documents and Settings\0hidden0\Desktop\PN01\Player.cpp:43:723:beg:0x40c26d
[debug]>>>>>>cb_gdb:

#1 0x0040c26d in Player::Player (this=0x131e40, x=320, y=240, w=32, h=32, img_addr=0x4815d8 <_ZStorSt13_Ios_OpenmodeS_+4724184> "./resources/player.png") at C:\Documents and Settings\0hidden0\Desktop\PN01\Player.cpp:43


C:\Documents and Settings\0hidden0\Desktop\PN01\Player.cpp:43:723:beg:0x40c26d
At C:\Documents and Settings\0hidden0\Desktop\PN01\Player.cpp:43

[debug]> bt 30
[debug]#0 0x681259e0 in SDL_DisplayFormatAlpha () from C:\DOCUME~1\0hidden0\Desktop\PN01\bin\Debug\SDL.dll
[debug]#1 0x0040c26d in Player::Player (this=0x131e40, x=320, y=240, w=32, h=32, img_addr=0x4815d8 <_ZStorSt13_Ios_OpenmodeS_+4724184> "./resources/player.png") at C:\Documents and Settings\0hidden0\Desktop\PN01\Player.cpp:43
[debug]#2 0x00409d11 in init () at C:\Documents and Settings\0hidden0\Desktop\PN01\main.cpp:950
[debug]#3 0x004056f9 in SDL_main (argc=1, argv=0x3e3e20) at C:\Documents and Settings\0hidden0\Desktop\PN01\main.cpp:141
[debug]#4 0x0040e6c9 in console_main (argc=1, argv=0x3e3e20) at ./src/main/win32/SDL_win32_main.c:315
[debug]#5 0x0040e78b in WinMain@16 (hInst=0x400000, hPrev=0x0, szCmdLine=0x241f15 "", sw=10) at ./src/main/win32/SDL_win32_main.c:398
[debug]#6 0x0047e1eb in main ()
[debug]>>>>>>cb_gdb:
[debug]> frame 1
[debug]#1 0x0040c26d in Player::Player (this=0x131e40, x=320, y=240, w=32, h=32, img_addr=0x4815d8 <_ZStorSt13_Ios_OpenmodeS_+4724184> "./resources/player.png") at C:\Documents and Settings\0hidden0\Desktop\PN01\Player.cpp:43
[debug]

C:\Documents and Settings\0hidden0\Desktop\PN01\Player.cpp:43:723:beg:0x40c26d
[debug]>>>>>>cb_gdb:

#1 0x0040c26d in Player::Player (this=0x131e40, x=320, y=240, w=32, h=32, img_addr=0x4815d8 <_ZStorSt13_Ios_OpenmodeS_+4724184> "./resources/player.png") at C:\Documents and Settings\0hidden0\Desktop\PN01\Player.cpp:43


C:\Documents and Settings\0hidden0\Desktop\PN01\Player.cpp:43:723:beg:0x40c26d
Continuing...

[debug]> cont
[debug]Continuing.
[debug][Inferior 1 (process 1208) exited with code 030000000005]
[debug]>>>>>>cb_gdb:

[Inferior 1 (process 1208) exited with code 030000000005]

[debug]> quit

Debugger finished with status 0
The IDE doesn;t do much at all it just crashes the program..
It gives me a don't send error when I run it individually.
Make sure your SDL_Surface is valid before you call SDL_DisplayFormatAlpha.

1
2
 if(!loadedImg)
    return -1;   // Show some sort of error  


If this is the case your filename is probably not correct.
Also make sure you are not calling SDL_DisplayFormatAlpha before you call SDL_Init.
Last edited on
Pages: 1234