src/main/windows/SDL_windows_main.c
author Philipp Wiesemann <philipp.wiesemann@arcor.de>
Wed, 29 Oct 2014 20:20:47 +0100
changeset 9201 21d9f9babb30
parent 9016 648dbad92045
child 9246 a761913e5e91
permissions -rw-r--r--
Fixed bug 2647 - Memory leak in SDL_AddHintCallback function - SDL_hints.c

Nitz

Variable entry going out of scope leaks the storage it points to, at:

/* Need to add a hint entry for this watcher */
hint = (SDL_Hint *)SDL_malloc(sizeof(*hint));
if (!hint) {
return;
}

Patch is attached.
     1 /*
     2     SDL_windows_main.c, placed in the public domain by Sam Lantinga  4/13/98
     3 
     4     The WinMain function -- calls your program's main() function
     5 */
     6 #include "SDL_config.h"
     7 
     8 #ifdef __WIN32__
     9 
    10 /* Include this so we define UNICODE properly */
    11 #include "../../core/windows/SDL_windows.h"
    12 
    13 /* Include the SDL main definition header */
    14 #include "SDL.h"
    15 #include "SDL_main.h"
    16 
    17 #ifdef main
    18 #  undef main
    19 #endif /* main */
    20 
    21 static void
    22 UnEscapeQuotes(char *arg)
    23 {
    24     char *last = NULL;
    25 
    26     while (*arg) {
    27         if (*arg == '"' && (last != NULL && *last == '\\')) {
    28             char *c_curr = arg;
    29             char *c_last = last;
    30 
    31             while (*c_curr) {
    32                 *c_last = *c_curr;
    33                 c_last = c_curr;
    34                 c_curr++;
    35             }
    36             *c_last = '\0';
    37         }
    38         last = arg;
    39         arg++;
    40     }
    41 }
    42 
    43 /* Parse a command line buffer into arguments */
    44 static int
    45 ParseCommandLine(char *cmdline, char **argv)
    46 {
    47     char *bufp;
    48     char *lastp = NULL;
    49     int argc, last_argc;
    50 
    51     argc = last_argc = 0;
    52     for (bufp = cmdline; *bufp;) {
    53         /* Skip leading whitespace */
    54         while (SDL_isspace(*bufp)) {
    55             ++bufp;
    56         }
    57         /* Skip over argument */
    58         if (*bufp == '"') {
    59             ++bufp;
    60             if (*bufp) {
    61                 if (argv) {
    62                     argv[argc] = bufp;
    63                 }
    64                 ++argc;
    65             }
    66             /* Skip over word */
    67             lastp = bufp;
    68             while (*bufp && (*bufp != '"' || *lastp == '\\')) {
    69                 lastp = bufp;
    70                 ++bufp;
    71             }
    72         } else {
    73             if (*bufp) {
    74                 if (argv) {
    75                     argv[argc] = bufp;
    76                 }
    77                 ++argc;
    78             }
    79             /* Skip over word */
    80             while (*bufp && !SDL_isspace(*bufp)) {
    81                 ++bufp;
    82             }
    83         }
    84         if (*bufp) {
    85             if (argv) {
    86                 *bufp = '\0';
    87             }
    88             ++bufp;
    89         }
    90 
    91         /* Strip out \ from \" sequences */
    92         if (argv && last_argc != argc) {
    93             UnEscapeQuotes(argv[last_argc]);
    94         }
    95         last_argc = argc;
    96     }
    97     if (argv) {
    98         argv[argc] = NULL;
    99     }
   100     return (argc);
   101 }
   102 
   103 /* Pop up an out of memory message, returns to Windows */
   104 static BOOL
   105 OutOfMemory(void)
   106 {
   107     SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Fatal Error", "Out of memory - aborting", NULL);
   108     return FALSE;
   109 }
   110 
   111 #if defined(_MSC_VER)
   112 /* The VC++ compiler needs main defined */
   113 #define console_main main
   114 #endif
   115 
   116 /* This is where execution begins [console apps] */
   117 int
   118 console_main(int argc, char *argv[])
   119 {
   120     SDL_SetMainReady();
   121 
   122     /* Run the application main() code */
   123     return SDL_main(argc, argv);
   124 }
   125 
   126 /* This is where execution begins [windowed apps] */
   127 int WINAPI
   128 WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR szCmdLine, int sw)
   129 {
   130     char **argv;
   131     int argc;
   132     char *cmdline;
   133 
   134     /* Grab the command line */
   135     TCHAR *text = GetCommandLine();
   136 #if UNICODE
   137     cmdline = SDL_iconv_string("UTF-8", "UCS-2-INTERNAL", (char *)(text), (SDL_wcslen(text)+1)*sizeof(WCHAR));
   138 #else
   139     cmdline = SDL_strdup(text);
   140 #endif
   141     if (cmdline == NULL) {
   142         return OutOfMemory();
   143     }
   144 
   145     /* Parse it into argv and argc */
   146     argc = ParseCommandLine(cmdline, NULL);
   147     argv = SDL_stack_alloc(char *, argc + 1);
   148     if (argv == NULL) {
   149         return OutOfMemory();
   150     }
   151     ParseCommandLine(cmdline, argv);
   152 
   153     /* Run the main program */
   154     console_main(argc, argv);
   155 
   156     SDL_stack_free(argv);
   157 
   158     SDL_free(cmdline);
   159 
   160     /* Hush little compiler, don't you cry... */
   161     return 0;
   162 }
   163 
   164 #endif /* __WIN32__ */
   165 
   166 /* vi: set ts=4 sw=4 expandtab: */