Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Segmentation fault al_map_rgb

Credits go to Chris Katko and GullRaDriel for helping out!
This thread is locked; no one can reply to it. rss feed Print
Segmentation fault al_map_rgb
Ariesnl
Member #2,902
November 2002
avatar

In my new startrek game I ran into a nasty problem..
since I added the communication class I get a segmentation fault in the engine constructor when making the colors needed with al_map_rgb..
Could any of you guys take a look at it ?

I'm staring at it for quite some time but I don't see the problem... :(

It should compile on windows and linux

Thanx in advance

also you have to copy the resources from the debug map to the release map to do a release build.. including resources in both maps was too big.

Perhaps one day we will find that the human factor is more complicated than space and time (Jean luc Picard)
Current project: [Star Trek Project ] Join if you want ;-)

Chris Katko
Member #1,881
January 2002
avatar

Is the constructor being called before allegro is initialized?

If you have a global / singleton that's calling constructors before Allegro gets initialized, it's going to crash.

-----sig:
“Programs should be written for people to read, and only incidentally for machines to execute.” - Structure and Interpretation of Computer Programs
"Political Correctness is fascism disguised as manners" --George Carlin

Ariesnl
Member #2,902
November 2002
avatar

No al_init() is the first thing I call

#SelectExpand
1// Setup Allegro 2bool Setup() 3{ 4 if(al_init()) 5 { 6#ifdef DEBUG 7 strcpy(g_szSector,""); 8#endif // DEBUG 9 10 g_nScreenWidth = 1024; 11 g_nScreenHeight = 768; 12 //al_set_new_display_option(ALLEGRO_VSYNC, 1, ALLEGRO_SUGGEST); 13#ifdef DEBUG 14 al_set_new_display_flags(ALLEGRO_WINDOWED); 15#else 16 al_set_new_display_flags(ALLEGRO_FULLSCREEN); 17#endif 18 g_pDisplay = al_create_display(g_nScreenWidth, g_nScreenHeight); 19 if (g_pDisplay != NULL) 20 { 21 // Install allegro modules 22 al_init_primitives_addon(); 23 al_init_image_addon(); 24 al_init_font_addon(); 25 al_init_ttf_addon(); 26 al_install_keyboard(); 27 al_install_mouse(); 28 al_install_audio(); 29 al_init_acodec_addon(); 30 31 32 g_pTimer=al_create_timer(1.000/g_nCyclesPerSecond); 33 34 // install event queue 35 g_pEventQueue = al_create_event_queue(); 36 al_register_event_source(g_pEventQueue, al_get_keyboard_event_source()); 37 al_register_event_source(g_pEventQueue, al_get_mouse_event_source()); 38 al_register_event_source(g_pEventQueue, al_get_display_event_source(g_pDisplay)); 39 al_register_event_source(g_pEventQueue, al_get_timer_event_source(g_pTimer)); 40 41 42 // Set Fontpath 43 g_pFontPath = al_get_standard_path(ALLEGRO_RESOURCES_PATH); 44 al_append_path_component(g_pFontPath, "font"); 45 46 // set Soundpath 47 g_pSoundPath = al_get_standard_path(ALLEGRO_RESOURCES_PATH); 48 al_append_path_component(g_pSoundPath, "sound"); 49 50 51 // Load a font 52 al_set_path_filename(g_pFontPath,"font.ttf"); 53 g_pFont=al_load_ttf_font(al_path_cstr(g_pFontPath,ALLEGRO_NATIVE_PATH_SEP),10,ALLEGRO_TTF_MONOCHROME); 54 if (g_pFont == NULL) 55 { 56 cout << "Could not load font"; 57 return false; 58 } 59 60 g_pInfoFont=al_load_ttf_font(al_path_cstr(g_pFontPath,ALLEGRO_NATIVE_PATH_SEP),15,0 /*ALLEGRO_TTF_MONOCHROME*/); 61 if (g_pInfoFont == NULL) 62 { 63 cout << "Could not load font"; 64 return false; 65 } 66 67 68 g_pBigFont = al_load_ttf_font(al_path_cstr(g_pFontPath,ALLEGRO_NATIVE_PATH_SEP),20,0); 69 if (g_pBigFont == NULL) 70 { 71 cout << "Could not load big font"; 72 return false; 73 } 74 75 if (!al_reserve_samples(RESERVED_SAMPLES)) 76 { 77 cout << "Could not Reserve samples"; 78 return false; 79 } 80 81 // Load sounds 82 for (int i=0;i<SND_MAX;i++) 83 { 84 char szFileName[255]; 85 sprintf(szFileName,"%04d.wav",i+1); 86 al_set_path_filename(g_pSoundPath,szFileName); 87 g_pSample[i] = al_load_sample(al_path_cstr(g_pSoundPath,ALLEGRO_NATIVE_PATH_SEP)); 88 if (g_pSample[i]==NULL) 89 { 90 cout << "Wrong sample"; 91 return false; 92 } 93 } 94 95 if (!InitObjects()) 96 { 97 cout << "Could not init Objects"; 98 return false; 99 } 100 101 102 // set quit to false 103 g_blQuit = false; 104 105 g_pEngine = new TEngine(g_nScreenWidth,g_nScreenHeight); 106 g_pParalax = new Paralax(g_nScreenWidth,g_nScreenHeight,300,5); 107 g_pEnterprise = new TEnterprise(); 108 g_pEngine->Add(g_pEnterprise); 109 110 g_pKeyMapper = new TKeyMapper(g_pEngine->m_clGREEN, g_pEngine->m_clYELLOW,g_pBigFont); 111 112 NewUniverse(); 113 NewSector(g_pEnterprise->m_nSectorPositionX,g_pEnterprise->m_nSectorPositionY); 114 g_pEnterprise->SetPosition(g_pUniverse->m_nSectorCenter, g_pUniverse->m_nSectorCenter); 115 g_pEngine->SetOrigin(g_pUniverse->m_nSectorCenter, g_pUniverse->m_nSectorCenter); 116 117 g_vGameState.push_back(GS_GAME); 118 119 120#ifdef DEBUG 121 sprintf(g_szSector,"SECTOR: %d,%d",g_pEnterprise->m_nSectorPositionX,g_pEnterprise->m_nSectorPositionY); 122#endif // DEBUG 123 124 al_hide_mouse_cursor(g_pDisplay); 125 al_start_timer(g_pTimer); 126 g_dLastTime = al_get_time(); 127 } 128 else 129 { 130 return false; 131 } 132 } 133 else 134 { 135 return false; 136 } 137 138 return true; 139}

