tags:

views:

51

answers:

1

Hi, 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;
}
+1  A: 

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]];
hanno
please see my Q again, i have used Autorelease in NSMutableArray,i tried to use copy, it gives the same crash
Mikhail Naimy
I can't see an autorelease pool (did you cut out some parts of the code?) and I can't see the copy. Am I missing something here? Maybe you want to have a look at http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmAutoreleasePools.html#//apple_ref/doc/uid/20000047-CJBFBEDI
hanno
copy is not there, but i used for checking...i used NSMutableArray *toReturn = [[[NSMutableArray alloc] initWithCapacity:0] autorelease];
Mikhail Naimy
As far as I can tell the only thing missing is to copy the string that is added to `toReturn`. hanno is right.
bddckr
I see that using copy is the answer, but since toReturn is an array shouldn't it retain automatically?
Rengers
what is the solution pls?
Mikhail Naimy