开发者社区> 问答> 正文

关于iOS内存释放的问题

我的类如下:

头文件:

#import "BmobDanmaku.h"

@interface BmobBubbleDanmaku : BmobDanmaku
@property NSMutableArray *listData;
@property UITableView *tableView;
@property UITableViewCell *tableViewCell;
-(instancetype)init;

@end

实现文件:

//
//  BmobBubbleDanmaku.m
//  BmobDanmaku
//
//  Created by limao on 15/4/14.
//  Copyright (c) 2015年 Bmob. All rights reserved.
//

#import "BmobBubbleDanmaku.h"
@interface BmobBubbleDanmaku()<UITableViewDataSource,UITableViewDelegate>


@end

@implementation BmobBubbleDanmaku

@synthesize listData=_listData;

@synthesize tableView = _tableView;

@synthesize tableViewCell =_tableViewCell;

-(instancetype)init{
    if (self = [super init]) {
        //初始化表格
        self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 200, 200) style:UITableViewStylePlain];
        // 设置协议,意思就是UITableView类的方法交给了tabView这个对象,让完去完成表格的一些设置操作
        self.tableView.delegate=self;
        self.tableView.dataSource=self;
        self.listData = [[NSMutableArray alloc] initWithCapacity:0];
        [self addSubview:self.tableView];
    }
    return self;
}

//返回多少个section
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.listData count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //    声明静态字符串型对象,用来标记重用单元格
    static NSString *TableSampleIdentifier = @"TableSampleIdentifier";
    //    用TableSampleIdentifier表示需要重用的单元
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:TableSampleIdentifier];
    //    如果如果没有多余单元,则需要创建新的单元
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:TableSampleIdentifier];
    }

    else {
        while ([cell.contentView.subviews lastObject ]!=nil) {
            [(UIView*)[cell.contentView.subviews lastObject]removeFromSuperview];
        }
    }

    //    获取当前行信息值
    NSUInteger row = [indexPath row];
    //    填充行的详细内容
    cell.detailTextLabel.text = @"详细内容";
    //    把数组中的值赋给单元格显示出来
    cell.textLabel.text=[self.listData objectAtIndex:row];


    //    cell.textLabel.backgroundColor= [UIColor greenColor];

    //    表视图单元提供的UILabel属性,设置字体大小
    cell.textLabel.font = [UIFont boldSystemFontOfSize:40.0f];

    //    设置单元格UILabel属性背景颜色
    cell.textLabel.backgroundColor=[UIColor clearColor];
    return cell;
}

-(void)dealloc{
    NSLog(@"dealloc");
}

@end

调用的代码如下:

- (void)viewDidLoad {
    [super viewDidLoad];
    NSMutableArray *array = [NSMutableArray arrayWithObjects:@"张三",@"张四",@"张五",@"李三",@"李四",@"李五",@"李六",@"王三",@"王四",@"王五",@"王六",@"王七",@"王八",@"王九",@"王十", nil];
    BmobBubbleDanmaku *bmobBubbleDanmaku = [[BmobBubbleDanmaku alloc] init];
    bmobBubbleDanmaku.listData = array;
    [self.view addSubview:bmobBubbleDanmaku.tableView];
}

运行后提示错误如下:

 2015-04-14 22:55:58.289 BmobDanmaku[4909:267989] dealloc
 2015-04-14 22:55:58.296 BmobDanmaku[4909:267989] *** -[BmobBubbleDanmaku numberOfSectionsInTableView:]: message sent to deallocated instance 0x7d0bab20

也就是运行到numberOfSectionsInTableView时,我的bmobBubbleDanmaku已经被释放掉了,但是我不理解为什么会被释放了,

展开
收起
爵霸 2016-03-13 10:12:01 2372 0
1 条回答
写回答
取消 提交回答
  • 调用代码中
    BmobBubbleDanmaku *bmobBubbleDanmaku = [[BmobBubbleDanmaku alloc] init];
    是个局部引用
    后面你又用
    [self.view addSubview:bmobBubbleDanmaku.tableView];
    把tableView加载到了当前的视图上,然后tableView就被retain了
    而viewDidLoad调用完成后,bmobBubbleDanmaku指向的对象会被释放,而tableView因为加载到了视图上,所以被retain了,当tableView要数据时
    会调用代理,而你的代理都被指定为self了,也就是bmobBubbleDanmaku指向的对象,而这个东西已经被释放了,自然会报错。两者的生命周期不一样导致
    了这样的问题。
    所以如果bmobBubbleDanmaku指向的对象是个视图,不妨将之加载到视图让,让其和tableView生命周期一致。否则的就再外部保留一个bmobBubbleDanmaku的引用。另外,个人认为在一个继承自NSObject中持有UIView类的对象不太好,我个人是不太愿意使用这样的做法。

    2019-07-17 19:01:58
    赞同 展开评论 打赏
问答分类:
问答标签:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
手淘iOS性能优化探索 立即下载
From Java/Android to Swift iOS 立即下载
深入剖析iOS性能优化 立即下载