...and setup is called from main...

#SelectExpand
2int main(int argc, char **argv) 3{ 4 try 5 { 6 7 8 if (Setup()) 9 { 10 while (!g_blQuit) 11 { 12 ALLEGRO_EVENT ev; 13 if (al_get_next_event(g_pEventQueue, &ev)) 14 { 15 16 switch (ev.type) 17 { 18 case ALLEGRO_EVENT_KEY_DOWN: 19 KeyDown(ev.keyboard.keycode); 20 break; 21 case ALLEGRO_EVENT_KEY_UP: 22 KeyUp(ev.keyboard.keycode); 23 break; 24 25 case ALLEGRO_EVENT_DISPLAY_CLOSE: 26 g_blQuit = true; 27 break; 28 29 case ALLEGRO_EVENT_MOUSE_AXES: 30 Mouse(&ev.mouse); 31 break; 32 33 case ALLEGRO_EVENT_MOUSE_BUTTON_DOWN: 34 MouseButtonDown(&ev.mouse); 35 break; 36 37 case ALLEGRO_EVENT_MOUSE_BUTTON_UP: 38 MouseButtonUp(&ev.mouse); 39 break; 40 41 case ALLEGRO_EVENT_TIMER: 42 Timer(&ev.timer); 43 break; 44 } 45 } 46 else 47 { 48 Render(); 49 } 50 } 51 } 52 else 53 { 54 return -1; 55 } 56 } 57 58 catch (A5Exception except) 59 { 60 cout << except.what(); 61 62 63 } 64 65 ShutDown(); 66 return 0; 67}

