From f9574e350c9634b5a4f9849fdfe66fbd84747b12 Mon Sep 17 00:00:00 2001 From: "Thiago Marcos P. Santos" Date: Thu, 14 Jan 2016 18:40:51 +0200 Subject: [PATCH] [core] Fix crash on transitionFrameFn callback This is a tricky one: We are storing a lambda at the member variable `transitionFrameFn` and capturing `this` at this lambda function. At some point when running the callback, we set `transitionFrameFn` to `nullptr`, effectively destroying the callback that is being executed. After this point, `this` is no longer valid, as the memory gets cleared and it might point to the correct location (or not). That is why it works on some compilers but not on g++ 5.3.1. Touching any `this->[smth]` is undefined after that. The fix is just move `transitionFrameFn = nullptr` to the last thing we do on the callback. This behaves similarly to calling `delete this` on a class method. --- src/mbgl/map/transform.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/mbgl/map/transform.cpp b/src/mbgl/map/transform.cpp index b38520b12..98b080448 100644 --- a/src/mbgl/map/transform.cpp +++ b/src/mbgl/map/transform.cpp @@ -539,8 +539,11 @@ void Transform::startTransition(const CameraOptions& camera, view.notifyMapChange(MapChangeRegionIsChanging); } else { transitionFinishFn(); - transitionFrameFn = nullptr; transitionFinishFn = nullptr; + + // This callback gets destroyed here, + // we can only return after this point. + transitionFrameFn = nullptr; } return result; };