问题可能很简单,但我没有认真思考。
我有以下 iPhone 代码
for(int x = 0 ; x < [allFriends count] ; x++)
{
Friend *f = [[Friend alloc] init];
f = [allFriends objectAtIndex:x];
if([uid isEqualToString:[f uid]])
{
[f AddAlbum:album];
[allFriends replaceObjectAtIndex:x withObject:f];
}
}
无论我在哪里调用[frelease],应用程序总是崩溃。为什么? (顺便说一句,循环运行了几千次)
有没有更有效的方法?
我认为我提供了足够的代码,如果没有,请告诉我!
非常感谢您的帮助!
最佳答案
您在这一行中创建的对象(并且可能正在尝试释放):
Friend *f = [[Friend alloc] init];
当您将 f
分配给从数组中获取的对象时,立即泄漏:
f = [allFriends objectAtIndex:x];
所以它确实是您要释放的对象,这意味着数组中的指针不再有效(它指向已释放的实例)。
关于iphone - 指针、NSMutableArray、保留、循环和困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2267749/