林猫副园长 2015-04-14 15:01 采纳率: 0%
浏览 1769

关于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已经被释放掉了,但是我不理解为什么会被释放了,求高手帮忙解答一下。

  • 写回答

2条回答 默认 最新

  • zhwchch 2015-04-17 06:35
    关注

    调用代码中
    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类的对象不太好,我个人是不太愿意使用这样的做法。
    Ok,希望对你有些帮助。

    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!