Skip to content

Commit 417084e

Browse files
committed
Merge pull request #133 from Sega-Zero/fix-memory-leak
Fix high memory usage in some rare cases
2 parents 40442f5 + e086ab3 commit 417084e

2 files changed

Lines changed: 50 additions & 44 deletions

File tree

FastImageCache/FastImageCache/FastImageCache/FICImageCache.m

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -214,18 +214,20 @@ - (BOOL)_retrieveImageForEntity:(id <FICEntity>)entity withFormatName:(NSString
214214
}
215215

216216
if (needsToFetch) {
217-
UIImage *image;
218-
if ([entity respondsToSelector:@selector(imageForFormat:)]){
219-
FICImageFormat *format = [self formatWithName:formatName];
220-
image = [entity imageForFormat:format];
221-
}
222-
223-
if (image){
224-
[self _imageDidLoad:image forURL:sourceImageURL];
225-
} else if (_delegateImplementsWantsSourceImageForEntityWithFormatNameCompletionBlock){
226-
[_delegate imageCache:self wantsSourceImageForEntity:entity withFormatName:formatName completionBlock:^(UIImage *sourceImage) {
227-
[self _imageDidLoad:sourceImage forURL:sourceImageURL];
228-
}];
217+
@autoreleasepool {
218+
UIImage *image;
219+
if ([entity respondsToSelector:@selector(imageForFormat:)]){
220+
FICImageFormat *format = [self formatWithName:formatName];
221+
image = [entity imageForFormat:format];
222+
}
223+
224+
if (image){
225+
[self _imageDidLoad:image forURL:sourceImageURL];
226+
} else if (_delegateImplementsWantsSourceImageForEntityWithFormatNameCompletionBlock){
227+
[_delegate imageCache:self wantsSourceImageForEntity:entity withFormatName:formatName completionBlock:^(UIImage *sourceImage) {
228+
[self _imageDidLoad:sourceImage forURL:sourceImageURL];
229+
}];
230+
}
229231
}
230232
}
231233
} else {

FastImageCache/FastImageCache/FastImageCache/FICImageTable.m

Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -685,43 +685,47 @@ - (NSNumber *)_numberForEntryAtIndex:(NSInteger)index {
685685
#pragma mark - Working with Metadata
686686

687687
- (void)saveMetadata {
688-
[_lock lock];
689-
690-
NSDictionary *metadataDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
691-
[_indexMap copy], FICImageTableIndexMapKey,
692-
[_sourceImageMap copy], FICImageTableContextMapKey,
693-
[[_MRUEntries array] copy], FICImageTableMRUArrayKey,
694-
[_imageFormatDictionary copy], FICImageTableFormatKey, nil];
688+
@autoreleasepool {
689+
[_lock lock];
690+
691+
NSDictionary *metadataDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
692+
[_indexMap copy], FICImageTableIndexMapKey,
693+
[_sourceImageMap copy], FICImageTableContextMapKey,
694+
[[_MRUEntries array] copy], FICImageTableMRUArrayKey,
695+
[_imageFormatDictionary copy], FICImageTableFormatKey, nil];
695696

696-
__block int32_t metadataVersion = OSAtomicIncrement32(&_metadataVersion);
697+
__block int32_t metadataVersion = OSAtomicIncrement32(&_metadataVersion);
697698

698-
[_lock unlock];
699-
700-
static dispatch_queue_t __metadataQueue = nil;
701-
static dispatch_once_t onceToken;
702-
dispatch_once(&onceToken, ^{
703-
__metadataQueue = dispatch_queue_create("com.path.FastImageCache.ImageTableMetadataQueue", NULL);
704-
});
705-
706-
dispatch_async(__metadataQueue, ^{
707-
// Cancel serialization if a new metadata version is queued to be saved
708-
if (metadataVersion != _metadataVersion) {
709-
return;
710-
}
699+
[_lock unlock];
700+
701+
static dispatch_queue_t __metadataQueue = nil;
702+
static dispatch_once_t onceToken;
703+
dispatch_once(&onceToken, ^{
704+
__metadataQueue = dispatch_queue_create("com.path.FastImageCache.ImageTableMetadataQueue", NULL);
705+
});
706+
707+
dispatch_async(__metadataQueue, ^{
708+
// Cancel serialization if a new metadata version is queued to be saved
709+
if (metadataVersion != _metadataVersion) {
710+
return;
711+
}
711712

712-
NSData *data = [NSJSONSerialization dataWithJSONObject:metadataDictionary options:kNilOptions error:NULL];
713+
@autoreleasepool {
714+
NSData *data = [NSJSONSerialization dataWithJSONObject:metadataDictionary options:kNilOptions error:NULL];
713715

714-
// Cancel disk writing if a new metadata version is queued to be saved
715-
if (metadataVersion != _metadataVersion) {
716-
return;
717-
}
716+
// Cancel disk writing if a new metadata version is queued to be saved
717+
if (metadataVersion != _metadataVersion) {
718+
return;
719+
}
718720

719-
BOOL fileWriteResult = [data writeToFile:[self metadataFilePath] atomically:NO];
720-
if (fileWriteResult == NO) {
721-
NSString *message = [NSString stringWithFormat:@"*** FIC Error: %s couldn't write metadata for format %@", __PRETTY_FUNCTION__, [_imageFormat name]];
722-
[self.imageCache _logMessage:message];
723-
}
724-
});
721+
BOOL fileWriteResult = [data writeToFile:[self metadataFilePath] atomically:NO];
722+
if (fileWriteResult == NO) {
723+
NSString *message = [NSString stringWithFormat:@"*** FIC Error: %s couldn't write metadata for format %@", __PRETTY_FUNCTION__, [_imageFormat name]];
724+
[self.imageCache _logMessage:message];
725+
}
726+
}
727+
});
728+
}
725729
}
726730

727731
- (void)_loadMetadata {

0 commit comments

Comments
 (0)