开发者

iphone Development - GDB error signal EXC_BAD_ACCESS

开发者 https://www.devze.com 2022-12-13 14:33 出处:网络
i get the signal error EXC_BAD_ACCESS when trying to retrieve the return output from the randomBallPick method, i p开发者_高级运维robably do it wrong.

i get the signal error EXC_BAD_ACCESS when trying to retrieve the return output from the randomBallPick method, i p开发者_高级运维robably do it wrong.

NSString *temp = [self randomBallPick];
upBall1.image = [UIImage imageNamed:temp];


Array (containers) retain/release items that are added/removed.

The object will receive release when it's removed from container with removeObjectAtIndex: so you need to retain it before it is removed and possibly autorelease since you are returning it from your method.

NSString * chosenFilename =
          [[[imageArray objectAtIndex:chosen] retain] autorelease];
[imageArray removeObjectAtIndex:chosen];
return chosenFilename;


OK, can you try this, please?

NSString *chosenFilename = [[imageArray objectAtIndex:chosen] retain];
[imageArray removeObjectAtIndex:chosen];
return [chosenFilename autorelease];


All you need to do is:

NSString *chosenFilename = [[imageArray objectAtIndex:chosen] retain];

Since the objectAtIndex method returns an autorelease object.


There are several things wrong with this piece of code.

You are initializing your array of names once, but then you keep removing stuff from it... I'm not sure you want to do this, otherwise you'll start returning nil exclusively (after the 38th call...). You may want to re-fill your array in that case. Here's a better version of your routine (I think):

static NSMutableArray *imageArray = nil;

if (!imageArray.count) {
    if (imageArray==nil) imageArray = [[NSMutableArray alloc] init];
    for (int c = 0; c < 37; c++)
    {
        NSString *imageName = [NSString stringWithFormat:@"ball_%i.png", c];
        [imageArray addObject:imageName];
    }
}
// Now we are basically sure that imageArray.count > 0
assert(imageArray.count>0);
NSUInteger chosen = arc4random() % imageArray.count;
NSString *chosenFilename = [[imageArray objectAtIndex:chosen] retain];
[imageArray removeObjectAtIndex:chosen];
return [chosenFilename autorelease];

As others have said, you have to retain then autorelease the strings you extract from the array (because the array releases them upon removal).

Note also that you should call 'retain' on the string before removing it from the array. The string is already released after the removeObjectAtIndex: call... so it's already too late to retain it then.


As soon as you remove the object from the array, its retain count is probably zero, and it will get dealloced. Try doing

return [[chosenFilename] retain] autorelease]
0

精彩评论

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