-
Notifications
You must be signed in to change notification settings - Fork 1.3k
[ios] Address bad access exception in MGLAttributionInfo
#13300
Conversation
error:NULL]; | ||
__block NSMutableAttributedString *attributedString; | ||
|
||
dispatch_sync(dispatch_get_main_queue() , ^{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note that this method is called as part of -[MGLTileSource attributionInfos]
, which is a public method that can be called at any time. Even internal uses of this method tend to occur in a for loop, since there are multiple sources to inspect at the same time.
If performance does turn out to be a problem, an alternative to blocking on the main thread here would be to do so further up the call stack, wherever it’s expected that the caller could be on a thread other than the main thread:
NSArray<MGLAttributionInfo *>* attributionInfo = [MGLMapSnapshotter generateAttributionInfos:attributions]; |
This would require documenting other callers, such as -[MGLTileSource attributionInfos]
, as needing to run on the main thread. There is precedent for such a requirement.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated to check whether it's being called on the main queue. Would it be better to call the NSMutableAttributedString
initializer earlier than to fiddle with queues? @julianrex and I found that the exception did not occur when we created an NSMutableAttributedString
in the initializer for a map view.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@julianrex and I found that the exception did not occur when we created an NSMutableAttributedString in the initializer for a map view.
We can't rely on that behavior as it's not documented.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry if I'm just parading my iOS ignorance here, but isn't MGLMapView
essentially required to be created and used on the main thread? And if that's the case then creating the attributed string in the initializer should be good, right?
If we can find a way to avoid "fiddling with queues" we should. It's not the queues themselves that are the problem -- but hidden/unexpected cross-thread blocking calls are trouble.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ChrisLoer @jmkiley was referring to the fact that we can avoid the crash if we initialize any old NSMutableAttributedString
on the main thread (it ends up calling WebKitInitialize
using a dispatch_once
- I suspect it's that initializer that needs to be on the main thread) - not the specific one.
This PR currently checks whether the |
Addressed a memory leak that popped up during profiling. Thank you @fabian-guerra for walking through the Instruments trace with me. |
dispatch_sync(dispatch_get_main_queue(), initialization); | ||
} else { | ||
initialization(); | ||
} | ||
#else | ||
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithHTML:htmlData |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does initWithHTML...
have the same concern?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nothing in the documentation: https://developer.apple.com/documentation/foundation/nsattributedstring/1535412-initwithhtml
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's keep an eye on the performance changes; we may need to either move the generation of these strings earlier, consider caching - or both.
Please add a ticket for a performance test for snapshotters. 🙇
* [ios] move creation of attributed string to global queue] * [ios] check if on main queue
Continues discussion from #13298 in order to add the fix to
master
.NSMutableAttributionString
This PR addresses a bad access exception with
MGLMapSnapshotter
on iOS 8. Per Apple docs:cc @julianrex @ChrisLoer