Perhaps one day we will find that the human factor is more complicated than space and time (Jean luc Picard)
Current project: [Star Trek Project ] Join if you want ;-)

Chris Katko
Member #1,881
January 2002
avatar

So where is it failing inside setup()? I don't see al_map_rgb() in there, but I do see them in TEngine::TEngine. Do you have a stack trace from GDB for the failure? Do any specific debug options / defines cause it to fail?

Also, did anything change recently that started the problem? That is, did they fail the second you started putting them in, or did they fail after you changed something else?

-----sig:
“Programs should be written for people to read, and only incidentally for machines to execute.” - Structure and Interpretation of Computer Programs
"Political Correctness is fascism disguised as manners" --George Carlin

Ariesnl
Member #2,902
November 2002
avatar

Like I sayed, the segmentationfault pops up in the engine constructor after a few calls to al_map_rgb.. exactly where the color white is made.. but If you remove that one the next color becomes the problem.. so it probably isn't the true location of the problem

#SelectExpand
3TEngine::TEngine(int a_nWidth,int a_nHeight) 4{ 5 m_blSensorStatic=false; 6 m_nCount = 0; 7 m_nGameOver = GO_PLAYING; 8 m_dX = 5000; 9 m_dY = 5000; 10 m_dPosx = m_dX; 11 m_dPosy = m_dY; 12 13 m_nScreenWidth = a_nWidth; 14 m_nScreenHeight = a_nHeight; 15 m_nScreenMidX = m_nScreenWidth/2; 16 m_nScreenMidY = m_nScreenHeight/2; 17 18 // set some colors 19 m_clBLACK = al_map_rgb(0,0,0); 20 m_clRED = al_map_rgb(255,0,0); 21 m_clGREEN = al_map_rgb(0,255,0); 22 m_clBLUE = al_map_rgb(0,0,255); 23 m_clYELLOW = al_map_rgb(255,255,0); 24 m_clBROWN = al_map_rgb(100,100,70); 25 m_clWHITE = al_map_rgb(255,255,255); 26 m_clMAGENTA = al_map_rgb(255,0,255); 27 m_clAQUA = al_map_rgb(0,255,255); 28 m_clATHM = al_map_rgb(80,80,60); 29 30 for(int i=0; i<KEY_MAX;i++) 31 { 32 m_blKeys[i]=false; 33 } 34 35}

#0 00402E00 TEngine::TEngine(this=0x2ef3df0, a_nWidth=1024, a_nHeight=768) (C:\Documents and Settings\Owner\Desktop\StarTrekV3_Linux\Engine.cpp:366)
#1 004139AC Setup() (C:\Documents and Settings\Owner\Desktop\StarTrekV3_Linux\StartrekV3.cpp:233)
#2 004152C7 main(argc=<error reading variable: Cannot access memory at address 0x22ff38>, argv=<error reading variable: Cannot access memory at address 0x22ff38>) (C:\Documents and Settings\Owner\Desktop\StarTrekV3_Linux\StartrekV3.cpp:767)

Perhaps one day we will find that the human factor is more complicated than space and time (Jean luc Picard)
Current project: [Star Trek Project ] Join if you want ;-)

GullRaDriel
Member #3,861
September 2003
avatar

Stack is so fkcd up, it can't even read argv.

"Code is like shit - it only smells if it is not yours"
Allegro Wiki, full of examples and articles !!

Ariesnl
Member #2,902
November 2002
avatar

how is that possible ?

Perhaps one day we will find that the human factor is more complicated than space and time (Jean luc Picard)
Current project: [Star Trek Project ] Join if you want ;-)

Chris Katko
Member #1,881
January 2002
avatar

That means some pointer, some where, is thrashing everything and its most likely completely unrelated to the actual al_map_rgb call.

It's likely that if you change the compiling options (debug/optimize levels) the error will jump around to a different line because the overall organization of the game and memory layout will have changed slightly.

Check all pointers. Check all arrays. Check all classes to make sure you're not using a non-pointer version of a derived class in a base class array (because if they're not pointers, the base class constructor/deconstructor never gets called).

