将自动释放的对象添加到 NSMutableArray
以下是一些由于我正在执行的内存管理而引发异常的代码:
我的头文件:
@property(nonatomic, retain) NSMutableArray *holderArray;
我的实现文件:
@synthesize holderArray
-(void) viewDidLoad{
holderArray = [[NSMutableArray alloc] init];
[self addSampleObject];
}
-(void) addSampleObject{
[holderArray addObject:[self createSampleObject]];
}
-(ModelObject *) createSampleObject{
ModelObject *mObj = [[ModelObject alloc] init];
// Set a few properties
return [mObj autorelease];
}
在这种情况下应该发生什么? createSampleObject
在某个时刻自动释放该对象,而 [holderArray addObject]
将该对象的引用计数增加 1。据我了解,这一切都应该很好。
但是当 getter 访问该元素时,我收到以下异常。如果我在创建模型对象时删除自动释放 stmt,它就会消失 -
#0 0x012525a8 in objc_exception_throw ()
#1 0x010f36e5 in -[__NSArrayM objectAtIndex:] ()
我是否遗漏了有关自动释放工作原理的一些基本知识?
谢谢,
特贾。
编辑:实际上你是对的,我的代码中有一段完全不相关的代码引发了这个错误。我认为我与工作版本相比唯一改变的是添加 autorelease
语句。
谢谢!
Here's some code that throws an exception because of some of the memory management I'm doing:
My Header file:
@property(nonatomic, retain) NSMutableArray *holderArray;
My Implementation file:
@synthesize holderArray
-(void) viewDidLoad{
holderArray = [[NSMutableArray alloc] init];
[self addSampleObject];
}
-(void) addSampleObject{
[holderArray addObject:[self createSampleObject]];
}
-(ModelObject *) createSampleObject{
ModelObject *mObj = [[ModelObject alloc] init];
// Set a few properties
return [mObj autorelease];
}
What should be happening in this context? createSampleObject
autoreleases the object at some point, while [holderArray addObject]
increments the reference count for that object by 1. From what I understand, it should all be good.
But when a getter accesses that element, I'm getting the following exception. It disappears if I remove the autorelease stmt when creating the model object -
#0 0x012525a8 in objc_exception_throw ()
#1 0x010f36e5 in -[__NSArrayM objectAtIndex:] ()
Am I missing something fundamental about how autorelease works?
Thanks,
Teja.
EDIT: Actually you're right, there's a completely unrelated piece of my code that was throwing this error. I thought the only thing that I've changed from a working one is adding the autorelease
statement.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为你的错误可能是在 getter 中 - 这看起来更像是索引越界问题而不是内存管理问题。
您对内存管理的理解似乎是正确的,我唯一要注意的是,在方法中使用“创建”一词表明返回的对象不是自动释放的。不确定您的实际方法名称是什么样的,我知道这只是示例代码。
I think your error might be in the getter - that looks more like an index out of bounds issue than a memory management one.
Your understanding of memory management seems to be correct, the only thing I would note is that the use of the word "create" in a method suggests that the returned object is not autoreleased. Not sure what your actual method names are like, I know this is just sample code.
自动释放的对象保证在自动释放的函数结束之前保持可访问状态。考虑以下代码:
您可能会得到相同的结果:
我没有足够的代码来告诉您到底出了什么问题,但我建议跟踪堆栈跟踪,直到您看到您的代码 你写的。调试器可能会突出显示存在确切问题的行。
Autoreleased objects are guaranteed to remain accessible until the end of the function in which they are autoreleased. Consider the following code:
You could have done this with the same results:
I don't have enough of your code to tell you exactly what is wrong, but I recommend following the stack trace up until you see your code that you wrote. The debugger may highlight the line that is the exact problem.