views:

38

answers:

2

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?

A: 

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.

V1ru8
+2  A: 

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.

Darryl H. Thomas
Short version: "Yes, you may have a crappy codebase to fix." ;-)
Darryl H. Thomas
Good to see my intuition is confirmed.
ageektrapped