iphone开发-GDB错误信号EXC_BAD_ACCESS

发布于 2024-08-13 16:03:53 字数 190 浏览 3 评论 0原文

当尝试从 randomBallPick 方法检索返回输出时,我收到信号错误 EXC_BAD_ACCESS ,我可能做错了。

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

i get the signal error EXC_BAD_ACCESS when trying to retrieve the return output from the randomBallPick method, i probably do it wrong.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

寄居人 2024-08-20 16:03:53

数组(容器)保留/释放添加/删除的项目。

当使用 removeObjectAtIndex: 从容器中删除该对象时,该对象将收到 release,因此您需要在删除之前retain 可能还有 autorelease 因为您是从方法中返回它的。

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

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;
等风来 2024-08-20 16:03:53

好的,你能试试这个吗?

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

OK, can you try this, please?

NSString *chosenFilename = [[imageArray objectAtIndex:chosen] retain];
[imageArray removeObjectAtIndex:chosen];
return [chosenFilename autorelease];
无悔心 2024-08-20 16:03:53

您需要做的就是:

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

由于 objectAtIndex 方法返回一个自动释放对象。

All you need to do is:

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

Since the objectAtIndex method returns an autorelease object.

九八野马 2024-08-20 16:03:53

这段代码有几个问题。

您正在初始化名称数组一次,但随后不断从中删除内容...我不确定您是否想要这样做,否则您将开始专门返回​​ nil (在第 38 次调用之后...)。在这种情况下,您可能需要重新填充数组。这是您的例程的更好版本(我认为):

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];

正如其他人所说,您必须保留然后自动释放从数组中提取的字符串(因为数组在删除时释放它们)。

另请注意,您应该在将字符串从数组中删除之前对字符串调用“retain”。该字符串在 removeObjectAtIndex: 调用后已被释放...因此此时保留它已经为时已晚。

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.

风追烟花雨 2024-08-20 16:03:53

一旦从数组中删除该对象,它的保留计数可能为零,并且它将被释放。尝试做

return [[chosenFilename] retain] autorelease]

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]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文