Get Valgrind with:

sudo apt-get install valgrind

And run your program with Valgrind. Valgrind is GREAT for finding memory problems because it runs the entire program in a fake (virtual) CPU that is smart enough to know when something inside it is wrong.

A sample valgrind line I found from a tutorial is this:

valgrind --tool=memcheck --leak-check=yes --show-reachable=yes --num-callers=20 --track-fds=yes ./test

Where test is your program name. Valgrind has hundreds of options so it can be daunting at first glance.

[edit] I tried downloading your code but I cannot get it to compile. Clang throws tons of compile errors including missing constants, missing STD namespace, and more. What did you use on Linux to compile it?

#SelectExpand
1In file included from GameEngine.cpp:21: 2In file included from ./Ship.h:33: 3./Engine.h:97:3: error: typedef redefinition with different types ('struct sGameObject' vs 'struct sGameObject') 4} sGameObject; 5 ^ 6./GameEngine.h:85:3: note: previous definition is here 7} sGameObject; 8 ^ 9In file included from GameEngine.cpp:21: 10In file included from ./Ship.h:33: 11./Engine.h:105:7: error: redefinition of 'TSprite' 12class TSprite 13 ^ 14./GameEngine.h:92:7: note: previous definition is here 15class TSprite 16 ^ 17GameEngine.cpp:152:59: error: use of undeclared identifier 'ID_SHIP' 18 if ((hulp!=NULL)&&(!hulp->m_blDestroyed)&&(hulp->m_nID>=ID_SHIP)) 19 ^ 20GameEngine.cpp:173:21: error: use of undeclared identifier 'ID_SHIP' 21 if (hulp->m_nID>=ID_SHIP) 22 ^ 23GameEngine.cpp:229:21: error: use of undeclared identifier 'ID_SHIP' 24 if (hulp->m_nID>=ID_SHIP) 25 ^ 26GameEngine.cpp:387:45: error: use of undeclared identifier 'ID_STARBASE'; did you mean 'ID_STAR_BLUE'? 27 if ((a_pPlayer->m_pTarget->m_nID==ID_STARBASE)&&(a_pPlayer->m_pTarget->m_nMember==MEM_FEDERATION)) color = m_clBLUE; 28 ^~~~~~~~~~~ 29 ID_STAR_BLUE 30./types.h:136:5: note: 'ID_STAR_BLUE' declared here 31 ID_STAR_BLUE, 32 ^ 33GameEngine.cpp:435:24: error: use of undeclared identifier 'ID_SHIP' 34 if ((hulp->m_nID >= ID_SHIP)&&(hulp->m_blDestroyed == false)&&(hulp->m_nMember!=a_nMember) && (hulp->m_blCanFind) ) 35 ^ 36GameEngine.cpp:447:24: error: use of undeclared identifier 'ID_SHIP' 37 if ((hulp->m_nID >= ID_SHIP)&&(hulp->m_blDestroyed == false)&&(hulp->m_nMember==a_nMember) && (hulp->m_blCanFind) ) 38 ^ 39GameEngine.cpp:477:24: error: use of undeclared identifier 'ID_STARBASE'; did you mean 'ID_STAR_BLUE'? 40 if ((hulp->m_nID >= ID_STARBASE)&&(hulp->m_blDestroyed==false)&&(hulp->m_nMember!=a_nMember)) 41 ^~~~~~~~~~~ 42 ID_STAR_BLUE 43./types.h:136:5: note: 'ID_STAR_BLUE' declared here 44 ID_STAR_BLUE, 45 ^ 46GameEngine.cpp:489:24: error: use of undeclared identifier 'ID_STARBASE'; did you mean 'ID_STAR_BLUE'? 47 if ((hulp->m_nID >= ID_STARBASE)&&(hulp->m_blDestroyed==false)&&(hulp->m_nMember==a_nMember)) 48 ^~~~~~~~~~~ 49 ID_STAR_BLUE 50./types.h:136:5: note: 'ID_STAR_BLUE' declared here 51 ID_STAR_BLUE, 52 ^ 53GameEngine.cpp:511:69: error: 'm_nPhaserEnergy' is a protected member of 'TShip' 54 al_draw_filled_rectangle(dx+64, dy+50, dx+69, (dy+50)-(a_pPlayer->m_nPhaserEnergy),m_clRED); 55 ^ 56./Ship.h:67:2: note: declared protected here 57 m_nPhaserEnergy, 58 ^ 59GameEngine.cpp:593:26: error: use of undeclared identifier 'TBullet' 60 object.m_nTarget = ((TBullet *)(*p))->m_nTarget; 61 ^ 62GameEngine.cpp:593:35: error: expected expression 63 object.m_nTarget = ((TBullet *)(*p))->m_nTarget; 64 ^ 65GameEngine.cpp:597:21: error: use of undeclared identifier 'ID_SHIP' 66 if ((*p)->m_nID>ID_SHIP) 67 ^ 68GameEngine.cpp:608:45: error: 'm_dWaypointX' is a protected member of 'TShip' 69 object.m_dWaypointX = ((TShip *)(*p))->m_dWaypointX; 70 ^ 71./Ship.h:60:5: note: declared protected here 72 m_dWaypointX, 73 ^ 74GameEngine.cpp:609:45: error: 'm_dWaypointY' is a protected member of 'TShip' 75 object.m_dWaypointY = ((TShip *)(*p))->m_dWaypointY; 76 ^ 77./Ship.h:61:5: note: declared protected here 78 m_dWaypointY, 79 ^ 80GameEngine.cpp:611:45: error: 'm_nTorpedoes' is a protected member of 'TShip' 81 object.m_nTorpedoes = ((TShip *)(*p))->m_nTorpedoes; 82 ^ 83./Ship.h:70:2: note: declared protected here 84 m_nTorpedoes, 85 ^ 86GameEngine.cpp:612:46: error: 'm_nRepairItem' is a protected member of 'TShip' 87 object.m_nRepairItem = ((TShip *)(*p))->m_nRepairItem; 88 ^ 89./Ship.h:65:5: note: declared protected here 90int m_nRepairItem, 91 ^ 92GameEngine.cpp:613:40: error: 'm_nCrew' is a protected member of 'TShip' 93 object.m_nCrew = ((TShip *)(*p))->m_nCrew; 94 ^ 95./Ship.h:66:2: note: declared protected here 96 m_nCrew, 97 ^ 98fatal error: too many errors emitted, stopping now [-ferror-limit=] 9920 errors generated. 100In file included from MathVectors.cpp:1: 101./MathVectors.h:9:17: warning: using directive refers to implicitly-defined namespace 'std' 102using namespace std; 103 ^ 1041 warning generated.

