iphone开发-GDB错误信号EXC_BAD_ACCESS
当尝试从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
数组(容器)
保留
/释放
添加/删除的项目。当使用
removeObjectAtIndex:
从容器中删除该对象时,该对象将收到release
,因此您需要在删除之前retain
它 可能还有autorelease
因为您是从方法中返回它的。Array (containers)
retain
/release
items that are added/removed.The object will receive
release
when it's removed from container withremoveObjectAtIndex:
so you need toretain
it before it is removed and possiblyautorelease
since you are returning it from your method.好的,你能试试这个吗?
OK, can you try this, please?
您需要做的就是:
由于 objectAtIndex 方法返回一个自动释放对象。
All you need to do is:
Since the objectAtIndex method returns an autorelease object.
这段代码有几个问题。
您正在初始化名称数组一次,但随后不断从中删除内容...我不确定您是否想要这样做,否则您将开始专门返回 nil (在第 38 次调用之后...)。在这种情况下,您可能需要重新填充数组。这是您的例程的更好版本(我认为):
正如其他人所说,您必须保留然后自动释放从数组中提取的字符串(因为数组在删除时释放它们)。
另请注意,您应该在将字符串从数组中删除之前对字符串调用“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):
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