Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Memleaks fixes
  • Loading branch information
djphoenix committed May 14, 2015
1 parent 85e7248 commit e2d3abc
Showing 1 changed file with 23 additions and 17 deletions.
40 changes: 23 additions & 17 deletions pxSVG/pxSVGLayer.m
Expand Up @@ -55,13 +55,15 @@ - (void)dealloc
{
if (self.loadOperation) [self.loadOperation cancel];
if (self.parseOperation) [self.parseOperation cancel];
if (self.layerOperation) [self.layerOperation cancel];
self.layerOperation = self.parseOperation = self.loadOperation = nil;
}

- (void)loadURL:(NSURL *)url
{
[self clean];
__weak pxSVGLayer *weakself = self;
__block NSBlockOperation *op = [NSBlockOperation blockOperationWithBlock:^{
__block NSBlockOperation *op = [NSBlockOperation blockOperationWithBlock:^{ @autoreleasepool {
__block NSURLResponse *resp;
__block NSError *err;
NSData *data;
Expand All @@ -71,7 +73,8 @@ - (void)loadURL:(NSURL *)url
return;
}
__block NSString *str = data?[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]:nil;
__block NSBlockOperation *sync = [NSBlockOperation blockOperationWithBlock:^{
data = nil;
__block NSBlockOperation *sync = [NSBlockOperation blockOperationWithBlock:^{ @autoreleasepool {
sync = nil;
if ([op isCancelled] || !weakself) {
op = nil, resp = nil, err = nil, str = nil;
Expand All @@ -90,9 +93,9 @@ - (void)loadURL:(NSURL *)url
}
[weakself loadString:str];
resp = nil, err = nil, str = nil;
}];
} }];
[[NSOperationQueue mainQueue] addOperations:@[sync] waitUntilFinished:YES];
}];
} }];
op.name = url.absoluteString;
op.threadPriority = 0.1f;
[[self.class loadQueue] addOperation:self.loadOperation=op];
Expand All @@ -107,13 +110,13 @@ - (void)loadString:(NSString *)string
{
[self clean];
__weak pxSVGLayer *weakself = self;
__block NSBlockOperation *op = [NSBlockOperation blockOperationWithBlock:^{
__block NSBlockOperation *op = [NSBlockOperation blockOperationWithBlock:^{ @autoreleasepool {
__block pxSVGImage *img = [pxSVGImage svgImageWithXML:string];
if ([op isCancelled]) {
op = nil, img = nil;
return;
}
__block NSBlockOperation *sync = [NSBlockOperation blockOperationWithBlock:^{
__block NSBlockOperation *sync = [NSBlockOperation blockOperationWithBlock:^{ @autoreleasepool {
sync = nil;
if ([op isCancelled] || !weakself) {
op = nil, img = nil;
Expand All @@ -126,9 +129,9 @@ - (void)loadString:(NSString *)string
}
[weakself loadImage:img];
img = nil;
}];
} }];
[[NSOperationQueue mainQueue] addOperations:@[sync] waitUntilFinished:YES];
}];
} }];
op.threadPriority = 0.1f;
[[self.class parseQueue] addOperation:self.parseOperation=op];
}
Expand All @@ -138,13 +141,13 @@ - (void)loadImage:(pxSVGImage*)image
[self clean];
CGRect bounds = image.bounds;
__weak pxSVGLayer *weakself = self;
__block NSBlockOperation *op = [NSBlockOperation blockOperationWithBlock:^{
__block NSBlockOperation *op = [NSBlockOperation blockOperationWithBlock:^{ @autoreleasepool {
__block CALayer *img = [image makeLayer];
if ([op isCancelled]) {
op = nil, img = nil;
return;
}
__block NSBlockOperation *sync = [NSBlockOperation blockOperationWithBlock:^{
__block NSBlockOperation *sync = [NSBlockOperation blockOperationWithBlock:^{ @autoreleasepool {
sync = nil;
if ([op isCancelled] || !weakself) {
img = nil, op = nil;
Expand All @@ -157,26 +160,29 @@ - (void)loadImage:(pxSVGImage*)image
weakself.contentRect = bounds;
if ([weakself.svgDelegate respondsToSelector:@selector(svgLayerDidLoadImage:)])
[weakself.svgDelegate svgLayerDidLoadImage:weakself];
}];
} }];
[[NSOperationQueue mainQueue] addOperations:@[sync] waitUntilFinished:YES];
}];
} }];
op.threadPriority = 0.2f;
[[self.class layererQueue] addOperation:self.layerOperation=op];
}

- (void)loadError:(NSError *)error
{
[self clean];
if ([self.svgDelegate respondsToSelector:@selector(svgLayer:didFailedLoad:)])
[self.svgDelegate svgLayer:self didFailedLoad:error];
}

- (void)clean
{
if (self.loadOperation) [self.loadOperation cancel];
if (self.parseOperation) [self.parseOperation cancel];
if (self.layerOperation) [self.layerOperation cancel];
while (self.sublayers.count) [self.sublayers.firstObject removeFromSuperlayer];
self.contentRect = CGRectZero;
@autoreleasepool {
if (self.loadOperation) [self.loadOperation cancel];
if (self.parseOperation) [self.parseOperation cancel];
if (self.layerOperation) [self.layerOperation cancel];
while (self.sublayers.count) [self.sublayers.firstObject removeFromSuperlayer];
self.contentRect = CGRectZero;
}
}

@end

0 comments on commit e2d3abc

Please sign in to comment.