zoukankan      html  css  js  c++  java
  • __NSAutoreleaseNoPool(): ... utoreleased with no pool in place

    __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 ");
    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 ");
    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

  • 相关阅读:
    what are Datatypes in SQLite supporting android
    Version of SQLite used in Android?
    使用(Drawable)资源———ShapeDrawable资源
    使用(Drawable)资源——LayerDrawable资源
    使用(Drawable)资源——StateListDrawable资源
    使用(Drawable)资源——图片资源
    数组(Array)资源
    使用字符串、颜色、尺寸资源
    资源的类型及存储方式——使用资源
    资源的类型及存储方式——资源的类型以及存储方式
  • 原文地址:https://www.cnblogs.com/timssd/p/4781141.html
Copyright © 2011-2022 走看看