views:

86

answers:

6

Hi, I want to know what is the difference between before releasing my object of [super delloc] and after releasing my object of line [super delloc]

For example, Before..

- (void)dealloc {
    [theAudioPlayer stop];

    [soundFilePath release];

    [theAudioPlayer release];

    [super dealloc];
}

Now after releasing object..

- (void)dealloc {

    [super dealloc];

    [theAudioPlayer stop];

    [soundFilePath release];

    [theAudioPlayer release];
}

When i used 1st case and i navigating my viewController firstly 1st to 2nd viewController class and again come backe 2nd to 1st then it will give me some error something following console.

#0  0x02d29c93 in objc_msgSend ()
#1  0x0628ae60 in ?? ()
#2  0x02b24814 in __CFURLDeallocate ()
#3  0x02b23ed0 in _CFRelease ()
#4  0x0012af48 in -[NSURL release] ()
#5  0x02795827 in -[AVAudioPlayer dealloc] ()
#6  0x0000480e in -[ViewController dealloc] (self=0x6285340, _cmd=0x2c0a934) at /Users/ajm/Desktop/DetectiveJone/Classes/ViewController.m:209
#7  0x02d23e9d in objc_setProperty ()
#8  0x0039f7b3 in -[UINavigationController setDisappearingViewController:] ()
#9  0x0039ce91 in -[UINavigationController _clearLastOperation] ()
#10 0x0039d732 in -[UINavigationController navigationTransitionView:didEndTransition:fromView:toView:] ()
#11 0x0054d25f in -[UINavigationTransitionView _notifyDelegateTransitionDidStopWithContext:] ()
#12 0x0054e39e in -[UINavigationTransitionView _navigationTransitionDidStop] ()
#13 0x00325d54 in -[UIViewAnimationState sendDelegateAnimationDidStop:finished:] ()
#14 0x00325be6 in -[UIViewAnimationState animationDidStop:finished:] ()
#15 0x048a1933 in run_animation_callbacks ()
#16 0x048a17da in CA::timer_callback ()
#17 0x02b497dc in CFRunLoopRunSpecific ()
#18 0x02b488a8 in CFRunLoopRunInMode ()
#19 0x0356989d in GSEventRunModal ()
#20 0x03569962 in GSEventRun ()
#21 0x00307372 in UIApplicationMain ()
#22 0x00002018 in main (argc=1, argv=0xbffff050) at /Users/ajm/Desktop/DetectiveJone/main.m:14
(gdb) 
A: 

Well, one aspect may have to do with performance. Imagine your superclass is some class you know nothing about. That means you have no idea what [super dealloc] may do. Perhaps it has an array member and has to call release on all its items. If you have multiple threads and are locking, assuming that calling [super dealloc] will be quick, you may be gravely mistaken.

Along this line, its probably best to call [super dealloc] last. Clean up the stuff that you are responsible for first, then pass control off.

As for the technical reason why or what the difference, I am curious myself.

darren
+1  A: 

You have to release the object of your class before release the one of the superclass. It is the inverse operation of a new.

When you call the [supper dealloc] you could consider that the object is no more an object of your class, but of the super class. (I don"'t know if I really clear...)

From Memory Management Programming Guide:

If your class has object instance variables that it owns, you must implement a dealloc method that releases them, and then invokes super’s implementation.

Benoît
A: 

In the second example, you cannot access any instance variables defined in superclasses anymore, because they are already released.

Keep this in mind, although you can execute code after [super dealloc]. It is merely a convention to avoid mistakes in the first place.

muffix
You also can't access any instance variables defined in the current class. The only code that you could reasonably execute after `[super dealloc]` would be code that only touches static and global variables.
Chuck
A: 

do it the first way.

the difference is that your superclass is torn down before your ivars and you should assume that your members are also now pointing to invalid memory. the 'after' is dangerous, especially when your ivars to destroy use anything in the interface of the object which is being destroyed, although there are other dangerous cases.

note: it's best to destroy something like an audio player outside of `dealloc'.

Justin
+9  A: 

The second way is categorically wrong. NSObject's dealloc literally deallocates the object, and [super dealloc] inevitably reaches NSObject's implementation for any properly written Cocoa class. This means that the memory for the object executing the dealloc method is no longer allocated, and accessing the memory that used to be the object's instance variables — even just to send them release — is undefined behavior.

Because of this, [super dealloc] should always come at the end of the method. You can't do anything meaningful after that line executes.

Chuck
@Chuck, thanks. That why when i play sound in 1st viewController class it will continue running after navigating 2nd viewController. I want sound should be play infinitely when i m currently 1st viewController class then the sound will stop and object should release when i navigating 2nd viewController.Which case should i have used there.
RRB
A: 

while deallocating the resources you have to deallocate the child resources first and next you have to deallocate the super.

Thankyou prasad