oc学习之旅:内存管理2修改数组为非ARC版本
来源:互联网 发布:淘宝网优依裤 编辑:程序博客网 时间:2024/06/10 06:53
为了重写数组的非arc版本,特意添加了一个Person类,把Person添加到自定义的数组类中,因为是强引用,所以添加完后把Person类释放掉,然后再把每个元素释放掉后会打印Person与自定义数组类的dealloc方法,顺序是先是调用Person的dealloc在调用自定义数组类的dealloc。每个功能都要实现内存管理,这样才不导致内存泄露
#import <Foundation/Foundation.h>
#import "Person.h"
#import "CBMutableArray.h"
int main(int argc,const char * argv[])
{
@autoreleasepool {
// NSMutableArray *>
CBMutableArray * arr = [[CBMutableArrayalloc] init];
Person *p = [[Personalloc] init];
Person *p2 = [[Personalloc] init];
Person *p3 = [[Personalloc] init];
Person *p4 = [[Personalloc] init];
p.str = [NSMutableStringstringWithString:@"wahaha"];
//数组添加对象 retain
[arrCBAddObject:p];
[arrCBAddObject:p2];
[arrCBAddObject:p3];
[arrCBAddObject:p4];
NSLog(@"p.retainCount is = %ld",p.retainCount);
[prelease];
[p2release];
[p3release];
[p4release];
[arrCBRemoveAtIndex:1];
Person *p5 = [[Personalloc] init];
[arr CBReplaceAtIndex:1withObject:p5];
[p5release];
[arrrelease];
}
return 0;
}
@interface CBMutableArray :NSObject
{
id _arr[127];
NSInteger _count;
}
- (void) CBAddObject:(id)obj;
- (void) CBRemoveAllObjects;
- (void) CBRemoveAtIndex:(NSInteger)index;
- (void) CBReplaceAtIndex:(NSInteger)index withObject:(id)obj;
- (id) CBObjectAtIndex:(NSInteger)index;
CBMutableArray.m
- (id)init
{
self = [superinit];
if (self) {
_count = 0;
}
return self;
}
- (void) CBAddObject:(id)obj
{
if (_arr[_count] !=obj) {
[_arr[_count]release];
_arr[_count] = [objretain];
_count++;
}
}
- (void) CBRemoveAllObjects
{
for (int i =0; i < _count; i++) {
[_arr[i]release];
NSLog(@"_arr[%i] release",i);
_arr[i] = nil;
}
_count = 0;
}
- (void) CBRemoveAtIndex:(NSInteger)index
{
if (index > _count-1) {
return;
}
[_arr[index]release];
// index之后的前移
for (NSInteger i = index; i <=_count-1; i++) {
_arr[i] = _arr[i+1];
}
_arr[_count] =nil;
_count--;
}
- (void) CBReplaceAtIndex:(NSInteger)index withObject:(id)obj
{
if (index > _count-1) {
return;
}
if (_arr[index] != obj) {
[_arr[index]release];
_arr[index] = [obj retain];
}
}
- (id) CBObjectAtIndex:(NSInteger)index
{
if (index > _count-1) {
return nil;
}
return _arr[index];
}
- (NSString *)description
{
NSMutableString * str = [NSMutableStringstringWithString:@" (\n"];
for (NSInteger i =0; i < _count; i++) {
// 最后一个元素
if (i == _count-1) {
[strappendFormat:@" %@\n",_arr[i]];
// 非最后一个元素
}else{
[strappendFormat:@" %@,\n",_arr[i]];
}
}
[strappendString:@")"];
return str;
}
-(void)dealloc
{
for (int i =0; i<_count; i++) {
[_arr[i]release];
NSLog(@"_arr[%i] dealloc",i);
}
NSLog(@"%@ dealloc",[selfclass]);
[superdealloc];
}
Person.h
@property(nonatomic,assign)NSString *str;
Person.m
-(void)dealloc
{
NSLog(@"==================\n");
NSLog(@"%@ dealloc",[selfclass]);
_str = nil;
[superdealloc];
}
2013-12-25 18:35:45.888 CounstomMSmutableArry[23716:303]>
2013-12-25 18:35:45.890 CounstomMSmutableArry[23716:303] ==================
2013-12-25 18:35:45.897 CounstomMSmutableArry[23716:303] Person dealloc
2013-12-25 18:35:45.898 CounstomMSmutableArry[23716:303] ==================
2013-12-25 18:35:45.899 CounstomMSmutableArry[23716:303] Person dealloc
2013-12-25 18:35:45.899 CounstomMSmutableArry[23716:303] ==================
2013-12-25 18:35:45.900 CounstomMSmutableArry[23716:303] Person dealloc
2013-12-25 18:35:45.900 CounstomMSmutableArry[23716:303] _arr[0] dealloc
2013-12-25 18:35:45.901 CounstomMSmutableArry[23716:303] ==================
2013-12-25 18:35:45.901 CounstomMSmutableArry[23716:303] Person dealloc
2013-12-25 18:35:45.901 CounstomMSmutableArry[23716:303] _arr[1] dealloc
2013-12-25 18:35:45.902 CounstomMSmutableArry[23716:303] ==================
2013-12-25 18:35:45.902 CounstomMSmutableArry[23716:303] Person dealloc
2013-12-25 18:35:45.903 CounstomMSmutableArry[23716:303] _arr[2] dealloc
2013-12-25 18:35:45.903 CounstomMSmutableArry[23716:303] CBMutableArray dealloc
- oc学习之旅:内存管理2修改数组为非ARC版本
- OC内存管理之ARC
- OC内存管理(非ARC)
- OC内存管理(ARC)
- oc学习之旅:内存管理1
- oc学习之旅:内存管理
- OC之非ARC设置
- OC学习笔记之OC内存管理
- 内存管理总结-ARC和非ARC
- 黑马程序员——OC内存管理(非ARC,手工管理)
- 内存管理之ARC
- 黑马程序员——OC内存管理复习(非ARC部分)
- OC内存管理教程之ARC(一)——objective-c内存管理和引用计数
- OC学习笔记09--OC中内存管理的ARC机制
- ARC/OC对象自动管理内存
- oc的ARC的内存管理
- OC内存管理--MRC&ARC
- OC内存管理 MRC与ARC
- [转] GCC 中的编译器堆栈保护技术
- 用Hibernate实现领域对象的自定义字段
- 黑马程序员_HTML
- 在Eclipse中配置动态加载Class的JVM插件--JavaRebel
- 笨方法实现字符串中字符频度的统计
- oc学习之旅:内存管理2修改数组为非ARC版本
- Starling MovieClip API
- 黑马程序员---------------------编程计算3乘8等于几,什么方法效率更高?
- Use Django to show data
- IOS---是做一个 Universal 应用好,还是分开做成 iPhone iPad 两个版本好?
- php截取中文字符串不乱码的方法
- 在sql2008上面建立dblink(链接服务器)
- openldap windows 下的安装与调试
- nova数据库模块的开发和使用