iOS中正确处理dealloc方法
iOS中正确处理dealloc方法
当我们继承类的时候,子类实例在构造的时候顺序是先高用父类的构造方法,再调用子类的构造方法。在c/c++是如此,在objc中也是如此,在iOS开发中,我们会看到这样的代码:
- - (void)init
- {
- self = [super init];
- if (self)
- {
- //init
- }
- return self;
- }
而销毁的时候则是相反的顺序,先销毁子类里分配的空间,再销毁父类的。如:
- - (void)dealloc {
- [companion release];
- free(myBigBlockOfMemory);
- [super dealloc];
- }
- [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;
- }
- - (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.
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
评论暂时关闭