mirror of
https://github.com/Relintai/sdl2_frt.git
synced 2024-12-16 11:06:49 +01:00
SDL_windows_main: use HeapAlloc/HeapFree for command line arguments
If a developer uses SDL_SetMemoryFunctions, we can't rely on SDL_free() working when SDL_main() returns. Signed-off-by: Steven Noonan <steven@valvesoftware.com> Signed-off-by: Sam Lantinga <slouken@libsdl.org>
This commit is contained in:
parent
17d8479d98
commit
f1ad942a11
@ -49,16 +49,29 @@ main_getcmdline(void)
|
|||||||
return OutOfMemory();
|
return OutOfMemory();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Note that we need to be careful about how we allocate/free memory here.
|
||||||
|
* If the application calls SDL_SetMemoryFunctions(), we can't rely on
|
||||||
|
* SDL_free() to use the same allocator after SDL_main() returns.
|
||||||
|
*/
|
||||||
|
|
||||||
/* Parse it into argv and argc */
|
/* Parse it into argv and argc */
|
||||||
argv = (char **)SDL_calloc(argc + 1, sizeof(*argv));
|
argv = (char **)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (argc + 1) * sizeof(*argv));
|
||||||
if (!argv) {
|
if (!argv) {
|
||||||
return OutOfMemory();
|
return OutOfMemory();
|
||||||
}
|
}
|
||||||
for (i = 0; i < argc; ++i) {
|
for (i = 0; i < argc; ++i) {
|
||||||
argv[i] = WIN_StringToUTF8W(argvw[i]);
|
DWORD len;
|
||||||
|
char *arg = WIN_StringToUTF8W(argvw[i]);
|
||||||
|
if (!arg) {
|
||||||
|
return OutOfMemory();
|
||||||
|
}
|
||||||
|
len = (DWORD)SDL_strlen(arg);
|
||||||
|
argv[i] = (char *)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, len + 1);
|
||||||
if (!argv[i]) {
|
if (!argv[i]) {
|
||||||
return OutOfMemory();
|
return OutOfMemory();
|
||||||
}
|
}
|
||||||
|
CopyMemory(argv[i], arg, len);
|
||||||
|
SDL_free(arg);
|
||||||
}
|
}
|
||||||
argv[i] = NULL;
|
argv[i] = NULL;
|
||||||
LocalFree(argvw);
|
LocalFree(argvw);
|
||||||
@ -70,9 +83,9 @@ main_getcmdline(void)
|
|||||||
|
|
||||||
/* Free argv, to avoid memory leak */
|
/* Free argv, to avoid memory leak */
|
||||||
for (i = 0; i < argc; ++i) {
|
for (i = 0; i < argc; ++i) {
|
||||||
SDL_free(argv[i]);
|
HeapFree(GetProcessHeap(), 0, argv[i]);
|
||||||
}
|
}
|
||||||
SDL_free(argv);
|
HeapFree(GetProcessHeap(), 0, argv);
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user