thermogl / TITokenField

An iOS version of the NSTokenField (See To: field in Mail and Messages).
http://thermoglobalnuclearwar.com/opensource/
599 stars 172 forks source link

crash when shouldAdd=NO #28

Closed JanC closed 12 years ago

JanC commented 12 years ago

Hi, I think there is a bug in the TITokenField#addToken method when the delegate discards the adding of a token.

- (TIToken *)addTokenWithTitle:(NSString *)title {
    if (title.length){
        TIToken * token = [[TIToken alloc] initWithTitle:title representedObject:nil font:self.font];
        [self addToken:token];
        [token release];
        return token;
    }
    return nil;
}

If shouldAdd says NO, [self addToken:token] will not retain the token so we end up with a TiToken with 1 retain count and we release it. The it returns a released object which crashes.

This method should return an autoreleased object:

- (TIToken *)addTokenWithTitle:(NSString *)title {
    if (title.length){
        TIToken * token = [[TIToken alloc] initWithTitle:title representedObject:nil font:self.font];
        [self addToken:token];
        return [token autorelease];
    }
    return nil;
}

cheers ;)