Differences between viewdidunload and dealloc)

Source: Internet
Author: User

From: http://www.cnblogs.com/zhwl/archive/2012/06/06/2538494.html

ForIPhone DevelopmentFor personnel, memory management is an extremely important technique, even ifProgramPowerful,DesignIf the memory is not properly controlled, the program may not be able to exit unexpectedly.DevelopmentIs completely different.

There are many channels in the memory control. Here we will analyze the differences between viewdidunload and dealloc.ArticleMany of them, but most of them are excerpted and translated official documents. Some of them are just a simple sentence and do not elaborate on the specific differences between the two.

To understand the differences between the two, you must first knowViewThere are a lot of articles in Google. You can search for them first. I will not explain them here.

AliasViewdidunloadIt refers to the statement executed after the view is detached, which echoes the viewdidload. Everyone knows that the official explanation is similar to the execution.

 

Self . myoutlet = nil ;

 

But why? When will this method be called?

This method cannot be called manually. Application The method automatically called when the program receives the phone memory warning to clear the memory. Except the currentViewcontrollerOther All viewcontroller files that have been loaded are no longer in use.Use To prevent the application from being forcibly shut down due to excessive memory consumption. Remember, yesDivisionOutside viewcontroller of the view currently being displayedAll viewcontroller commands that are already in the memoryViewdidunload Method Instead of executing viewdidunload on the current viewcontroller, these viewcontrollers are not dealloc. Therefore, in viewdidunload, iboutlet variables are usually released and data that can be reconstructed in viewdidload, viewwillappear, viewdidappear, and other methods. Data transmitted from other pages or data that cannot be reconstructed by viewdidload, viewwillappear, viewdidappear, and other methods cannot be released. For example, if the navigationcontroller is transmitted from the previous page Image If it is released in viewdidunload, the view cannot be restored when it is loaded again.

Why should we write it as self. myoutlet = nil; in fact, this syntax executes the setter method in property, rather than a simple variable assignment. It does two things: 1. Release old data, 2. New data (NiL) Retain (when property is set to retain) is of course meaningless to nil retain. If myoutlet = NIL is written, it simply points myoutlet to nil, so the memory will leak, because the old data has no release. If you only write it as [myoutlet release ],ProblemBecause the view will be release again when it is dealloc, and the program will go wrong, but there is no problem with nil release.
 
DeallocWhen the current viewcontroller is released, all the objects and data in the current viewcontroller are cleared to release the memory. This method is automatically called without manual execution. For example, when modalview is returned to the previous page by dismissmodalviewcontrolleranimated or navigationcontroller, this method is automatically called. Because this page is no longer used, you can release all objects and data (release.

In fact, the biggest difference between the two is: viewdidunload is that all viewcontrollers except the current memory are executed simultaneously, while dealloc is only executed by the current viewcontroller. This is not described on the Internet.

For personal notice, please correct the error!

PS: many friends have said that viewdidunload cannot be debugged. The method is to select hardware> Simulate Memory warning in the menu of the IOS simulator. At this time, you can see nslog in viewdidunload, you can try nslog in the opened viewcontroller to check the effect. In dealloc, nslog can be used directly.

From: http://www.cnblogs.com/zhwl/archive/2012/06/06/2538494.html

ForIPhone DevelopmentFor personnel, memory management is an extremely important skill, even if the program functions more powerful,DesignIf the memory is not properly controlled, the program may not be able to exit unexpectedly.DevelopmentIs completely different.

There are many channels in the memory control. Here we will analyze the differences between viewdidunload and dealloc. There are many articles about the differences between the two, but most of them are excerpted and translated official documents, some of them are just two simple sentences, but they do not elaborate on the specific differences between the two.

To understand the differences between the two, you must first knowViewThere are a lot of articles in Google. You can search for them first. I will not explain them here.

AliasViewdidunloadIt refers to the statement executed after the view is detached, which echoes the viewdidload. Everyone knows that the official explanation is similar to the execution.

 

Self . myoutlet = nil ;

 

But why? When will this method be called?

This method cannot be called manually. Application The method automatically called when the program receives the phone memory warning to clear the memory. Except the currentViewcontrollerOther All viewcontroller files that have been loaded are no longer in use.Use To prevent the application from being forcibly shut down due to excessive memory consumption. Remember, yesDivisionOutside viewcontroller of the view currently being displayedAll viewcontroller commands that are already in the memoryViewdidunload Method Instead of executing viewdidunload on the current viewcontroller, these viewcontrollers are not dealloc. Therefore, in viewdidunload, iboutlet variables are usually released and data that can be reconstructed in viewdidload, viewwillappear, viewdidappear, and other methods. Data transmitted from other pages or data that cannot be reconstructed by viewdidload, viewwillappear, viewdidappear, and other methods cannot be released. For example, if the navigationcontroller is transmitted from the previous page Image If it is released in viewdidunload, the view cannot be restored when it is loaded again.

Why should we write it as self. myoutlet = nil; in fact, this syntax executes the setter method in property, rather than a simple variable assignment. It does two things: 1. Release old data, 2. New data (NiL) Retain (when property is set to retain) is of course meaningless to nil retain. If myoutlet = NIL is written, it simply points myoutlet to nil, so the memory will leak, because the old data has no release. If you only write it as [myoutlet release ],ProblemBecause the view will be release again when it is dealloc, and the program will go wrong, but there is no problem with nil release.
 
DeallocWhen the current viewcontroller is released, all the objects and data in the current viewcontroller are cleared to release the memory. This method is automatically called without manual execution. For example, when modalview is returned to the previous page by dismissmodalviewcontrolleranimated or navigationcontroller, this method is automatically called. Because this page is no longer used, you can release all objects and data (release.

In fact, the biggest difference between the two is: viewdidunload is that all viewcontrollers except the current memory are executed simultaneously, while dealloc is only executed by the current viewcontroller. This is not described on the Internet.

For personal notice, please correct the error!

PS: many friends say that viewdidunload cannot be debugged. The method is to select hardware> Simulate Memory warning in the menu of the IOS simulator. At this time, you can see nslog in viewdidunload, you can try nslog in the opened viewcontroller to check the effect. In dealloc, nslog can be used directly.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.