我的平台
mac os 10.6
Xcode 3.2.6
编译时出的问题
- 2013-12-02 21:52:33.177 UniworkC[1662:4307] *** __NSAutoreleaseNoPool(): Object 0x134830 of class __NSCFDate autoreleased with no pool in place - just leaking
- 2013-12-02 21:52:33.178 UniworkC[1662:4307] *** __NSAutoreleaseNoPool(): Object 0x113900 of class NSCFNumber autoreleased with no pool in place - just leaking
- 2013-12-02 21:52:33.179 UniworkC[1662:4307] *** __NSAutoreleaseNoPool(): Object 0x10f590 of class NSCFLocale autoreleased with no pool in place - just leaking
- 2013-12-02 21:52:33.180 UniworkC[1662:4307] *** __NSAutoreleaseNoPool(): Object 0x30a4 of class NSCFString autoreleased
原来的代码
- void savePNGImage(CGImageRef imageRef, NSString *path)
- {
- NSURL *fileURL = [NSURL fileURLWithPath:path];
- CGImageDestinationRef dr = CGImageDestinationCreateWithURL(( CFURLRef)fileURL, kUTTypePNG , 1, NULL);
- CGImageDestinationAddImage(dr, imageRef, NULL);
- CGImageDestinationFinalize(dr);
- CFRelease(dr);
- }
- void save()
- {
- CGDirectDisplayID displayID = CGMainDisplayID();
- CGImageRef imageRef = CGDisplayCreateImage(displayID);
- NSDate* now = [NSDate date];
- NSDateFormatter* fmt = [[NSDateFormatter alloc] init];
- fmt.dateFormat = @"yyMMddHHmmss";
- //fmt.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
- NSString* dateString = [fmt stringFromDate:now];
- NSString *path = [[NSString stringWithFormat:@"~/Desktop/tmp/%@.png", dateString ] stringByExpandingTildeInPath];
- NSLog(@"save file: %@", path);
- savePNGImage(imageRef, path);
- CFRelease(imageRef);
- }
- void *screenCaputureFunc( void *para)
- {
- for(int i=0; i< 10; i++){
- sleep(10);
- save();
- }
- printf("end of capture\n");
- return (void *)0;
- }
更改为
- void savePNGImage(CGImageRef imageRef, NSString *path)
- {
- // references: http://stackoverflow.com/questions/8225838/save-cgimageref-to-png-file-errors-arc-caused
- NSURL *fileURL = [NSURL fileURLWithPath:path];
- CGImageDestinationRef dr = CGImageDestinationCreateWithURL(( CFURLRef)fileURL, kUTTypePNG , 1, NULL);
- CGImageDestinationAddImage(dr, imageRef, NULL);
- CGImageDestinationFinalize(dr);
- //CFRelease(dr);
- }
- void save()
- {
- // references: http://stackoverflow.com/questions/8225838/save-cgimageref-to-png-file-errors-arc-caused
- CGDirectDisplayID displayID = CGMainDisplayID();
- CGImageRef imageRef = CGDisplayCreateImage(displayID);
- NSDate* now = [NSDate date];
- NSDateFormatter* fmt = [[NSDateFormatter alloc] init];
- fmt.dateFormat = @"yyMMddHHmmss";
- //fmt.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
- NSString* dateString = [fmt stringFromDate:now];
- NSString *path = [[NSString stringWithFormat:@"~/Desktop/tmp/%@.png", dateString ] stringByExpandingTildeInPath];
- NSLog(@"save file: %@", path);
- savePNGImage(imageRef, path);
- //CFRelease(imageRef);
- }
- void *screenCaputureFunc( void *para)
- {
- for(int i=0; i< 10; i++){
- sleep(10);
- NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
- save();
- [pool release];
- }
- printf("end of capture\n");
- return (void *)0;
- }
增加的自动释放的内存池。
参考
- The error you get is caused by something somewhere creating an Objective-C class (NSURL) using the convenience static method [NSURL urlWithString:]. Methods that return objects that aren‘t "alloc" or "copy" should put the object inside an autorelease pool before returning the object. And since you haven‘t setup one up it‘ll just crash or leak memory.
- I‘m not sure exactly how to fix this but you need to put something like:
- NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
- doStuff();
- [pool release];
- somewhere in your code.
刚刚做MAC的开发,不能给出什么更深层次的解释,只是得到这么个心得:
有不能控制内存的代码,放在自动释放的内存池中。
以后再做解释吧
参考
http://stackoverflow.com/questions/2557562/using-apple-autorelease-pools-without-objective-c
时间: 2024-10-07 13:19:35