Added missing autorelease pool blocks in UIKit backend code. Fixes memory leak issues, especially in SDL_video.

main
Alex Szpakowski 2014-07-29 00:36:12 -03:00
parent 31257842ec
commit caad673f06
6 changed files with 322 additions and 269 deletions

View File

@ -90,6 +90,7 @@ SDL_SYS_JoystickOpen(SDL_Joystick * joystick, int device_index)
joystick->nballs = 0; joystick->nballs = 0;
joystick->nbuttons = 0; joystick->nbuttons = 0;
@autoreleasepool {
if (motionManager == nil) { if (motionManager == nil) {
motionManager = [[CMMotionManager alloc] init]; motionManager = [[CMMotionManager alloc] init];
} }
@ -97,6 +98,7 @@ SDL_SYS_JoystickOpen(SDL_Joystick * joystick, int device_index)
/* Shorter times between updates can significantly increase CPU usage. */ /* Shorter times between updates can significantly increase CPU usage. */
motionManager.accelerometerUpdateInterval = 0.1; motionManager.accelerometerUpdateInterval = 0.1;
[motionManager startAccelerometerUpdates]; [motionManager startAccelerometerUpdates];
}
return 0; return 0;
} }
@ -113,11 +115,13 @@ static void SDL_SYS_AccelerometerUpdate(SDL_Joystick * joystick)
const SInt16 maxsint16 = 0x7FFF; const SInt16 maxsint16 = 0x7FFF;
CMAcceleration accel; CMAcceleration accel;
@autoreleasepool {
if (!motionManager.accelerometerActive) { if (!motionManager.accelerometerActive) {
return; return;
} }
accel = [[motionManager accelerometerData] acceleration]; accel = motionManager.accelerometerData.acceleration;
}
/* /*
Convert accelerometer data from floating point to Sint16, which is what Convert accelerometer data from floating point to Sint16, which is what
@ -161,7 +165,9 @@ SDL_SYS_JoystickUpdate(SDL_Joystick * joystick)
void void
SDL_SYS_JoystickClose(SDL_Joystick * joystick) SDL_SYS_JoystickClose(SDL_Joystick * joystick)
{ {
@autoreleasepool {
[motionManager stopAccelerometerUpdates]; [motionManager stopAccelerometerUpdates];
}
joystick->closed = 1; joystick->closed = 1;
} }
@ -169,10 +175,12 @@ SDL_SYS_JoystickClose(SDL_Joystick * joystick)
void void
SDL_SYS_JoystickQuit(void) SDL_SYS_JoystickQuit(void)
{ {
@autoreleasepool {
if (motionManager != nil) { if (motionManager != nil) {
[motionManager release]; [motionManager release];
motionManager = nil; motionManager = nil;
} }
}
numjoysticks = 0; numjoysticks = 0;
} }

View File

@ -50,11 +50,12 @@ SDL_UIKit_UpdateBatteryMonitoring(void)
SDL_bool SDL_bool
SDL_GetPowerInfo_UIKit(SDL_PowerState * state, int *seconds, int *percent) SDL_GetPowerInfo_UIKit(SDL_PowerState * state, int *seconds, int *percent)
{ {
@autoreleasepool {
UIDevice *uidev = [UIDevice currentDevice]; UIDevice *uidev = [UIDevice currentDevice];
if (!SDL_UIKitLastPowerInfoQuery) { if (!SDL_UIKitLastPowerInfoQuery) {
SDL_assert([uidev isBatteryMonitoringEnabled] == NO); SDL_assert(uidev.isBatteryMonitoringEnabled == NO);
[uidev setBatteryMonitoringEnabled:YES]; uidev.batteryMonitoringEnabled = YES;
} }
/* UIKit_GL_SwapWindow() (etc) will check this and disable the battery /* UIKit_GL_SwapWindow() (etc) will check this and disable the battery
@ -66,8 +67,7 @@ SDL_GetPowerInfo_UIKit(SDL_PowerState * state, int *seconds, int *percent)
*seconds = -1; /* no API to estimate this in UIKit. */ *seconds = -1; /* no API to estimate this in UIKit. */
switch ([uidev batteryState]) switch (uidev.batteryState) {
{
case UIDeviceBatteryStateCharging: case UIDeviceBatteryStateCharging:
*state = SDL_POWERSTATE_CHARGING; *state = SDL_POWERSTATE_CHARGING;
break; break;
@ -86,10 +86,11 @@ SDL_GetPowerInfo_UIKit(SDL_PowerState * state, int *seconds, int *percent)
break; break;
} }
const float level = [uidev batteryLevel]; const float level = uidev.batteryLevel;
*percent = ( (level < 0.0f) ? -1 : ((int) ((level * 100) + 0.5f)) ); *percent = ( (level < 0.0f) ? -1 : ((int) ((level * 100) + 0.5f)) );
return SDL_TRUE; /* always the definitive answer on iOS. */ return SDL_TRUE; /* always the definitive answer on iOS. */
} }
}
#endif /* SDL_POWER_UIKIT */ #endif /* SDL_POWER_UIKIT */
#endif /* SDL_POWER_DISABLED */ #endif /* SDL_POWER_DISABLED */

