开发者

custom NSView crash when changing the background color

I have a custom NSView object with a retained property named bgColor. I override the setter method by defining setBgColor method:

- (void)setBgColor:(NSColor *)theColor
{
    [bgColor autorelease];
    bgColor = [theColor retain];
    [self setNeedsDisplay:YES];
}

I also have another function called isOnline:

-(void)isOnline:(BOOL)connected{
    if(connected){
        self.bgColor = onlineBackgroundColor;
    } else {
        self.bgColor = offlineBackgroundColor;
    }
}

When I called the isOnline method in initWithFrame method using [self isOnline:NO], it works fine. But when I try to call isOnline method from a controlling object with:

[theCustomedView isOnline:YES]; or theCustomedView.isOnline = YES;

It would crash in the 开发者_JS百科setBgColor method at the line: bgColor = [theColor retain]; The complier complains Program received signal: "EX_BAD_ACCESS". I can't figure out why. Was that autorelease wrong?

If so, how come I can call from the controlling object [theCustomedView setBgColor:aColor]; and from self in the initWithFrame and it would work fine?

Any ideas?


You're trying to send -retain to theColor after it's been destroyed. Check where it's coming from.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