Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Explicitly initialize default runloop on Darwin #6613

Merged
merged 2 commits into from
Oct 6, 2016

Conversation

kkaefer
Copy link
Contributor

@kkaefer kkaefer commented Oct 6, 2016

#6579 introduced a unit test crash on macOS that exposed a larger issue: In the Darwin RunLoop implementation, we are statically initializing a RunLoop object for the main thread. However, this object is overwritten, and subsequently destructed/nulled by most unit tests that use their own RunLoop. This moves the static initialization to the macOS and iOS SDK. The GLFW implementation which also runs on macOS already creates its own RunLoop.

/cc @jfirebaugh

@mention-bot
Copy link

@kkaefer, thanks for your PR! By analyzing the history of the files in this pull request, we identified @jfirebaugh, @1ec5 and @incanus to be potential reviewers.

@@ -22,6 +21,7 @@ RunLoop* RunLoop::Get() {

RunLoop::RunLoop(Type)
: impl(std::make_unique<Impl>()) {
assert(!current.get());
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Allows us to quickly catch duplicate RunLoops in the future.

/// Initializes the run loop shim that lives on the main thread.
void MGLinitializeRunLoop() {
static mbgl::util::RunLoop mainRunLoop;
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Static function to avoid the fiasco

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also means that this RunLoop isn't destructed. We could use weak_ptr/shared_ptr and tie them to the lifetime of the MGLMapView object

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another option would’ve been to have this be a global static, unset, and set it in a dispatch_once() block. This is a common pattern in Objective-C code.

@@ -15,6 +16,7 @@ class RasterTileTest {
public:
FakeFileSource fileSource;
TransformState transformState;
util::RunLoop loop;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests were missing an explicit RunLoop. If running these tests separately, this is not an issue because we had the default RunLoop, but when running after other unit tests that create/destroy a RunLoop, we got a null RunLoop further down the line.

@jfirebaugh jfirebaugh merged commit 388fa9c into master Oct 6, 2016
@jfirebaugh jfirebaugh deleted the 6613-initialize-runloop-in-sdk branch October 6, 2016 17:16
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants