iPhoneMac Objective-C内存管理教程和原理剖析(四)系统自动创建新的autorelease pool

    技术2024-03-28  16

    版权声明

    此文版权归作者 Vince Yuan (vince.yuan#gmail.com)所有。欢迎非营利性转载,转载时必须包含原始链接 http://vinceyuan.cnblogs.com/ ,且必须包含此版权声明的完整内容。

    版本 1.1 发表于 2010-03-08

     

     

    系统自动创建新的 autorelease pool

    在生成新的 Run Loop 的时候,系统会自动创建新的 autorelease pool (非常感谢网友 hhyytt neogui 的提醒)。注意,此处不同于 xcode 在新建项目时自动生成的代码中加入的 autorelease pool xcode 生成的代码可以被删除,但系统自动创建的新的 autorelease pool 是无法删除的(对于无 Garbage Collection 的环境来说)。 Objective-C 没有给出实现代码,官方文档也没有说明,但我们可以通过小程序来证明。

    在这个小程序中,我们先生成了一个 autorelease pool ,然后生成一个 autorelease ClassA 的实例,再在一个新的 run loop 中生成一个 autorelease ClassB 的对象(注意,我们并没有手动在新 run loop 中生成 autorelease pool )。 精简的示例代码如下,详细代码请见附件中的 memman-run-loop-with-pool.m

    int main(int argc, char**argv) 

    {

             NSLog(@"create an autorelasePool/n");

             NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];   

     

             NSLog(@"create an instance of ClassA and autorelease/n");

             ClassA *obj1 = [[[ClassA alloc] init] autorelease];

             NSDate *now = [[NSDate alloc] init];

             NSTimer *timer = [[NSTimer alloc] initWithFireDate:now

                       interval:0.0

                       target:obj1

                       selector:@selector(createClassB)

                       userInfo:nil

                       repeats:NO];

             NSRunLoop *runLoop = [NSRunLoop currentRunLoop];

             [runLoop addTimer:timer forMode:NSDefaultRunLoopMode];

             [timer release];

             [now release];

             [runLoop run]; // 在新 loop 中调用一函数, 生成 ClassB autorelease 实例

     

             NSLog(@"releasing autorelasePool/n");

             [pool release];

             NSLog(@"autorelasePool is released/n");

             return 0;

    输出如下:

    create an autorelasePool

    create an instance of ClassA and autorelease

    create an instance of ClassB and autorelease

    ClassB destroyed

    releasing autorelasePool

    ClassA destroyed

    autorelasePool is released

    注意在我们销毁 autorelease pool 之前, ClassB autorelease 实例就已经被销毁了。

    有人可能会说,这并不能说明新的 run loop 自动生成了一个新的 autorelease pool ,说不定还只是用了老的 autorelease pool ,只不过后来 drain 了一次而已。我们可以在 main 函数中不生成 autorelease pool 。精简的示例代码如下,详细代码请见附件中的 memman-run-loop-without-pool.m

    int main(int argc, char**argv) 

    {

             NSLog(@"No autorelasePool created/n");

     

             NSLog(@"create an instance of ClassA/n");

             ClassA *obj1 = [[ClassA alloc] init];

             NSDate *now = [[NSDate alloc] init];

             NSTimer *timer = [[NSTimer alloc] initWithFireDate:now

                       interval:0.0

                       target:obj1

                       selector:@selector(createClassB)

                       userInfo:nil

                       repeats:NO];

             NSRunLoop *runLoop = [NSRunLoop currentRunLoop];

             [runLoop addTimer:timer forMode:NSDefaultRunLoopMode];

             [timer release];

             [now release];

             [runLoop run]; // 在新 loop 中调用一函数,生成 ClassB autorelease 实例

             NSLog(@"Manually release the instance of ClassA/n");

             [obj1 release];

     

             return 0;

    输出如下:

    No autorelasePool created

    create an instance of ClassA

    create an instance of ClassB and autorelease

    ClassB destroyed

    Manually release the instance of ClassA

    ClassA destroyed

    我们可以看出来,我们并没有创建任何 autorelease pool ,可是 ClassB 的实例依然被自动销毁了,这说明新的 run loop 自动创建了一个 autorelease pool ,这个 pool 在新的 run loop 结束的时候会销毁自己(并自动 release 所包含的对象)。

     

    补充说明

    在研究 retain count 的时候,我不建议用 NSString 。因为在下面的语句中,

    NSString *str1 = @”constant string”;

    str1 retain count 是个很大的数字。 Objective-C 对常量字符串做了特殊处理。

    当然,如果你这样创建 NSString ,得到的 retain count 依然为 1

    NSString *str2 = [NSString stringWithFormat:@”123”];

     

      示例代码文件链接: http://files.cnblogs.com/VinceYuan/objective-c-memman.zip

    最新回复(0)