Skip to content
Permalink
Browse files

Added missing autorelease pool blocks in UIKit backend code. Fixes me…

…mory leak issues, especially in SDL_video.
  • Loading branch information
slime73 committed Jul 29, 2014
1 parent 3125784 commit caad673f06113c0fdf99b6c53e4801582a6c2081
@@ -90,14 +90,16 @@ SDL_JoystickID SDL_SYS_GetInstanceIdOfDeviceIndex(int device_index)
joystick->nballs = 0;
joystick->nbuttons = 0;

if (motionManager == nil) {
motionManager = [[CMMotionManager alloc] init];
@autoreleasepool {
if (motionManager == nil) {
motionManager = [[CMMotionManager alloc] init];
}

/* Shorter times between updates can significantly increase CPU usage. */
motionManager.accelerometerUpdateInterval = 0.1;
[motionManager startAccelerometerUpdates];
}

/* 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;

if (!motionManager.accelerometerActive) {
return;
}
@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,17 +165,21 @@ static void SDL_SYS_AccelerometerUpdate(SDL_Joystick * joystick)
void
SDL_SYS_JoystickClose(SDL_Joystick * joystick)
{
[motionManager stopAccelerometerUpdates];
@autoreleasepool {
[motionManager stopAccelerometerUpdates];
}
joystick->closed = 1;
}

/* Function to perform any system-specific joystick related cleanup */
void
SDL_SYS_JoystickQuit(void)
{
if (motionManager != nil) {
[motionManager release];
motionManager = nil;
@autoreleasepool {
if (motionManager != nil) {
[motionManager release];
motionManager = nil;
}
}

numjoysticks = 0;
@@ -50,24 +50,24 @@
SDL_bool
SDL_GetPowerInfo_UIKit(SDL_PowerState * state, int *seconds, int *percent)
{
UIDevice *uidev = [UIDevice currentDevice];
@autoreleasepool {
UIDevice *uidev = [UIDevice currentDevice];

if (!SDL_UIKitLastPowerInfoQuery) {
SDL_assert([uidev isBatteryMonitoringEnabled] == NO);
[uidev setBatteryMonitoringEnabled:YES];
}
if (!SDL_UIKitLastPowerInfoQuery) {
SDL_assert(uidev.isBatteryMonitoringEnabled == NO);
uidev.batteryMonitoringEnabled = YES;
}

/* UIKit_GL_SwapWindow() (etc) will check this and disable the battery
* monitoring if the app hasn't queried it in the last X seconds.
* Apparently monitoring the battery burns battery life. :)
* Apple's docs say not to monitor the battery unless you need it.
*/
SDL_UIKitLastPowerInfoQuery = SDL_GetTicks();
/* UIKit_GL_SwapWindow() (etc) will check this and disable the battery
* monitoring if the app hasn't queried it in the last X seconds.
* Apparently monitoring the battery burns battery life. :)
* Apple's docs say not to monitor the battery unless you need it.
*/
SDL_UIKitLastPowerInfoQuery = SDL_GetTicks();

*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:
*state = SDL_POWERSTATE_CHARGING;
break;
@@ -84,11 +84,12 @@
default:
*state = SDL_POWERSTATE_UNKNOWN;
break;
}
}

const float level = [uidev batteryLevel];
*percent = ( (level < 0.0f) ? -1 : ((int) ((level * 100) + 0.5f)) );
return SDL_TRUE; /* always the definitive answer on iOS. */
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 */
@@ -159,9 +159,11 @@
int
UIKit_InitModes(_THIS)
{
for (UIScreen *uiscreen in [UIScreen screens]) {
if (UIKit_AddDisplay(uiscreen) < 0) {
return -1;
@autoreleasepool {
for (UIScreen *uiscreen in [UIScreen screens]) {
if (UIKit_AddDisplay(uiscreen) < 0) {
return -1;
}
}
}

@@ -173,26 +175,28 @@
{
SDL_DisplayData *data = (SDL_DisplayData *) display->driverdata;

SDL_bool isLandscape = UIKit_IsDisplayLandscape(data->uiscreen);
SDL_bool addRotation = (data->uiscreen == [UIScreen mainScreen]);
CGFloat scale = data->uiscreen.scale;

for (UIScreenMode *uimode in [data->uiscreen availableModes]) {
/* The size of a UIScreenMode is in pixels, but we deal exclusively in
* points (except in SDL_GL_GetDrawableSize.) */
CGSize size = [uimode size];
int w = (int)(size.width / scale);
int h = (int)(size.height / scale);

/* Make sure the width/height are oriented correctly */
if (isLandscape != (w > h)) {
int tmp = w;
w = h;
h = tmp;
}
@autoreleasepool {
SDL_bool isLandscape = UIKit_IsDisplayLandscape(data->uiscreen);
SDL_bool addRotation = (data->uiscreen == [UIScreen mainScreen]);
CGFloat scale = data->uiscreen.scale;

for (UIScreenMode *uimode in [data->uiscreen availableModes]) {
/* The size of a UIScreenMode is in pixels, but we deal exclusively in
* points (except in SDL_GL_GetDrawableSize.) */
CGSize size = [uimode size];
int w = (int)(size.width / scale);
int h = (int)(size.height / scale);

/* Make sure the width/height are oriented correctly */
if (isLandscape != (w > h)) {
int tmp = w;
w = h;
h = tmp;
}

/* Add the native screen resolution. */
UIKit_AddDisplayMode(display, w, h, uimode, addRotation);
/* Add the native screen resolution. */
UIKit_AddDisplayMode(display, w, h, uimode, addRotation);
}
}
}

@@ -202,16 +206,18 @@
SDL_DisplayData *data = (SDL_DisplayData *) display->driverdata;
SDL_DisplayModeData *modedata = (SDL_DisplayModeData *)mode->driverdata;

[data->uiscreen setCurrentMode:modedata->uiscreenmode];

if (data->uiscreen == [UIScreen mainScreen]) {
if (mode->w > mode->h) {
if (!UIKit_IsDisplayLandscape(data->uiscreen)) {
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:NO];
}
} else if (mode->w < mode->h) {
if (UIKit_IsDisplayLandscape(data->uiscreen)) {
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:NO];
@autoreleasepool {
[data->uiscreen setCurrentMode:modedata->uiscreenmode];

if (data->uiscreen == [UIScreen mainScreen]) {
if (mode->w > mode->h) {
if (!UIKit_IsDisplayLandscape(data->uiscreen)) {
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:NO];
}
} else if (mode->w < mode->h) {
if (UIKit_IsDisplayLandscape(data->uiscreen)) {
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:NO];
}
}
}
}
@@ -224,19 +230,21 @@
{
/* Release Objective-C objects, so higher level doesn't free() them. */
int i, j;
for (i = 0; i < _this->num_displays; i++) {
SDL_VideoDisplay *display = &_this->displays[i];
@autoreleasepool {
for (i = 0; i < _this->num_displays; i++) {
SDL_VideoDisplay *display = &_this->displays[i];

UIKit_FreeDisplayModeData(&display->desktop_mode);
for (j = 0; j < display->num_display_modes; j++) {
SDL_DisplayMode *mode = &display->display_modes[j];
UIKit_FreeDisplayModeData(mode);
}

UIKit_FreeDisplayModeData(&display->desktop_mode);
for (j = 0; j < display->num_display_modes; j++) {
SDL_DisplayMode *mode = &display->display_modes[j];
UIKit_FreeDisplayModeData(mode);
SDL_DisplayData *data = (SDL_DisplayData *) display->driverdata;
[data->uiscreen release];
SDL_free(data);
display->driverdata = NULL;
}

SDL_DisplayData *data = (SDL_DisplayData *) display->driverdata;
[data->uiscreen release];
SDL_free(data);
display->driverdata = NULL;
}
}

0 comments on commit caad673

Please sign in to comment.