@@ -320,15 +320,21 @@ - (UIImage *)newImageForEntityUUID:(NSString *)entityUUID sourceImageUUID:(NSStr
320320 [self saveMetadata ];
321321 } else {
322322 [self _entryWasAccessedWithEntityUUID: entityUUID];
323-
323+
324324 // Create CGImageRef whose backing store *is* the mapped image table entry. We avoid a memcpy this way.
325325 CGDataProviderRef dataProvider = CGDataProviderCreateWithData ((__bridge_retained void *)entryData, [entryData bytes ], [entryData imageLength ], _FICReleaseImageData);
326-
326+
327+ [_inUseEntries addObject: entityUUID];
328+ __weak FICImageTable *weakSelf = self;
329+ [entryData executeBlockOnDealloc: ^{
330+ [weakSelf removeInUseForEntityUUID: entityUUID];
331+ }];
332+
327333 CGSize pixelSize = [_imageFormat pixelSize ];
328334 CGBitmapInfo bitmapInfo = [_imageFormat bitmapInfo ];
329335 NSInteger bitsPerComponent = [_imageFormat bitsPerComponent ];
330336 NSInteger bitsPerPixel = [_imageFormat bytesPerPixel ] * 8 ;
331- CGColorSpaceRef colorSpace = [_imageFormat isGrayscale ] ? CGColorSpaceCreateDeviceGray () : CGColorSpaceCreateDeviceRGB ();
337+ CGColorSpaceRef colorSpace = [_imageFormat isGrayscale ] ? CGColorSpaceCreateDeviceGray () : CGColorSpaceCreateDeviceRGB ();
332338
333339 CGImageRef imageRef = CGImageCreate (pixelSize.width , pixelSize.height , bitsPerComponent, bitsPerPixel, _imageRowLength, colorSpace, bitmapInfo, dataProvider, NULL , false , (CGColorRenderingIntent)0 );
334340 CGDataProviderRelease (dataProvider);
@@ -354,6 +360,12 @@ static void _FICReleaseImageData(void *info, const void *data, size_t size) {
354360 CFRelease (info);
355361}
356362
363+ - (void )removeInUseForEntityUUID : (NSString *)entityUUID {
364+ [_lock lock ];
365+ [_inUseEntries removeObject: entityUUID];
366+ [_lock unlock ];
367+ }
368+
357369- (void )deleteEntryForEntityUUID : (NSString *)entityUUID {
358370 if (entityUUID != nil ) {
359371 [_lock lock ];
@@ -440,7 +452,7 @@ - (FICImageTableEntry *)_entryDataAtIndex:(NSInteger)index {
440452 off_t entryOffsetInChunk = entryOffset - chunkOffset;
441453 void *mappedChunkAddress = [chunk bytes ];
442454 void *mappedEntryAddress = mappedChunkAddress + entryOffsetInChunk;
443- entryData = [[FICImageTableEntry alloc ] initWithImageTableChunk: chunk bytes: mappedEntryAddress length: _entryLength];
455+ entryData = [[FICImageTableEntry alloc ] initWithImageTableChunk: chunk bytes: mappedEntryAddress length: _entryLength ];
444456 }
445457 }
446458
@@ -464,15 +476,15 @@ - (NSInteger)_nextEntryIndex {
464476 NSString *oldestEvictableEntityUUID = [self oldestEvictableEntityUUID ];
465477 if (oldestEvictableEntityUUID) {
466478 [self deleteEntryForEntityUUID: oldestEvictableEntityUUID];
467- index = [self _nextEntryIndex ];
468- }
479+ index = [self _nextEntryIndex ];
480+ }
469481 }
470482
471483 if (index >= [self _maximumCount ]) {
472484 NSString *message = [NSString stringWithFormat: @" FICImageTable - unable to evict entry from table '%@ ' to make room. New index %d , desired max %d " , [_imageFormat name ], index, [self _maximumCount ]];
473485 [[FICImageCache sharedImageCache ] _logMessage: message];
474486 }
475-
487+
476488 return index;
477489}
478490
0 commit comments