iPhone开发学习 内存释放注意事项

移动开发 iOS
本文介绍的是iPhone开发学习 内存释放注意事项,关于内存的操作也已经与友们进行了分享,我们先来看本篇文章。

iPhone开发学习 内存释放注意事项是本文要介绍的内容,iPhone内存管理中,涉及到malloc在堆上分配内存时,需要使用对应的free释放堆内存,而不是单纯的release相关的对象了事,如下代码:

  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 

在进行初始化时为innerItem指针在堆空间上分配内存,但是要记得调用free函数进行堆空间的释放,代码如下:

  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];  

小结:iPhone开发学习 内存释放注意事项的内容介绍完了,希望本文对你有所帮助。

责任编辑:zhaolei 来源: 互联网
相关推荐

2011-07-21 15:40:24

iPhone 内存管理 对象

2011-08-01 12:53:25

iPhone 多线程 线程

2011-07-06 11:13:29

iOS游戏开发

2009-12-16 16:02:30

Visual Stud

2010-02-03 14:49:54

Python 模块

2009-12-15 10:10:38

VS 2008开发

2021-10-08 11:45:33

内存HeapByteBuf堆内

2010-01-14 18:19:40

C++语言

2010-01-25 18:12:28

C++

2011-07-21 15:20:31

iPhone SDK 多线程

2012-03-11 18:46:18

iPhone4S

2011-05-26 11:22:04

SEO

2010-01-26 16:54:58

学习C++

2010-01-27 09:12:01

C++语言学习

2009-12-08 09:45:50

调用WCF

2009-12-15 17:47:17

VSIP

2009-12-16 15:41:10

Ruby on Rai

2009-12-21 09:53:45

连接Oracle

2010-05-07 10:19:48

Oracle 注意事项

2014-07-01 12:49:06

Android Stu安装
点赞
收藏

51CTO技术栈公众号