NSMutableArray addObject冻结我的应用程序

时间:2011-06-03 08:34:05

标签: iphone debugging

很多类似的问题,但他们的答案似乎都没有回答我......

我正在创建我的数组:

imgArray = [NSMutableArray arrayWithCapacity:10];

稍后(在另一个函数中)我试图向它添加一个对象:

Attachment newAttachment = [[[Attachment alloc] init] autorelease];
newAttachment.fileName = filename;
newAttachment.file = file;
[imgArray addObject:newAttachment];

这导致iPhone应用程序冻结。模拟器似乎很好;状态栏上的时钟一直在滴答作响,我没有收到任何错误消息,但我的应用程序不再响应。

我做错了什么?

2 个答案:

答案 0 :(得分:2)

您似乎没有保留 imgArray 。你是?尝试,

imgArray = [[NSMutableArray alloc] initWithCapacity:10]

如果没有。

答案 1 :(得分:1)

只是做

imgArray = [[NSMutableArray arrayWithCapacity:10] retain];

所有类方法如果返回一个对象,并且返回一个对象已经存在于自动释放池中,那么如果想要在当前工作块之外使用该对象,则应该始终保留该对象,因为这样在块之外会丢失引用。

相关问题