NSMutableArray返回时发生崩溃? [英] crash happens when NSMutableArray is returned?

查看:51
本文介绍了NSMutableArray返回时发生崩溃?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经编写了这样的代码(该函数将被反复调用),但是返回的对象给出了"BAD ACCESS",NSLog打印出正确的字符串,但是有时会返回(我称之为 一次又一次地)崩溃..任何帮助更改此代码,如果我删除自动释放"方法,它会很好

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 ([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"]];

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

这篇关于NSMutableArray返回时发生崩溃?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