Dynamic API: don't leak if a library loaded but didn't have the entry point.

This commit is contained in:
Ryan C. Gordon 2015-01-05 01:07:36 -05:00
parent f5fa9a59d0
commit 2a1e422375
1 changed files with 21 additions and 4 deletions

View File

@ -206,7 +206,14 @@ SDL_DYNAPI_entry(Uint32 apiver, void *table, Uint32 tablesize)
static SDL_INLINE void *get_sdlapi_entry(const char *fname, const char *sym) static SDL_INLINE void *get_sdlapi_entry(const char *fname, const char *sym)
{ {
HANDLE lib = LoadLibraryA(fname); HANDLE lib = LoadLibraryA(fname);
return lib ? GetProcAddress(lib, sym) : NULL; void *retval = NULL;
if (lib) {
retval = GetProcAddress(lib, sym);
if (retval == NULL) {
FreeLibrary(lib);
}
}
return retval;
} }
#elif defined(__HAIKU__) #elif defined(__HAIKU__)
@ -215,9 +222,12 @@ static SDL_INLINE void *get_sdlapi_entry(const char *fname, const char *sym)
{ {
image_id lib = load_add_on(fname); image_id lib = load_add_on(fname);
void *retval = NULL; void *retval = NULL;
if ((lib < 0) || (get_image_symbol(lib, sym, B_SYMBOL_TYPE_TEXT, &retval) != B_NO_ERROR)) { if (lib >= 0) {
if (get_image_symbol(lib, sym, B_SYMBOL_TYPE_TEXT, &retval) != B_NO_ERROR) {
unload_add_on(lib);
retval = NULL; retval = NULL;
} }
}
return retval; return retval;
} }
#elif defined(unix) || defined(__unix__) || defined(__APPLE__) #elif defined(unix) || defined(__unix__) || defined(__APPLE__)
@ -225,7 +235,14 @@ static SDL_INLINE void *get_sdlapi_entry(const char *fname, const char *sym)
static SDL_INLINE void *get_sdlapi_entry(const char *fname, const char *sym) static SDL_INLINE void *get_sdlapi_entry(const char *fname, const char *sym)
{ {
void *lib = dlopen(fname, RTLD_NOW | RTLD_LOCAL); void *lib = dlopen(fname, RTLD_NOW | RTLD_LOCAL);
return lib ? dlsym(lib, sym) : NULL; void *retval = NULL;
if (lib != NULL) {
retval = dlsym(lib, sym);
if (retval == NULL) {
dlclose(lib);
}
}
return retval;
} }
#else #else
#error Please define your platform. #error Please define your platform.