Is the dealloc method actually executed when I exit the application?

I use the following code (e.g. in my appController.m application) to do some cleanup when my application terminates ...

- (void) dealloc { [myObject release]; // myObject dealloc will not be called either !!! [arraySMSs release]; [super dealloc]; } 

This method is never called when the application closes! What for? Is there a better place for my cleaning? The fact that it is not called the memory leak problem addresses? Or will the OS take care of cleaning up?

Thanks...

+4
source share
4 answers

There is no reason for the system to guarantee that each object will be freed separately from the end of the application.

This is just a waste of processor cycles and a waste of user time.

When the application terminates, all resources used by this application are restored by the system in a completely automatic and inevitable way.

If you need something to happen when the application terminates, use delegation of application delegation for this. But do not rely on it. The user can force a reboot of the device or force exit the application at will.

+8
source

Good question, I was also confused.

now i got this:

It is said that there is no object managed by our custom code that the appDelegate class itself needs, we really do not need to worry to “free” its instance. UIApplication is the only class that saves it, but we should not.

But, for academic discussion or if there is some purpose that I don’t know at the moment when you want to test dealloc in the class appDelegate:

applicationWillTerminate is the right place to find out if your application will close.

 - (void)applicationWillTerminate:(UIApplication *)application { // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. [UIApplication sharedApplication].delegate = nil; // after this, the dealloc method of our appDelegate class will be called } 
+3
source

Here is a quote from the NSObject link: Important: note that when the application terminates, the dealloc message cannot be sent to the objects, since the process memory is automatically cleared upon exit - it is more efficient to simply allow the operating system to clear resources than to call all memory management methods. " largely confirms what many people have said.

+2
source

What makes you think dealloc is not called? Did you run this debugger? Please check out this question, why can you not necessarily call NSLog in the dealloc method: when executing dealloc?

-1
source

Source: https://habr.com/ru/post/1340801/


All Articles