开发者

Should -dealloc do anything other than release memory?

I inherited an iPhone app at work and I'm new to Objective-C so I don't have my bearings just yet. I encountered code similar to this:

- (void) dealloc {
    [[StaticObject sharedObject] showSomeDialog];

    [super dealloc];
}

I know this is frowned upon in other languages. My spider sense is going crazy looking at that code.

Is this a common Objective-C idiom? Or do I have a c开发者_运维百科rappy codebase to fix?


You should not put UI code in a -dealloc. General rule of thumb, only use -dealloc to clean up what you've done: release objects, remove observers, etc.

Consider what would happen if this object lived on a thread other than the main thread... now you'd have UI code running on the non-main thread, which is a bad thing.


You can do such thing for some debugging reasons. But I don't think you should ever do anything like this! This means a dialog is prompted when an object is being deallocated. So if you need any mechanism to show a dialog at a certain time don't make it depended on an object being deallocated. In the dealloc method you should really just release all objects retained by the deallocated object. And not doing some fancy application features.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