summaryrefslogtreecommitdiffstats
path: root/src/plugins/sensors/ios/iosaccelerometer.mm
diff options
context:
space:
mode:
authorRichard Moe Gustavsen <richard.gustavsen@digia.com>2013-03-21 17:09:23 +0100
committerThe Qt Project <gerrit-noreply@qt-project.org>2013-03-22 12:21:22 +0100
commitbfe01716dd62fb47f51fda33490a3a4f2528df76 (patch)
tree21d8c323d6240e5941e926ffa2b42bb315078b5d /src/plugins/sensors/ios/iosaccelerometer.mm
parent08cae6047c607793a1dae9c1a20e3184602c1908 (diff)
iOS: add QPointer guard to accelerometer and gyro
Since we process data from the sensors using a callback queued to the application operation queue, we need to check that the qt sensor is still alive before accessing it. Change-Id: I697d72f94aedec34b125006d6405428e282bfc0d Reviewed-by: Tor Arne Vestbø <tor.arne.vestbo@digia.com>
Diffstat (limited to 'src/plugins/sensors/ios/iosaccelerometer.mm')
-rw-r--r--src/plugins/sensors/ios/iosaccelerometer.mm31
1 files changed, 19 insertions, 12 deletions
diff --git a/src/plugins/sensors/ios/iosaccelerometer.mm b/src/plugins/sensors/ios/iosaccelerometer.mm
index ab6e8c45..b657b507 100644
--- a/src/plugins/sensors/ios/iosaccelerometer.mm
+++ b/src/plugins/sensors/ios/iosaccelerometer.mm
@@ -39,11 +39,12 @@
**
****************************************************************************/
-#include "iosaccelerometer.h"
-#include "iosmotionmanager.h"
-
#include <UIKit/UIAccelerometer.h>
#include <CoreMotion/CMMotionManager.h>
+#include <QPointer>
+
+#include "iosaccelerometer.h"
+#include "iosmotionmanager.h"
char const * const IOSAccelerometer::id("ios.accelerometer");
@@ -70,17 +71,23 @@ void IOSAccelerometer::start()
int hz = sensor()->dataRate();
motionManager.accelerometerUpdateInterval = (hz == 0) ? 0 : 1. / hz;
+ QPointer<QObject> self = this;
[motionManager startAccelerometerUpdatesToQueue:m_updateQueue withHandler:^(CMAccelerometerData *data, NSError *error) {
+ // NSOperationQueue is multi-threaded, so we process the data by queuing a callback to
+ // the main application queue. By the time the callback executes, IOSAccelerometer might
+ // have been deleted, so we need an extra QPointer check for that:
dispatch_async(dispatch_get_main_queue(), ^{
- Q_UNUSED(error);
- // Convert from NSTimeInterval to microseconds and G to m/s2, and flip axes:
- CMAcceleration acc = data.acceleration;
- const qreal G = 9.8066;
- m_reading.setTimestamp(quint64(data.timestamp * 1000000));
- m_reading.setX(qreal(acc.x) * G * -1);
- m_reading.setY(qreal(acc.y) * G * -1);
- m_reading.setZ(qreal(acc.z) * G * -1);
- newReadingAvailable();
+ if (self) {
+ Q_UNUSED(error);
+ // Convert from NSTimeInterval to microseconds and G to m/s2, and flip axes:
+ CMAcceleration acc = data.acceleration;
+ const qreal G = 9.8066;
+ m_reading.setTimestamp(quint64(data.timestamp * 1000000));
+ m_reading.setX(qreal(acc.x) * G * -1);
+ m_reading.setY(qreal(acc.y) * G * -1);
+ m_reading.setZ(qreal(acc.z) * G * -1);
+ newReadingAvailable();
+ }
});
}];
}