Should -dealloc do anything other than release memory?
Asked Answered
L

2

8

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 crappy codebase to fix?

Loosejointed answered 23/7, 2010 at 13:42 Comment(0)
L
7

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.

Livvyy answered 23/7, 2010 at 13:57 Comment(2)
Short version: "Yes, you may have a crappy codebase to fix." ;-)Livvyy
Good to see my intuition is confirmed.Loosejointed
N
0

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.

Neurology answered 23/7, 2010 at 13:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.