开发者

EXC_BAD_ACCESS due to PostNotification

开发者 https://www.devze.com 2023-01-03 17:35 出处:网络
I am facing one issue regarding one module let me clear the flow for the same. I have one customized UITableviewCell.

I am facing one issue regarding one module let me clear the flow for the same.

I have one customized UITableviewCell.

When I am getting some new information I am posting one notification

[[NSNotificationCenter defaultCenter] postNotificationName:KGotSomething object:nil userInfo:message];

In view where I am maintaining the table I am initiating a customized cell

- (UIT开发者_运维知识库ableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    cell= [[CustomCell alloc] initWithFrame: reuseIdentifier:identifier document:doc];
    return cell;
}

now in customcell.mm

- (id)initWithFrame:(CGRect)frame reuseIdentifier:(NSString *)reuseIdentifier 
{
[[NSNotificationCenter defaultCenter] addObserver:self 
                 selector:@selector(GotSomething:) 
                         name:KGotSomething 
                        object:nil];
}

and in dealloc

- (void)dealloc 
{
    [[NSNotificationCenter defaultCenter] removeObserver:self
                    name:KGotSomething 
                       object:nil];
}

Now my app crashes due to this notification and dealloc is never get called.

Can you guys help me, how to get this working or anything I m doing wrong over here...

Thanks,

Sagar


You initWithFrame:reuseIdentifier: and dealloc methods are incomplete. Is it on purpose ?

initWithFrame:reuseIdentifier: should contains a call to super:

- (id)initWithFrame:(CGRect)frame reuseIdentifier:(NSString *)reuseIdentifier 
{
    self = [super initWithFrame:frame reuseIdentifier:reuseIdentifier];
    if (self) {
        [[NSNotificationCenter defaultCenter] addObserver:self 
                 selector:@selector(GotSomething:) 
                         name:KGotSomething 
                        object:nil];
    }
    return self;
}

and dealloc too:

- (void)dealloc 
{
    [[NSNotificationCenter defaultCenter] removeObserver:self
                    name:KGotSomething 
                       object:nil];
    [super dealloc];
}

Update

The cell is not auto-release after its creation. So the cell is leaking and never gets deallocated. The code should be:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    cell= [[CustomCell alloc] initWithFrame: reuseIdentifier:identifier document:doc];
    return [cell autorelease];
}
0

精彩评论

暂无评论...
验证码 换一张
取 消