Difference between release of object before and after approval [super delloc]?

I want to know what the difference is between before freeing my [super delloc] object and after freeing my line object [super delloc]

For example, Before ..

- (void)dealloc { [theAudioPlayer stop]; [soundFilePath release]; [theAudioPlayer release]; [super dealloc]; } 

Now after the release of the object ..

 - (void)dealloc { [super dealloc]; [theAudioPlayer stop]; [soundFilePath release]; [theAudioPlayer release]; } 

When I used the 1st case, and I switched to my viewController first from 1st to 2nd class, viewController and returned from 2nd to 1st again, then it will give me some error, something like the 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) 
+4
source share
5 answers

The second method is categorically erroneous. NSObject dealloc literally frees the object, and [super dealloc] inevitably reaches the NSObject implementation for any correctly written Cocoa class. This means that the memory for the object executing the dealloc method is no longer allocated, and access to the memory that uses the instance variables of the object — even just sending them release — is undefined.

Because of this, [super dealloc] should always come at the end of a method. After completing this line, you cannot do anything meaningful.

+10
source

You must free an object of your class before releasing one of the superclass. This is the inverse of the new.

When you call [supper dealloc], you can assume that the object is no longer an object of your class, but a superclass. (I don't know if I'm really right ...)

From Memory Management Programming Guide :

If your class has instance variables of the object that it owns, you must implement the dealloc method, which releases them, and then call the supers implementation.

+1
source

Well, one aspect may be related to performance. Imagine that your superclass is a class that you know nothing about. This means that you do not know what [super dealloc] can do. Perhaps it has an array member and should trigger a release on all its elements. If you have multiple threads and are blocking, assuming the call [super dealloc] will be fast, you could be seriously wrong.

Along this line, it is probably best to call [super dealloc] last. First clean the material you are responsible for, then turn off the controls.

As for the technical reason why or what the difference is, I wonder.

0
source

do it the first way.

the difference is that your superclass is torn down in front of your ivars, and you should assume that your members also now point to an invalid memory. The “after” is dangerous, especially when your ivars destroy the use of something in the interface of the object that is destroyed, although there are other dangerous cases.

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

0
source

when freeing the resources you have to free from child resources and then you have to free super.

Thankyou prasad

0
source

All Articles