You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Aug 8, 2023. It is now read-only.
Repository navigation
This repository was archived by the owner on Aug 8, 2023. It is now read-only.
MGLSource should not maintain parallel state #7375
MGLSource and its subclasses, particularly MGLShapeSource, currently hang onto objects passed into their initializers in order to return them in their properties’ getters. This is a workaround for #6584, but it’s problematic because MGLSource objects don’t necessarily know the canonical value of these properties. For example, if you get a source via -[MGLStyle sourceWithIdentifier:], the properties are all unset because the source was initialized via -[MGLSource initWithRawSource:]. You can then proceed to modify the source’s contents behind the backs of any other MGLShapeSource instances that wrap that source:
letone=MGLShapeSource(identifier:"one",shape:MGLPointFeature(),options:nil)mapView.style.addSource(one)
// a short while later…
letuno=mapView.style.source(withIdentifier:"one")uno.shape=someOtherFeature
// finally…
assert(one.shape is MGLPointFeature)
// 💣💥
This is essentially blocked by #7376, because otherwise we can continue to expose an -[MGLShapeSource setShape:] but we’d have to eliminate -[MGLShapeSource shape]. A write-only property is theoretically possible but unheard of in Objective-C and Swift. (The closest thing I can think of is -[NSView setNeedsDisplay:], which isn’t a property and which no one thinks is a good design.)
#7377 will eliminate MGLShapeSource.geoJSONData and make MGLShapeSource.URL’s getter computed. It will also eliminate all the parallel state in MGLRasterSource and MGLVectorSource. However, MGLShapeSource.shape remains.
I updated the example in the original description above:
s/let uno = mapView.style.layer(withIdentifier: "one")/let uno = mapView.style.source(withIdentifier: "one")
This gets into #7376 and the memory concerns noted there but would it make sense for MGLStyle to keep references to the source and layer objects in use? Then, asking the style for a source would/could always return the same source instance.
It's definitely possible and it'd solve several problems associated with these wrappers. We already do that for MGLOpenGLStyleLayers, which are entirely managed by the style. I wanted to avoid doing that for sources and layers in general, since there may be ways for the style to change behind MGLStyle's back, whereas a custom layer can only come from application code.
I'm open to adding a void* peer member to mbgl::Style::Source, mbgl::Style::Layer, etc., as a place where SDKs could stash a pointer to the SDK-level object, so that they can ensure that methods like getLayer() always returns the same object for a given identifier. Would that help?
The more I think about this, the more attractive @boundsj’s idea in #7375 (comment) looks. The only catch is that we have to be absolutely certain MGLStyle knows exactly when a source or layer goes away, such as due to polyline/polygon annotation removals.
A void* peer member on Source and Layer wouldn’t be a complete solution, because ARC doesn’t track ownership in void* pointers – the pointer would immediately become stale without some other Objective-C object hanging onto the object. However, a general-purpose void* context member would probably be useful for associating (non-object) data with any mbgl object that the SDK might need to wrap.
Type erasure via any would also potentially eliminate the edge cases caused by application code and mbgl effectively maintaining separate registries of sources (or layers).
A void* peer member on Source and Layer wouldn’t be a complete solution, because ARC doesn’t track ownership in void* pointers – the pointer would immediately become stale without some other Objective-C object hanging onto the object.
This might not be a problem anymore if ARC can track ownership of Objective-C objects within C/C++ structs. The only unknown, then, is whether that also extends to void * or any pointers.
Activity
1ec5 commentedon Dec 11, 2016
This is essentially blocked by #7376, because otherwise we can continue to expose an
-[MGLShapeSource setShape:]but we’d have to eliminate-[MGLShapeSource shape]. A write-only property is theoretically possible but unheard of in Objective-C and Swift. (The closest thing I can think of is-[NSView setNeedsDisplay:], which isn’t a property and which no one thinks is a good design.)1ec5 commentedon Dec 12, 2016
#7377 will eliminate
MGLShapeSource.geoJSONDataand makeMGLShapeSource.URL’s getter computed. It will also eliminate all the parallel state in MGLRasterSource and MGLVectorSource. However,MGLShapeSource.shaperemains.boundsj commentedon Dec 12, 2016
I updated the example in the original description above:
s/
let uno = mapView.style.layer(withIdentifier: "one")/let uno = mapView.style.source(withIdentifier: "one")boundsj commentedon Dec 12, 2016
This gets into #7376 and the memory concerns noted there but would it make sense for
MGLStyleto keep references to the source and layer objects in use? Then, asking the style for a source would/could always return the same source instance.1ec5 commentedon Dec 13, 2016
It's definitely possible and it'd solve several problems associated with these wrappers. We already do that for MGLOpenGLStyleLayers, which are entirely managed by the style. I wanted to avoid doing that for sources and layers in general, since there may be ways for the style to change behind MGLStyle's back, whereas a custom layer can only come from application code.
jfirebaugh commentedon Dec 14, 2016
I'm open to adding a
void* peermember tombgl::Style::Source,mbgl::Style::Layer, etc., as a place where SDKs could stash a pointer to the SDK-level object, so that they can ensure that methods likegetLayer()always returns the same object for a given identifier. Would that help?1ec5 commentedon Dec 14, 2016
The more I think about this, the more attractive @boundsj’s idea in #7375 (comment) looks. The only catch is that we have to be absolutely certain MGLStyle knows exactly when a source or layer goes away, such as due to polyline/polygon annotation removals.
A
void* peermember onSourceandLayerwouldn’t be a complete solution, because ARC doesn’t track ownership invoid*pointers – the pointer would immediately become stale without some other Objective-C object hanging onto the object. However, a general-purposevoid* contextmember would probably be useful for associating (non-object) data with any mbgl object that the SDK might need to wrap.jfirebaugh commentedon Dec 14, 2016
What does ARC do if you use a type-erasure-based
anytype instead ofvoid*? http://www.artima.com/cppsource/type_erasure2.html1ec5 commentedon Dec 2, 2018
Type erasure via
anywould also potentially eliminate the edge cases caused by application code and mbgl effectively maintaining separate registries of sources (or layers)./ref #13492
stale commentedon May 31, 2019
This issue has been automatically detected as stale because it has not had recent activity and will be archived. Thank you for your contributions.
1ec5 commentedon Aug 6, 2019
Still the biggest pain point with runtime styling in Objective-C and Swift.
1ec5 commentedon Aug 6, 2019
This might not be a problem anymore if ARC can track ownership of Objective-C objects within C/C++ structs. The only unknown, then, is whether that also extends to
void *oranypointers.stale commentedon May 22, 2020
This issue has been automatically detected as stale because it has not had recent activity and will be archived. Thank you for your contributions.