iOS中正确处理dealloc方法

本文详细解释了iOS开发中对象初始化和销毁的过程,强调了初始化时先调用父类构造方法的原则,以及销毁时遵循的子类先行原则。通过具体代码示例展示了正确的dealloc方法实现方式,避免内存管理错误。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

当我们继承类的时候,子类实例在构造的时候顺序是先高用父类的构造方法,再调用子类的构造方法。在c/c++是如此,在objc中也是如此,在iOS开发中,我们会看到这样的代码:

- (void)init
{
    self = [super init];
    if (self)
    {
        //init
    }

    return self;
}

看到没,初始化的时候都是先调用父类的初始化方法,为什么呢,因为父类更老,当然是先出生了。 大笑,同样的情况可以在viewDidLoad中看到。


而销毁的时候则是相反的顺序,先销毁子类里分配的空间,再销毁父类的。如:

- (void)dealloc {
    [companion release];
    free(myBigBlockOfMemory);
    [super dealloc];
}

为什么会是这个顺序呢?因为长江后浪推前浪,子类是继承了父类的优点,发挥了自己长外, 敌人要想消灭对方,当然是先灭最强的,都说树大招风,就是这个道理。在销毁的时候如果不按这个顺序,有时候可能会crash。如在子类中应用了:

[outputStream setDelegate:self];
[outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];

如果dealloc写这成这样就会在removeFromRunLoop的时候crash:

- (void)dealloc {
    [super dealloc];
    if (outputStream) {
        [outputStream close];
        [outputStream removeFromRunLoop:[NSRunLoop currentRunLoop]
                                forMode:NSDefaultRunLoopMode];
        [outputStream release];
        outputStream = nil;
    }
    delegate = nil;
}

如果写成这样就ok了:

- (void)dealloc {
    if (outputStream) {
        [outputStream close];
        [outputStream removeFromRunLoop:[NSRunLoop currentRunLoop]
                                forMode:NSDefaultRunLoopMode];
        [outputStream release];
        outputStream = nil;
    }
    delegate = nil;
    [super dealloc]; // must be last!
}


在xCode version:4.2.1及以前版本中开发iOS的时候,如果将super dealloc写在子类dealloc中前面的时候是不会出错的,但在xCode version4.3.2中,它会有自动检测super dealloc的功能,如果写在前面,则会crash.


苹果官网也推荐这样写,请参看: https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSObject_Class/Reference/Reference.html#//apple_ref/occ/instm/NSObject/dealloc 中的特别注意事项中

Special Considerations
When garbage collection is enabled, the garbage collector sends finalize to the receiver instead of dealloc, and this method is a no-op.

If you are using manual reference counting, subclasses must implement their own versions of dealloc to allow the release of any additional memory consumed by the object—such as dynamically allocated storage for data or object instance variables owned by the deallocated object. After performing the class-specific deallocation, the subclass method should incorporate superclass versions of dealloc through a message to super









评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值