This repository has been archived by the owner on Aug 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Use a pair of NSSets to manage the retain/release lifecycle of layers. #11343
Closed
julianrex
wants to merge
6
commits into
gl-layer-memory-fix
from
jrex-custom-layer-style-changed-leak
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
22a1da5
[ios] Adding a few more test cases. testOpenGLLayerDoesNotLeakWhenSty…
0327167
[ios] Added sets to manage retain/release of layers (esp custom gl la…
6c9239c
[macos] Added retain/release calls to macos's MGLMapView cf. iOS.
ad323de
Merge branch 'gl-layer-memory-fix' into jrex-custom-layer-style-chang…
julianrex 6fb2a94
[ios, macos, core] Added retain/release manager that holds on to the …
bcdf230
[macos] Updated MGLMapView with new layer manager.
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
#import "MGLStyle.h" | ||
#import "MGLStyleLayer.h" | ||
#import "MGLStyleLayerRetentionManager_Private.h" | ||
#include <mbgl/style/style.hpp> | ||
#include <mbgl/style/layers/custom_layer.hpp> | ||
|
||
static const NSUInteger MGLStyleLayerRetentionManagerCapacityHint = 100; | ||
|
||
@interface MGLStyleLayerRetentionManager () | ||
@property (nonatomic) NSMapTable<MGLStyleLayer*, NSNumber*> *retentionTable; | ||
@end | ||
|
||
@implementation MGLStyleLayerRetentionManager | ||
|
||
- (instancetype)init { | ||
if ((self = [super init])) { | ||
_retentionTable = [[NSMapTable alloc] initWithKeyOptions:NSPointerFunctionsStrongMemory|NSPointerFunctionsObjectPointerPersonality | ||
valueOptions:NSPointerFunctionsStrongMemory | ||
capacity:MGLStyleLayerRetentionManagerCapacityHint]; | ||
} | ||
return self; | ||
} | ||
|
||
- (BOOL)isManagedLayer:(nullable MGLStyleLayer*)styleLayer { | ||
return [self.retentionTable objectForKey:styleLayer] != nil; | ||
} | ||
|
||
- (void)updateRetainedLayers:(nonnull NSSet<MGLStyleLayer*>*)sourceObjects { | ||
|
||
// Add/update the objects from the source set, with a default lifetime | ||
for (id layer in sourceObjects) { | ||
[self.retentionTable setObject:@(MGLStyleLayerRetentionManagerDefaultLifetime) forKey:layer]; | ||
} | ||
} | ||
|
||
- (void)decrementLifetimes { | ||
// Consider double-buffering the two tables, so we don't keep allocing/deallocing tables. | ||
NSMapTable *retentionTable = [[NSMapTable alloc] initWithKeyOptions:NSPointerFunctionsStrongMemory|NSPointerFunctionsObjectPointerPersonality | ||
valueOptions:NSPointerFunctionsStrongMemory | ||
capacity:MGLStyleLayerRetentionManagerCapacityHint]; | ||
|
||
for (MGLStyleLayer *layer in self.retentionTable) { | ||
NSInteger lifeTime = [[self.retentionTable objectForKey:layer] integerValue]; | ||
|
||
if (lifeTime > 0) { | ||
[retentionTable setObject:@(lifeTime - 1) forKey:layer]; | ||
} | ||
} | ||
|
||
self.retentionTable = retentionTable; | ||
} | ||
|
||
@end |
16 changes: 16 additions & 0 deletions
16
platform/darwin/src/MGLStyleLayerRetentionManager_Private.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
@class MGLStyle; | ||
|
||
static const NSInteger MGLStyleLayerRetentionManagerDefaultLifetime = 2; | ||
|
||
/** | ||
Object use to manage the retain/release of `MGLStyleLayer`s (currently only `MGLOpenGLStyleLayer`. | ||
Managed layers are given a "lifetime", and this is reset everytime `-updateRetainedLayers:` is called. | ||
The lifetime is decremented with each call to `-decrementLifetimes`, when it reaches 0 the layer | ||
is removed from the manager (potentially releasing it) | ||
*/ | ||
@interface MGLStyleLayerRetentionManager : NSObject | ||
- (BOOL)isManagedLayer:(nullable MGLStyleLayer*)styleLayer; | ||
- (void)updateRetainedLayers:(nonnull NSSet<MGLStyleLayer*>*)sourceObjects; | ||
- (void)decrementLifetimes; | ||
@end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This needs to be invoked from
[MGLStyle layerFromMBGLLayer]
as well, to capture internally created style layers.