Skip to content

Commit

Permalink
Merge pull request #133 from Sega-Zero/fix-memory-leak
Browse files Browse the repository at this point in the history
Fix high memory usage in some rare cases
  • Loading branch information
mallorypaine committed Apr 15, 2016
2 parents 40442f5 + e086ab3 commit 417084e
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 44 deletions.
26 changes: 14 additions & 12 deletions FastImageCache/FastImageCache/FastImageCache/FICImageCache.m
Original file line number Diff line number Diff line change
Expand Up @@ -214,18 +214,20 @@ - (BOOL)_retrieveImageForEntity:(id <FICEntity>)entity withFormatName:(NSString
}

if (needsToFetch) {
UIImage *image;
if ([entity respondsToSelector:@selector(imageForFormat:)]){
FICImageFormat *format = [self formatWithName:formatName];
image = [entity imageForFormat:format];
}

if (image){
[self _imageDidLoad:image forURL:sourceImageURL];
} else if (_delegateImplementsWantsSourceImageForEntityWithFormatNameCompletionBlock){
[_delegate imageCache:self wantsSourceImageForEntity:entity withFormatName:formatName completionBlock:^(UIImage *sourceImage) {
[self _imageDidLoad:sourceImage forURL:sourceImageURL];
}];
@autoreleasepool {
UIImage *image;
if ([entity respondsToSelector:@selector(imageForFormat:)]){
FICImageFormat *format = [self formatWithName:formatName];
image = [entity imageForFormat:format];
}

if (image){
[self _imageDidLoad:image forURL:sourceImageURL];
} else if (_delegateImplementsWantsSourceImageForEntityWithFormatNameCompletionBlock){
[_delegate imageCache:self wantsSourceImageForEntity:entity withFormatName:formatName completionBlock:^(UIImage *sourceImage) {
[self _imageDidLoad:sourceImage forURL:sourceImageURL];
}];
}
}
}
} else {
Expand Down
68 changes: 36 additions & 32 deletions FastImageCache/FastImageCache/FastImageCache/FICImageTable.m
Original file line number Diff line number Diff line change
Expand Up @@ -685,43 +685,47 @@ - (NSNumber *)_numberForEntryAtIndex:(NSInteger)index {
#pragma mark - Working with Metadata

- (void)saveMetadata {
[_lock lock];

NSDictionary *metadataDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
[_indexMap copy], FICImageTableIndexMapKey,
[_sourceImageMap copy], FICImageTableContextMapKey,
[[_MRUEntries array] copy], FICImageTableMRUArrayKey,
[_imageFormatDictionary copy], FICImageTableFormatKey, nil];
@autoreleasepool {
[_lock lock];

NSDictionary *metadataDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
[_indexMap copy], FICImageTableIndexMapKey,
[_sourceImageMap copy], FICImageTableContextMapKey,
[[_MRUEntries array] copy], FICImageTableMRUArrayKey,
[_imageFormatDictionary copy], FICImageTableFormatKey, nil];

__block int32_t metadataVersion = OSAtomicIncrement32(&_metadataVersion);
__block int32_t metadataVersion = OSAtomicIncrement32(&_metadataVersion);

[_lock unlock];

static dispatch_queue_t __metadataQueue = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
__metadataQueue = dispatch_queue_create("com.path.FastImageCache.ImageTableMetadataQueue", NULL);
});

dispatch_async(__metadataQueue, ^{
// Cancel serialization if a new metadata version is queued to be saved
if (metadataVersion != _metadataVersion) {
return;
}
[_lock unlock];
static dispatch_queue_t __metadataQueue = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
__metadataQueue = dispatch_queue_create("com.path.FastImageCache.ImageTableMetadataQueue", NULL);
});
dispatch_async(__metadataQueue, ^{
// Cancel serialization if a new metadata version is queued to be saved
if (metadataVersion != _metadataVersion) {
return;
}

NSData *data = [NSJSONSerialization dataWithJSONObject:metadataDictionary options:kNilOptions error:NULL];
@autoreleasepool {
NSData *data = [NSJSONSerialization dataWithJSONObject:metadataDictionary options:kNilOptions error:NULL];

// Cancel disk writing if a new metadata version is queued to be saved
if (metadataVersion != _metadataVersion) {
return;
}
// Cancel disk writing if a new metadata version is queued to be saved
if (metadataVersion != _metadataVersion) {
return;
}

BOOL fileWriteResult = [data writeToFile:[self metadataFilePath] atomically:NO];
if (fileWriteResult == NO) {
NSString *message = [NSString stringWithFormat:@"*** FIC Error: %s couldn't write metadata for format %@", __PRETTY_FUNCTION__, [_imageFormat name]];
[self.imageCache _logMessage:message];
}
});
BOOL fileWriteResult = [data writeToFile:[self metadataFilePath] atomically:NO];
if (fileWriteResult == NO) {
NSString *message = [NSString stringWithFormat:@"*** FIC Error: %s couldn't write metadata for format %@", __PRETTY_FUNCTION__, [_imageFormat name]];
[self.imageCache _logMessage:message];
}
}
});
}
}

- (void)_loadMetadata {
Expand Down

0 comments on commit 417084e

Please sign in to comment.