Skip to content

Instantly share code, notes, and snippets.

@echristopherson
Created September 5, 2011 23:36
Show Gist options
  • Save echristopherson/1196173 to your computer and use it in GitHub Desktop.
Save echristopherson/1196173 to your computer and use it in GitHub Desktop.
// Save as mem_leak_objc.m and compile with:
// gcc -o mem_leak_objc mem_leak_objc.m -lobjc -framework Foundation
#import <Foundation/Foundation.h>
#import <stdio.h> // for printf and friends
#import <stdarg.h> // for va_list and va_start
void P(NSString *fmt, ...) {
va_list va;
va_start(va, fmt);
NSAutoreleasePool *tempPool = [NSAutoreleasePool new];
NSString *formatted = [[NSString alloc]
initWithFormat:fmt arguments:va];
printf("%s", [formatted UTF8String]); // printf issues warning if I pass it just the string
[formatted release];
printf("\n");
[tempPool release];
}
int main() {
NSAutoreleasePool *pool = [NSAutoreleasePool new];
int i;
for(i = 0; i < 1000000000; ++ i) {
P(@"No longer leaking %@!", @"memory");
}
[pool release];
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment