Skip to content

Instantly share code, notes, and snippets.

@soxjke
Last active March 27, 2017 15:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save soxjke/1275e069f938f12fd666d67ec397f736 to your computer and use it in GitHub Desktop.
Save soxjke/1275e069f938f12fd666d67ec397f736 to your computer and use it in GitHub Desktop.
RaceCondition2
Thread 5Queue : com.apple.root.user-initiated-qos (concurrent)
#0 0x000000010f181d2b in objc_release ()
#1 0x000000010f1831d1 in (anonymous namespace)::AutoreleasePoolPage::pop(void*) ()
#2 0x00000001125037ce in _dispatch_root_queue_drain ()
#3 0x0000000112503059 in _dispatch_worker_thread3 ()
#4 0x00000001128cb4de in _pthread_wqthread ()
#5 0x00000001128c9341 in start_wqthread ()
#import <Foundation/Foundation.h>
@interface StringContainer : NSObject
@property (nonatomic, strong) NSString *string;
- (instancetype)initWithString:(NSString *)string;
@end
#import "StringContainer.h"
@implementation StringContainer
- (instancetype)initWithString:(NSString *)string {
self = [super init];
if (self) {
self.string = string;
}
return self;
}
@end
#import <Foundation/Foundation.h>
@class StringContainer;
@interface Test : NSObject
@property (nonatomic, strong) StringContainer *stringProp;
@end
#import "Test.h"
#import "StringContainer.h"
@interface Test() {
StringContainer *_stringProp;
}
@end
@implementation Test
- (void)setStringProp:(StringContainer *)stringProp {
[_stringProp autorelease];
_stringProp = [stringProp retain];
}
- (StringContainer *)stringProp {
return _stringProp;
}
@end
@interface ViewController ()
@property (nonatomic, strong) Test *test;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self testCrash];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)testCrash {
self.test = [Test new];
for(int i = 0; i < 1000; i++) {
dispatch_async(dispatch_get_global_queue(0, 0), ^() {
self.test.stringProp = [[StringContainer alloc] initWithString:[[NSString alloc] initWithFormat:@"%d", i]];
});
dispatch_async(dispatch_get_global_queue(0, 0), ^() {
[self print:self.test.stringProp];
});
}
}
- (void)print:(StringContainer * __unsafe_unretained)string {
NSLog(@"%@", string.string);
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment