__NSAutoreleaseNoPool(): ... utoreleased with no pool in place - just leaking

__NSAutoreleaseNoPool(): ... utoreleased with no pool in place - just leaking

我的平台

mac os 10.6
Xcode 3.2.6

编译时出的问题

  1. 2013-12-02 21:52:33.177 UniworkC[1662:4307] *** __NSAutoreleaseNoPool(): Object 0x134830 of class __NSCFDate autoreleased with no pool in place - just leaking
  2. 2013-12-02 21:52:33.178 UniworkC[1662:4307] *** __NSAutoreleaseNoPool(): Object 0x113900 of class NSCFNumber autoreleased with no pool in place - just leaking
  3. 2013-12-02 21:52:33.179 UniworkC[1662:4307] *** __NSAutoreleaseNoPool(): Object 0x10f590 of class NSCFLocale autoreleased with no pool in place - just leaking
  4. 2013-12-02 21:52:33.180 UniworkC[1662:4307] *** __NSAutoreleaseNoPool(): Object 0x30a4 of class NSCFString autoreleased


原来的代码

  1. void savePNGImage(CGImageRef imageRef, NSString *path)
  2. {
  3.     
  4.     NSURL *fileURL = [NSURL fileURLWithPath:path];
  5.     CGImageDestinationRef dr = CGImageDestinationCreateWithURL(( CFURLRef)fileURL, kUTTypePNG , 1, NULL);

  6.     CGImageDestinationAddImage(dr, imageRef, NULL);
  7.     CGImageDestinationFinalize(dr);
  8.     
  9.     CFRelease(dr);
  10. }

  11. void save()
  12. {
  13.     CGDirectDisplayID displayID = CGMainDisplayID();
  14.     CGImageRef imageRef = CGDisplayCreateImage(displayID);
  15.     
  16.     NSDate* now = [NSDate date];
  17.     NSDateFormatter* fmt = [[NSDateFormatter alloc] init];
  18.     fmt.dateFormat = @"yyMMddHHmmss";
  19.     //fmt.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
  20.     NSString* dateString = [fmt stringFromDate:now];
  21.     
  22.     NSString *path = [[NSString stringWithFormat:@"~/Desktop/tmp/%@.png", dateString ] stringByExpandingTildeInPath];
  23.     NSLog(@"save file: %@", path);
  24.     savePNGImage(imageRef, path);
  25.     
  26.     CFRelease(imageRef);    
  27. }


  28. void *screenCaputureFunc( void *para)
  29. {
  30.     for(int i=0; i< 10; i++){
  31.         sleep(10);
  32.         save();
  33.     }
  34.     
  35.     printf("end of capture\n");
  36.     return (void *)0;
  37. }



更改为

  1. void savePNGImage(CGImageRef imageRef, NSString *path)
  2. {
  3.     
  4.     // references: http://stackoverflow.com/questions/8225838/save-cgimageref-to-png-file-errors-arc-caused
  5.     NSURL *fileURL = [NSURL fileURLWithPath:path];
  6.     CGImageDestinationRef dr = CGImageDestinationCreateWithURL(( CFURLRef)fileURL, kUTTypePNG , 1, NULL);

  7.     CGImageDestinationAddImage(dr, imageRef, NULL);
  8.     CGImageDestinationFinalize(dr);
  9.     
  10.     //CFRelease(dr);
  11. }

  12. void save()
  13. {
  14.     // references: http://stackoverflow.com/questions/8225838/save-cgimageref-to-png-file-errors-arc-caused
  15.     
  16.     CGDirectDisplayID displayID = CGMainDisplayID();
  17.     CGImageRef imageRef = CGDisplayCreateImage(displayID);
  18.     
  19.     NSDate* now = [NSDate date];
  20.     NSDateFormatter* fmt = [[NSDateFormatter alloc] init];
  21.     fmt.dateFormat = @"yyMMddHHmmss";
  22.     //fmt.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
  23.     NSString* dateString = [fmt stringFromDate:now];
  24.     
  25.     NSString *path = [[NSString stringWithFormat:@"~/Desktop/tmp/%@.png", dateString ] stringByExpandingTildeInPath];
  26.     NSLog(@"save file: %@", path);
  27.     savePNGImage(imageRef, path);
  28.     
  29.     //CFRelease(imageRef);    
  30. }


  31. void *screenCaputureFunc( void *para)
  32. {
  33.     for(int i=0; i< 10; i++){
  34.         sleep(10);
  35.         
  36.         NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
  37.         save();
  38.         [pool release];
  39.     }
  40.     
  41.     printf("end of capture\n");
  42.     return (void *)0;
  43. }


增加的自动释放的内存池。
参考

  1. 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.
  2. I'm not sure exactly how to fix this but you need to put something like:

  3. NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
  4. doStuff();
  5. [pool release];
  6. somewhere in your code.

刚刚做MAC的开发,不能给出什么更深层次的解释,只是得到这么个心得:
有不能控制内存的代码,放在自动释放的内存池中。
以后再做解释吧

参考
http://stackoverflow.com/questions/2557562/using-apple-autorelease-pools-without-objective-c

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值