View File

@ -159,11 +159,13 @@ UIKit_IsDisplayLandscape(UIScreen *uiscreen)
int int
UIKit_InitModes(_THIS) UIKit_InitModes(_THIS)
{ {
@autoreleasepool {
for (UIScreen *uiscreen in [UIScreen screens]) { for (UIScreen *uiscreen in [UIScreen screens]) {
if (UIKit_AddDisplay(uiscreen) < 0) { if (UIKit_AddDisplay(uiscreen) < 0) {
return -1; return -1;
} }
} }
}
return 0; return 0;
} }
@ -173,6 +175,7 @@ UIKit_GetDisplayModes(_THIS, SDL_VideoDisplay * display)
{ {
SDL_DisplayData *data = (SDL_DisplayData *) display->driverdata; SDL_DisplayData *data = (SDL_DisplayData *) display->driverdata;
@autoreleasepool {
SDL_bool isLandscape = UIKit_IsDisplayLandscape(data->uiscreen); SDL_bool isLandscape = UIKit_IsDisplayLandscape(data->uiscreen);
SDL_bool addRotation = (data->uiscreen == [UIScreen mainScreen]); SDL_bool addRotation = (data->uiscreen == [UIScreen mainScreen]);
CGFloat scale = data->uiscreen.scale; CGFloat scale = data->uiscreen.scale;
@ -195,6 +198,7 @@ UIKit_GetDisplayModes(_THIS, SDL_VideoDisplay * display)
UIKit_AddDisplayMode(display, w, h, uimode, addRotation); UIKit_AddDisplayMode(display, w, h, uimode, addRotation);
} }
} }
}
int int
UIKit_SetDisplayMode(_THIS, SDL_VideoDisplay * display, SDL_DisplayMode * mode) UIKit_SetDisplayMode(_THIS, SDL_VideoDisplay * display, SDL_DisplayMode * mode)
@ -202,6 +206,7 @@ UIKit_SetDisplayMode(_THIS, SDL_VideoDisplay * display, SDL_DisplayMode * mode)
SDL_DisplayData *data = (SDL_DisplayData *) display->driverdata; SDL_DisplayData *data = (SDL_DisplayData *) display->driverdata;
SDL_DisplayModeData *modedata = (SDL_DisplayModeData *)mode->driverdata; SDL_DisplayModeData *modedata = (SDL_DisplayModeData *)mode->driverdata;
@autoreleasepool {
[data->uiscreen setCurrentMode:modedata->uiscreenmode]; [data->uiscreen setCurrentMode:modedata->uiscreenmode];
if (data->uiscreen == [UIScreen mainScreen]) { if (data->uiscreen == [UIScreen mainScreen]) {
@ -215,6 +220,7 @@ UIKit_SetDisplayMode(_THIS, SDL_VideoDisplay * display, SDL_DisplayMode * mode)
} }
} }
} }
}
return 0; return 0;
} }
@ -224,6 +230,7 @@ UIKit_QuitModes(_THIS)
{ {
/* Release Objective-C objects, so higher level doesn't free() them. */ /* Release Objective-C objects, so higher level doesn't free() them. */
int i, j; int i, j;
@autoreleasepool {
for (i = 0; i < _this->num_displays; i++) { for (i = 0; i < _this->num_displays; i++) {
SDL_VideoDisplay *display = &_this->displays[i]; SDL_VideoDisplay *display = &_this->displays[i];
@ -239,6 +246,7 @@ UIKit_QuitModes(_THIS)
display->driverdata = NULL; display->driverdata = NULL;
} }
} }
}
#endif /* SDL_VIDEO_DRIVER_UIKIT */ #endif /* SDL_VIDEO_DRIVER_UIKIT */

