Precautions for memory release for iPhone development and learning

Source: Internet
Author: User

IPhoneDevelopment LearningMemoryRelease Notes are described in this article,IPhone memoryManagement, involving malloc allocation on the heapMemoryYou need to use the corresponding free to release the heap memory, rather than simply release related objects. The following code:

 
 
  1. @interface MemoryTestProjViewController : UIViewController {  
  2. @public  
  3. NSMutableArray * memoryArray;  
  4. }  
  5.  
  6. @end  
  7. @interface MemoryTestProjItem : NSObject  
  8. {  
  9. @public  
  10. char * innerItem;  
  11. }  
  12. @end 

During initialization, The innerItem pointer allocates memory on the heap space, but remember to call the free function to release the heap space. The Code is as follows:

 
 
  1. - (void)viewDidUnload {  
  2. // Release any retained subviews of the main view.  
  3. // e.g. self.myOutlet = nil;  
  4. memoryArray = [[NSMutableArray alloc] initWithCapacity:10];  
  5. for(int i = 0;i < 1024;i++)  
  6. {  
  7.      MemoryTestProjItem* item = [[MemoryTestProjItem alloc] init];  
  8.  
  9.      item->innerItem = (char*)malloc(1024);  
  10.  
  11.      [memoryArray addObject:item];  
  12.      [item release];  
  13.      free(item->innerItem);  
  14.      free(item);  
  15. }  
  16. [memoryArray removeAllObjects];  

Summary:IPhoneDevelopment LearningMemoryThe release notes are described. I hope this article will help you.

Related Article

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.