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

This commit is contained in:
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->nbuttons = 0;
@autoreleasepool {
if (motionManager == nil) {
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. */
motionManager.accelerometerUpdateInterval = 0.1;
[motionManager startAccelerometerUpdates];
}
return 0;
}
@ -113,11 +115,13 @@ static void SDL_SYS_AccelerometerUpdate(SDL_Joystick * joystick)
const SInt16 maxsint16 = 0x7FFF;
CMAcceleration accel;
@autoreleasepool {
if (!motionManager.accelerometerActive) {
return;
}
accel = [[motionManager accelerometerData] acceleration];
accel = motionManager.accelerometerData.acceleration;
}
/*
Convert accelerometer data from floating point to Sint16, which is what
@ -161,7 +165,9 @@ SDL_SYS_JoystickUpdate(SDL_Joystick * joystick)
void
SDL_SYS_JoystickClose(SDL_Joystick * joystick)
{
@autoreleasepool {
[motionManager stopAccelerometerUpdates];
}
joystick->closed = 1;
}
@ -169,10 +175,12 @@ SDL_SYS_JoystickClose(SDL_Joystick * joystick)
void
SDL_SYS_JoystickQuit(void)
{
@autoreleasepool {
if (motionManager != nil) {
[motionManager release];
motionManager = nil;
}
}
numjoysticks = 0;
}

View File

@ -50,11 +50,12 @@ SDL_UIKit_UpdateBatteryMonitoring(void)
SDL_bool
SDL_GetPowerInfo_UIKit(SDL_PowerState * state, int *seconds, int *percent)
{
@autoreleasepool {
UIDevice *uidev = [UIDevice currentDevice];
if (!SDL_UIKitLastPowerInfoQuery) {
SDL_assert([uidev isBatteryMonitoringEnabled] == NO);
[uidev setBatteryMonitoringEnabled:YES];
SDL_assert(uidev.isBatteryMonitoringEnabled == NO);
uidev.batteryMonitoringEnabled = YES;
}
/* 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. */
switch ([uidev batteryState])
{
switch (uidev.batteryState) {
case UIDeviceBatteryStateCharging:
*state = SDL_POWERSTATE_CHARGING;
break;
@ -86,9 +86,10 @@ SDL_GetPowerInfo_UIKit(SDL_PowerState * state, int *seconds, int *percent)
break;
}
const float level = [uidev batteryLevel];
const float level = uidev.batteryLevel;
*percent = ( (level < 0.0f) ? -1 : ((int) ((level * 100) + 0.5f)) );
return SDL_TRUE; /* always the definitive answer on iOS. */
}
}
#endif /* SDL_POWER_UIKIT */

View File

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

View File

@ -52,6 +52,7 @@ UIKit_GL_GetProcAddress(_THIS, const char *proc)
int
UIKit_GL_MakeCurrent(_THIS, SDL_Window * window, SDL_GLContext context)
{
@autoreleasepool {
if (context) {
SDL_WindowData *data = (SDL_WindowData *)window->driverdata;
[data->view setCurrentContext];
@ -59,6 +60,7 @@ UIKit_GL_MakeCurrent(_THIS, SDL_Window * window, SDL_GLContext context)
else {
[EAGLContext setCurrentContext: nil];
}
}
return 0;
}
@ -67,12 +69,14 @@ void UIKit_GL_GetDrawableSize(_THIS, SDL_Window * window, int * w, int * h)
{
SDL_WindowData *data = (SDL_WindowData *)window->driverdata;
@autoreleasepool {
if (w) {
*w = data->view.backingWidth;
}
if (h) {
*h = data->view.backingHeight;
}
}
}
@ -92,6 +96,7 @@ UIKit_GL_LoadLibrary(_THIS, const char *path)
void UIKit_GL_SwapWindow(_THIS, SDL_Window * window)
{
@autoreleasepool {
#if SDL_POWER_UIKIT
/* Check once a frame to see if we should turn off the battery monitor. */
SDL_UIKit_UpdateBatteryMonitoring();
@ -108,11 +113,13 @@ void UIKit_GL_SwapWindow(_THIS, SDL_Window * window)
We don't pump events here because we don't want iOS application events
(low memory, terminate, etc.) to happen inside low level rendering.
*/
}
}
SDL_GLContext
UIKit_GL_CreateContext(_THIS, SDL_Window * window)
{
@autoreleasepool {
SDL_uikitopenglview *view;
SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
UIWindow *uiwindow = data->uiwindow;
@ -175,11 +182,13 @@ UIKit_GL_CreateContext(_THIS, SDL_Window * window)
}
return view;
}
}
void
UIKit_GL_DeleteContext(_THIS, SDL_GLContext context)
{
@autoreleasepool {
/* the delegate has retained the view, this will release him */
SDL_uikitopenglview *view = (SDL_uikitopenglview *)context;
if (view->viewcontroller) {
@ -192,6 +201,7 @@ UIKit_GL_DeleteContext(_THIS, SDL_GLContext context)
}
[view removeFromSuperview];
[view release];
}
}
#endif /* SDL_VIDEO_DRIVER_UIKIT */

View File

@ -318,28 +318,34 @@ SDL_bool UIKit_HasScreenKeyboardSupport(_THIS)
void UIKit_ShowScreenKeyboard(_THIS, SDL_Window *window)
{
@autoreleasepool {
SDL_uikitview *view = getWindowView(window);
if (view != nil) {
[view showKeyboard];
}
}
}
void UIKit_HideScreenKeyboard(_THIS, SDL_Window *window)
{
@autoreleasepool {
SDL_uikitview *view = getWindowView(window);
if (view != nil) {
[view hideKeyboard];
}
}
}
SDL_bool UIKit_IsScreenKeyboardShown(_THIS, SDL_Window *window)
{
@autoreleasepool {
SDL_uikitview *view = getWindowView(window);
if (view == nil) {
return 0;
}
return view.isKeyboardVisible;
}
}
@ -424,12 +430,14 @@ UIKit_SetTextInputRect(_THIS, SDL_Rect *rect)
return;
}
@autoreleasepool {
SDL_uikitview *view = getWindowView(SDL_GetFocusWindow());
if (view == nil) {
return ;
return;
}
view.textInputRect = *rect;
}
}

View File

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