Fix objc template (#6725)

- A static set needs a static lock or we get multiple locks if the class is inherited
- If this fix is not included and SWGObject is accessed from a second thread then we get EXC_BAD_ACCESS error (crash)
This commit is contained in:
Michał Cichoń 2020-06-21 13:12:46 +02:00 committed by GitHub
parent 4bb5afdefb
commit 195b376f48
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 9 additions and 3 deletions

View File

@ -8,13 +8,15 @@
*/ */
- (instancetype)initWithDictionary:(NSDictionary *)dict error:(NSError **)err { - (instancetype)initWithDictionary:(NSDictionary *)dict error:(NSError **)err {
static NSMutableSet *classNames; static NSMutableSet *classNames;
static NSObject *lock;
static dispatch_once_t onceToken; static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{ dispatch_once(&onceToken, ^{
classNames = [NSMutableSet new]; classNames = [NSMutableSet new];
lock = [NSObject new];
}); });
BOOL initSync; BOOL initSync;
@synchronized([self class]) @synchronized(lock)
{ {
NSString *className = NSStringFromClass([self class]); NSString *className = NSStringFromClass([self class]);
initSync = ![classNames containsObject:className]; initSync = ![classNames containsObject:className];

View File

@ -8,13 +8,15 @@
*/ */
- (instancetype)initWithDictionary:(NSDictionary *)dict error:(NSError **)err { - (instancetype)initWithDictionary:(NSDictionary *)dict error:(NSError **)err {
static NSMutableSet *classNames; static NSMutableSet *classNames;
static NSObject *lock;
static dispatch_once_t onceToken; static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{ dispatch_once(&onceToken, ^{
classNames = [NSMutableSet new]; classNames = [NSMutableSet new];
lock = [NSObject new];
}); });
BOOL initSync; BOOL initSync;
@synchronized([self class]) @synchronized(lock)
{ {
NSString *className = NSStringFromClass([self class]); NSString *className = NSStringFromClass([self class]);
initSync = ![classNames containsObject:className]; initSync = ![classNames containsObject:className];

View File

@ -8,13 +8,15 @@
*/ */
- (instancetype)initWithDictionary:(NSDictionary *)dict error:(NSError **)err { - (instancetype)initWithDictionary:(NSDictionary *)dict error:(NSError **)err {
static NSMutableSet *classNames; static NSMutableSet *classNames;
static NSObject *lock;
static dispatch_once_t onceToken; static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{ dispatch_once(&onceToken, ^{
classNames = [NSMutableSet new]; classNames = [NSMutableSet new];
lock = [NSObject new];
}); });
BOOL initSync; BOOL initSync;
@synchronized([self class]) @synchronized(lock)
{ {
NSString *className = NSStringFromClass([self class]); NSString *className = NSStringFromClass([self class]);
initSync = ![classNames containsObject:className]; initSync = ![classNames containsObject:className];