View File

@ -52,6 +52,7 @@ UIKit_GL_GetProcAddress(_THIS, const char *proc)
int int
UIKit_GL_MakeCurrent(_THIS, SDL_Window * window, SDL_GLContext context) UIKit_GL_MakeCurrent(_THIS, SDL_Window * window, SDL_GLContext context)
{ {
@autoreleasepool {
if (context) { if (context) {
SDL_WindowData *data = (SDL_WindowData *)window->driverdata; SDL_WindowData *data = (SDL_WindowData *)window->driverdata;
[data->view setCurrentContext]; [data->view setCurrentContext];
@ -59,6 +60,7 @@ UIKit_GL_MakeCurrent(_THIS, SDL_Window * window, SDL_GLContext context)
else { else {
[EAGLContext setCurrentContext: nil]; [EAGLContext setCurrentContext: nil];
} }
}
return 0; return 0;
} }
@ -67,6 +69,7 @@ void UIKit_GL_GetDrawableSize(_THIS, SDL_Window * window, int * w, int * h)
{ {
SDL_WindowData *data = (SDL_WindowData *)window->driverdata; SDL_WindowData *data = (SDL_WindowData *)window->driverdata;
@autoreleasepool {
if (w) { if (w) {
*w = data->view.backingWidth; *w = data->view.backingWidth;
} }
@ -74,6 +77,7 @@ void UIKit_GL_GetDrawableSize(_THIS, SDL_Window * window, int * w, int * h)
*h = data->view.backingHeight; *h = data->view.backingHeight;
} }
} }
}
int int
@ -92,6 +96,7 @@ UIKit_GL_LoadLibrary(_THIS, const char *path)
void UIKit_GL_SwapWindow(_THIS, SDL_Window * window) void UIKit_GL_SwapWindow(_THIS, SDL_Window * window)
{ {
@autoreleasepool {
#if SDL_POWER_UIKIT #if SDL_POWER_UIKIT
/* Check once a frame to see if we should turn off the battery monitor. */ /* Check once a frame to see if we should turn off the battery monitor. */
SDL_UIKit_UpdateBatteryMonitoring(); SDL_UIKit_UpdateBatteryMonitoring();
@ -109,10 +114,12 @@ void UIKit_GL_SwapWindow(_THIS, SDL_Window * window)
(low memory, terminate, etc.) to happen inside low level rendering. (low memory, terminate, etc.) to happen inside low level rendering.
*/ */
} }
}
SDL_GLContext SDL_GLContext
UIKit_GL_CreateContext(_THIS, SDL_Window * window) UIKit_GL_CreateContext(_THIS, SDL_Window * window)
{ {
@autoreleasepool {
SDL_uikitopenglview *view; SDL_uikitopenglview *view;
SDL_WindowData *data = (SDL_WindowData *) window->driverdata; SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
UIWindow *uiwindow = data->uiwindow; UIWindow *uiwindow = data->uiwindow;
@ -176,10 +183,12 @@ UIKit_GL_CreateContext(_THIS, SDL_Window * window)
return view; return view;
} }
}
void void
UIKit_GL_DeleteContext(_THIS, SDL_GLContext context) UIKit_GL_DeleteContext(_THIS, SDL_GLContext context)
{ {
@autoreleasepool {
/* the delegate has retained the view, this will release him */ /* the delegate has retained the view, this will release him */
SDL_uikitopenglview *view = (SDL_uikitopenglview *)context; SDL_uikitopenglview *view = (SDL_uikitopenglview *)context;
if (view->viewcontroller) { if (view->viewcontroller) {
@ -193,6 +202,7 @@ UIKit_GL_DeleteContext(_THIS, SDL_GLContext context)
[view removeFromSuperview]; [view removeFromSuperview];
[view release]; [view release];
} }
}
#endif /* SDL_VIDEO_DRIVER_UIKIT */ #endif /* SDL_VIDEO_DRIVER_UIKIT */

View File

@ -318,22 +318,27 @@ SDL_bool UIKit_HasScreenKeyboardSupport(_THIS)
void UIKit_ShowScreenKeyboard(_THIS, SDL_Window *window) void UIKit_ShowScreenKeyboard(_THIS, SDL_Window *window)
{ {
@autoreleasepool {
SDL_uikitview *view = getWindowView(window); SDL_uikitview *view = getWindowView(window);
if (view != nil) { if (view != nil) {
[view showKeyboard]; [view showKeyboard];
} }
} }
}
void UIKit_HideScreenKeyboard(_THIS, SDL_Window *window) void UIKit_HideScreenKeyboard(_THIS, SDL_Window *window)
{ {
@autoreleasepool {
SDL_uikitview *view = getWindowView(window); SDL_uikitview *view = getWindowView(window);
if (view != nil) { if (view != nil) {
[view hideKeyboard]; [view hideKeyboard];
} }
} }
}
SDL_bool UIKit_IsScreenKeyboardShown(_THIS, SDL_Window *window) SDL_bool UIKit_IsScreenKeyboardShown(_THIS, SDL_Window *window)
{ {
@autoreleasepool {
SDL_uikitview *view = getWindowView(window); SDL_uikitview *view = getWindowView(window);
if (view == nil) { if (view == nil) {
return 0; return 0;
@ -341,6 +346,7 @@ SDL_bool UIKit_IsScreenKeyboardShown(_THIS, SDL_Window *window)
return view.isKeyboardVisible; return view.isKeyboardVisible;
} }
}
void _uikit_keyboard_update() { void _uikit_keyboard_update() {
@ -424,6 +430,7 @@ UIKit_SetTextInputRect(_THIS, SDL_Rect *rect)
return; return;
} }
@autoreleasepool {
SDL_uikitview *view = getWindowView(SDL_GetFocusWindow()); SDL_uikitview *view = getWindowView(SDL_GetFocusWindow());
if (view == nil) { if (view == nil) {
return; return;
@ -431,6 +438,7 @@ UIKit_SetTextInputRect(_THIS, SDL_Rect *rect)
view.textInputRect = *rect; view.textInputRect = *rect;
} }
}
#endif /* SDL_IPHONE_KEYBOARD */ #endif /* SDL_IPHONE_KEYBOARD */

View File

@ -132,6 +132,7 @@ static int SetupWindowData(_THIS, SDL_Window *window, SDL_uikitwindow *uiwindow,
int int
UIKit_CreateWindow(_THIS, SDL_Window *window) UIKit_CreateWindow(_THIS, SDL_Window *window)
{ {
@autoreleasepool {
SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window); SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
SDL_DisplayData *data = (SDL_DisplayData *) display->driverdata; SDL_DisplayData *data = (SDL_DisplayData *) display->driverdata;
const BOOL external = ([UIScreen mainScreen] != data->uiscreen); const BOOL external = ([UIScreen mainScreen] != data->uiscreen);
@ -211,6 +212,7 @@ UIKit_CreateWindow(_THIS, SDL_Window *window)
[uiwindow release]; [uiwindow release];
return -1; return -1;
} }
}
return 1; return 1;
} }
@ -218,18 +220,22 @@ UIKit_CreateWindow(_THIS, SDL_Window *window)
void void
UIKit_ShowWindow(_THIS, SDL_Window * window) UIKit_ShowWindow(_THIS, SDL_Window * window)
{ {
@autoreleasepool {
UIWindow *uiwindow = ((SDL_WindowData *) window->driverdata)->uiwindow; UIWindow *uiwindow = ((SDL_WindowData *) window->driverdata)->uiwindow;
[uiwindow makeKeyAndVisible]; [uiwindow makeKeyAndVisible];
} }
}
void void
UIKit_HideWindow(_THIS, SDL_Window * window) UIKit_HideWindow(_THIS, SDL_Window * window)
{ {
@autoreleasepool {
UIWindow *uiwindow = ((SDL_WindowData *) window->driverdata)->uiwindow; UIWindow *uiwindow = ((SDL_WindowData *) window->driverdata)->uiwindow;
uiwindow.hidden = YES; uiwindow.hidden = YES;
} }
}
void void
UIKit_RaiseWindow(_THIS, SDL_Window * window) UIKit_RaiseWindow(_THIS, SDL_Window * window)
@ -286,31 +292,39 @@ UIKit_UpdateWindowBorder(_THIS, SDL_Window * window)
void void
UIKit_SetWindowBordered(_THIS, SDL_Window * window, SDL_bool bordered) UIKit_SetWindowBordered(_THIS, SDL_Window * window, SDL_bool bordered)
{ {
@autoreleasepool {
UIKit_UpdateWindowBorder(_this, window); UIKit_UpdateWindowBorder(_this, window);
} }
}
void void
UIKit_SetWindowFullscreen(_THIS, SDL_Window * window, SDL_VideoDisplay * display, SDL_bool fullscreen) UIKit_SetWindowFullscreen(_THIS, SDL_Window * window, SDL_VideoDisplay * display, SDL_bool fullscreen)
{ {
@autoreleasepool {
UIKit_UpdateWindowBorder(_this, window); UIKit_UpdateWindowBorder(_this, window);
} }
}
void void
UIKit_DestroyWindow(_THIS, SDL_Window * window) UIKit_DestroyWindow(_THIS, SDL_Window * window)
{ {
SDL_WindowData *data = (SDL_WindowData *)window->driverdata; SDL_WindowData *data = (SDL_WindowData *)window->driverdata;
@autoreleasepool {
if (data) { if (data) {
[data->viewcontroller release]; [data->viewcontroller release];
[data->uiwindow release]; [data->uiwindow release];
SDL_free(data); SDL_free(data);
} }
}
window->driverdata = NULL; window->driverdata = NULL;
} }
SDL_bool SDL_bool
UIKit_GetWindowWMInfo(_THIS, SDL_Window * window, SDL_SysWMinfo * info) UIKit_GetWindowWMInfo(_THIS, SDL_Window * window, SDL_SysWMinfo * info)
{ {
@autoreleasepool {
UIWindow *uiwindow = ((SDL_WindowData *) window->driverdata)->uiwindow; UIWindow *uiwindow = ((SDL_WindowData *) window->driverdata)->uiwindow;
if (info->version.major <= SDL_MAJOR_VERSION) { if (info->version.major <= SDL_MAJOR_VERSION) {
@ -323,6 +337,7 @@ UIKit_GetWindowWMInfo(_THIS, SDL_Window * window, SDL_SysWMinfo * info)
return SDL_FALSE; return SDL_FALSE;
} }
} }
}
int int
SDL_iPhoneSetAnimationCallback(SDL_Window * window, int interval, void (*callback)(void*), void *callbackParam) SDL_iPhoneSetAnimationCallback(SDL_Window * window, int interval, void (*callback)(void*), void *callbackParam)
@ -333,7 +348,10 @@ SDL_iPhoneSetAnimationCallback(SDL_Window * window, int interval, void (*callbac
return SDL_SetError("Invalid window or view not set"); return SDL_SetError("Invalid window or view not set");
} }
@autoreleasepool {
[data->view setAnimationCallback:interval callback:callback callbackParam:callbackParam]; [data->view setAnimationCallback:interval callback:callback callbackParam:callbackParam];
}
return 0; return 0;
} }