Skip to content

Instantly share code, notes, and snippets.

@ccgus
Last active December 6, 2019 18:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ccgus/b810c80f0c246d7587cb141bd3f8c216 to your computer and use it in GitHub Desktop.
Save ccgus/b810c80f0c246d7587cb141bd3f8c216 to your computer and use it in GitHub Desktop.
Asking the internet for help to make sure I'm not being stupid.
/*
This sample shows how CGImageSourceCreateThumbnailAtIndex leaks something akin
to the memory behind a CGImageRef when asked to create a thumbnail for a 16bpc
TIFF image if one isn't present.
Full sample project here:
http://shapeof.com/archives/2019/media/SixteenBitPerComponentThumbnailLeak.zip
*/
void report_memory(void) {
struct task_basic_info info;
mach_msg_type_number_t size = sizeof(info);
kern_return_t kerr = task_info(mach_task_self(),
TASK_BASIC_INFO,
(task_info_t)&info,
&size);
if( kerr == KERN_SUCCESS ) {
NSLog(@"Used memory (in mb): %f", (double)info.resident_size / 1024.0 / 1024.0);
} else {
NSLog(@"Error with task_info(): %s", mach_error_string(kerr));
}
}
- (void)loadThumbnailsForImageWithName:(NSString *)name {
@autoreleasepool {
NSLog(@"Loading image %@", name);
NSURL *sixteenURL = [[NSBundle mainBundle] URLForImageResource:name];
assert(sixteenURL);
CGImageSourceRef imageSourceRef = CGImageSourceCreateWithURL((CFURLRef)sixteenURL, nil);
for (int i = 0; i < 500; i++) {
CGImageRef r = CGImageSourceCreateThumbnailAtIndex(imageSourceRef, 0, (CFDictionaryRef)@{(id)kCGImageSourceCreateThumbnailFromImageIfAbsent: @(YES), (id)kCGImageSourceThumbnailMaxPixelSize:@(256), (id)kCGImageSourceCreateThumbnailWithTransform: @(YES)});
assert(r);
CGImageRelease(r);
}
CFRelease(imageSourceRef);
NSLog(@"Finished with image %@", name);
}
}
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
report_memory();
[self loadThumbnailsForImageWithName:@"EightBit"];
report_memory();
[self loadThumbnailsForImageWithName:@"SixteenBit"];
report_memory();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment