Added some extra protection to notify the developer if they haven't initialized the application properly.
authorSam Lantinga <slouken@libsdl.org>
Wed, 05 Jun 2013 21:23:59 -0700
changeset 7281cd5516b9263d
parent 7280 d1681bbd274a
child 7282 df78adc8ba04
Added some extra protection to notify the developer if they haven't initialized the application properly.

This will help reduce issues like that reported in bug 1819:

Wouter van Oortmerssen 2013-04-23 20:12:07 EDT
#0 0x01d1e881 in __HALT ()
#1 0x01c58971 in _CFRuntimeCreateInstance ()
#2 0x02e4acc1 in GSFontCreateWithName ()
#3 0x00adc0e1 in UINewFont ()
#4 0x00adc24c in +[UIFont systemFontOfSize:traits:] ()
#5 0x00adc298 in +[UIFont systemFontOfSize:] ()
#6 0x009fb5d9 in +[UITextFieldLabel defaultFont] ()
#7 0x00a8ccd5 in -[UILabel _commonInit] ()
#8 0x00a8ce14 in -[UILabel initWithFrame:] ()
#9 0x00a052eb in -[UITextField createTextLabelWithTextColor:] ()
#10 0x009fbede in -[UITextField initWithFrame:] ()
#11 0x00152ead in -[SDL_uikitview initializeKeyboard] at /Users/aardappel/lobster/external/SDL-2.0.0-7046/Xcode-iOS/SDL/../../src/video/uikit/SDL_uikitview.m:208
#12 0x0015290c in -[SDL_uikitview initWithFrame:] at /Users/aardappel/lobster/external/SDL-2.0.0-7046/Xcode-iOS/SDL/../../src/video/uikit/SDL_uikitview.m:50
#13 0x00153b5b in -[SDL_uikitopenglview initWithFrame:scale:retainBacking:rBits:gBits:bBits:aBits:depthBits:stencilBits:majorVersion:] at /Users/aardappel/lobster/external/SDL-2.0.0-7046/Xcode-iOS/SDL/../../src/video/uikit/SDL_uikitopenglview.m:53
#14 0x001524ff in UIKit_GL_CreateContext at /Users/aardappel/lobster/external/SDL-2.0.0-7046/Xcode-iOS/SDL/../../src/video/uikit/SDL_uikitopengles.m:114
#15 0x0015078f in SDL_GL_CreateContext at /Users/aardappel/lobster/external/SDL-2.0.0-7046/Xcode-iOS/SDL/../../src/video/SDL_video.c:2666
#16 0x000d8c5c in SDLInit(char const*, vec<int, 2>&) at /Users/aardappel/lobster/dev/xcode/lobster/../../src/sdlsystem.cpp:193
include/SDL_main.h
src/SDL.c
src/main/android/SDL_android_main.cpp
src/main/psp/SDL_psp_main.c
src/main/windows/SDL_windows_main.c
src/video/uikit/SDL_uikitappdelegate.m
     1.1 --- a/include/SDL_main.h	Wed Jun 05 21:14:26 2013 -0700
     1.2 +++ b/include/SDL_main.h	Wed Jun 05 21:23:59 2013 -0700
     1.3 @@ -72,6 +72,15 @@
     1.4  extern "C" {
     1.5  #endif
     1.6  
     1.7 +/*
     1.8 + *  This is called by the real SDL main function to let the rest of the
     1.9 + *  library know that initialization was done properly.
    1.10 + *
    1.11 + *  Calling this yourself without knowing what you're doing can cause
    1.12 + *  crashes and hard to diagnose problems with your application.
    1.13 + */
    1.14 +extern DECLSPEC void SDL_SetMainReady(void);
    1.15 +
    1.16  #ifdef __WIN32__
    1.17  
    1.18  /**
     2.1 --- a/src/SDL.c	Wed Jun 05 21:14:26 2013 -0700
     2.2 +++ b/src/SDL.c	Wed Jun 05 21:23:59 2013 -0700
     2.3 @@ -43,6 +43,11 @@
     2.4  
     2.5  
     2.6  /* The initialized subsystems */
     2.7 +#ifdef SDL_MAIN_NEEDED
     2.8 +static SDL_bool SDL_MainIsReady = SDL_FALSE;
     2.9 +#else
    2.10 +static SDL_bool SDL_MainIsReady = SDL_TRUE;
    2.11 +#endif
    2.12  static SDL_bool SDL_bInMainQuit = SDL_FALSE;
    2.13  static Uint8 SDL_SubsystemRefCount[ 32 ];
    2.14  
    2.15 @@ -88,9 +93,20 @@
    2.16      return SDL_SubsystemRefCount[subsystem_index] == 1 || SDL_bInMainQuit;
    2.17  }
    2.18  
    2.19 +void
    2.20 +SDL_SetMainReady(void)
    2.21 +{
    2.22 +    SDL_MainIsReady = SDL_TRUE;
    2.23 +}
    2.24 +
    2.25  int
    2.26  SDL_InitSubSystem(Uint32 flags)
    2.27  {
    2.28 +    if (!SDL_MainIsReady) {
    2.29 +        SDL_SetError("Application didn't initialize properly, did you include SDL_main.h in the file containing your main() function?");
    2.30 +        return -1;
    2.31 +    }
    2.32 +
    2.33  #if !SDL_TIMERS_DISABLED
    2.34      SDL_InitTicks();
    2.35  #endif
     3.1 --- a/src/main/android/SDL_android_main.cpp	Wed Jun 05 21:14:26 2013 -0700
     3.2 +++ b/src/main/android/SDL_android_main.cpp	Wed Jun 05 21:23:59 2013 -0700
     3.3 @@ -20,6 +20,8 @@
     3.4      /* This interface could expand with ABI negotiation, calbacks, etc. */
     3.5      SDL_Android_Init(env, cls);
     3.6  
     3.7 +    SDL_SetMainReady();
     3.8 +
     3.9      /* Run the application code! */
    3.10      int status;
    3.11      char *argv[2];
     4.1 --- a/src/main/psp/SDL_psp_main.c	Wed Jun 05 21:14:26 2013 -0700
     4.2 +++ b/src/main/psp/SDL_psp_main.c	Wed Jun 05 21:23:59 2013 -0700
     4.3 @@ -37,8 +37,6 @@
     4.4     PSP_MAIN_THREAD_STACK_SIZE, etc.
     4.5  */
     4.6  
     4.7 -extern int SDL_main(int argc, char *argv[]);
     4.8 -
     4.9  PSP_MODULE_INFO("SDL App", 0, 1, 1);
    4.10  
    4.11  int sdl_psp_exit_callback(int arg1, int arg2, void *common)
    4.12 @@ -75,6 +73,8 @@
    4.13      /* Register sceKernelExitGame() to be called when we exit */
    4.14      atexit(sceKernelExitGame);
    4.15  
    4.16 +    SDL_SetMainReady();
    4.17 +
    4.18      (void)SDL_main(argc, argv);
    4.19      return 0;
    4.20  }
     5.1 --- a/src/main/windows/SDL_windows_main.c	Wed Jun 05 21:14:26 2013 -0700
     5.2 +++ b/src/main/windows/SDL_windows_main.c	Wed Jun 05 21:23:59 2013 -0700
     5.3 @@ -134,6 +134,8 @@
     5.4  {
     5.5      int status;
     5.6  
     5.7 +    SDL_SetMainReady();
     5.8 +
     5.9      /* Run the application main() code */
    5.10      status = SDL_main(argc, argv);
    5.11  
     6.1 --- a/src/video/uikit/SDL_uikitappdelegate.m	Wed Jun 05 21:14:26 2013 -0700
     6.2 +++ b/src/video/uikit/SDL_uikitappdelegate.m	Wed Jun 05 21:23:59 2013 -0700
     6.3 @@ -37,7 +37,6 @@
     6.4  #undef main
     6.5  #endif
     6.6  
     6.7 -extern int SDL_main(int argc, char *argv[]);
     6.8  static int forward_argc;
     6.9  static char **forward_argv;
    6.10  static int exit_status;
    6.11 @@ -187,6 +186,8 @@
    6.12  
    6.13  - (void)postFinishLaunch
    6.14  {
    6.15 +    SDL_SetMainReady();
    6.16 +
    6.17      /* run the user's application, passing argc and argv */
    6.18      SDL_iPhoneSetEventPump(SDL_TRUE);
    6.19      exit_status = SDL_main(forward_argc, forward_argv);