-----sig:
“Programs should be written for people to read, and only incidentally for machines to execute.” - Structure and Interpretation of Computer Programs
"Political Correctness is fascism disguised as manners" --George Carlin

Ariesnl
Member #2,902
November 2002
avatar

Ik was coding on Windows yesterday, But I built and ran it several times on Linux (ubuntu) using code::blocks (gcc).
I had to reinstall ubuntu some time ago so I need to install and setup allegro again. I'll try it tonight.

Edit.. I was missing permissions to run .. errrm

however.. strange thing is I can build on linux.. the debug version runs fine now..
how that is possible the devil may know... the release build gives error 255 however.. stack overflow ? I thought a debug used up more stack than a release ???

{"name":"610242","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/1\/c\/1ca328eaed59fabaa186e693d99191c9.png","w":1280,"h":1024,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/1\/c\/1ca328eaed59fabaa186e693d99191c9"}610242

Edit: after some testing with other code I found the version of allegro I was using caused the problem.. I have no idea why but with version 5.0.4 I get the segmentation fault. With version 5.0.1 everything runs fine....
it could be that the 5.0.4 version was build wrong somehow since I built that one myself some time ago. The 5.0.1 version is from allegro.cc

Buy why ?

Perhaps one day we will find that the human factor is more complicated than space and time (Jean luc Picard)
Current project: [Star Trek Project ] Join if you want ;-)

Go to: