Odd segmentation fault with CC/GCC but not G++ (C/SDL2/Linux) -


the code posted copied directly example of popular sdl2 tutorial, ensure wasn't had made silly mistake. i've done example changing path of image file in question, changed type bool int, false 0 , true 1. understand it, nothing c++ specific should remain.

everything seems work regardless do, when compiling cc/gcc (i suppose that's same deal) segmentation fault in end, suspect in close(), i've been unable determine. compiling g++ somehow prevents segmentation fault.

the solution of course simple, use g++, know wherein problem lies.

main.c:

//using sdl , standard io #include <sdl2/sdl.h> #include <stdio.h>  //screen dimension constants const int screen_width = 640; const int screen_height = 480;  //starts sdl , creates window int init();  //loads media int loadmedia();  //frees media , shuts down sdl void close();  //the window we'll rendering sdl_window* gwindow = null;  //the surface contained window sdl_surface* gscreensurface = null;  //the image load , show on screen sdl_surface* ghelloworld = null;  int init() {     //initialization flag     int success = 1;      //initialize sdl     if( sdl_init( sdl_init_video ) < 0 )     {         printf( "sdl not initialize! sdl_error: %s\n", sdl_geterror() );         success = 0;     }     else     {         //create window         gwindow = sdl_createwindow( "sdl tutorial", sdl_windowpos_undefined,                                     sdl_windowpos_undefined, screen_width,                                     screen_height, sdl_window_shown );         if( gwindow == null )         {             printf( "window not created! sdl_error: %s\n",                     sdl_geterror() );             success = 0;         }         else         {             //get window surface             gscreensurface = sdl_getwindowsurface( gwindow );         }     }      return success; }  int loadmedia() {     //loading success flag     int success = 1;      //load splash image     ghelloworld = sdl_loadbmp( "hello_world.bmp" );     if( ghelloworld == null )     {         printf( "unable load image %s! sdl error: %s\n", "hello_world.bmp",                 sdl_geterror() );         success = 0;     }      return success; }  void close() {     //deallocate surface     sdl_freesurface( ghelloworld );     ghelloworld = null;      //destroy window     sdl_destroywindow( gwindow );     gwindow = null;      //quit sdl subsystems     sdl_quit(); }  int main( int argc, char* args[] ) {     //start sdl , create window     if( !init() )     {         printf( "failed initialize!\n" );     }     else     {         //load media         if( !loadmedia() )         {             printf( "failed load media!\n" );         }         else         {             //apply image             sdl_blitsurface( ghelloworld, null, gscreensurface, null );              //update surface             sdl_updatewindowsurface( gwindow );              //wait 2 seconds             sdl_delay( 2000 );         }     }      //free resources , close sdl     close();      return 0; } 

i don't see how it's relevant, on safe side, here's standard makefile i'm using

#objs specifies files compile part of project objs = main.c  #cc specifies compiler we're using cc = cc  #compiler_flags specifies additional compilation options we're using # -w suppresses warnings compiler_flags = -wall -wextra -pedantic  #linker_flags specifies libraries we're linking against linker_flags = -lsdl2  #obj_name specifies name of our exectuable obj_name = test  #this target compiles our executable : $(objs)     $(cc) $(objs) $(compiler_flags) $(linker_flags) -o $(obj_name) 

i no errors or warnings unused argv , argc.

at point i'm @ dead end, ask here.

best regards.

nb: should mentioned whatever issue, it's not hardware issue, various search results have suggested, exact same result , problem, on 2 entirely different hardware platforms.

if rename function close() segfault goes away, looks init() calls sdl calls x11 calls driver calls close(), instead of calling right close() calls yours instead. in c++ functions name mangled else it not problem.


Comments

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -