开发者

crash happens when NSMutableArray is returned?

开发者 https://www.devze.com 2022-12-22 21:09 出处:网络
I have coded like that(that function will be called again and again), but the returned object gives \"BAD ACCESS\", the NSLog prints correct string, but toReturn sometimes(i called

I have coded like that(that function will be called again and again), but the returned object gives "BAD ACCESS", the NSLog prints correct string, but toReturn sometimes(i called again and again) gives crashes..any help to alter this code,If i remove the "autorelease" method,it worsks fine

 - (NSMutableArray *)getAll:(NSString *)type
{
    NSLog(@"Type: %@", type);
    NSMutableArray *toReturn = [[[NSMutableArray alloc] initWithCapacity:0] autorelease];

    rs = [db executeQuery:Query1];

    while 开发者_如何学C([rs next]) {
        [toReturn addObject:[rs stringForColumn:@"Name"]];
        NSLog(@"name: %@", [rs stringForColumn:@"Name"]);
    }

    [rs close];

    return toReturn;
}


You need to make sure that your string is not deallocated in the meantime. Try changing

    [toReturn addObject:[rs stringForColumn:@"Name"]];

to

    [toReturn addObject:[[rs stringForColumn:@"Name"] copy]];
0

精彩评论

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

关注公众号