Chinaunix首页 | 论坛 | 博客
  • 博客访问: 542896
  • 博文数量: 104
  • 博客积分: 2089
  • 博客等级: 大尉
  • 技术积分: 1691
  • 用 户 组: 普通用户
  • 注册时间: 2010-06-29 08:48
文章分类

全部博文(104)

文章存档

2015年(1)

2013年(13)

2012年(31)

2011年(59)

分类: iOS平台

2013-12-02 22:21:48

我的平台
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的开发,不能给出什么更深层次的解释,只是得到这么个心得:
有不能控制内存的代码,放在自动释放的内存池中。
以后再做解释吧

参考




阅读(1453) | 评论(0) | 转发(0) |
0

上一篇:NSDateFormatter

下一篇:blender资源篇

给主人留下些什么吧!~~