[英]NSMutableArray Access Issue
I (think I) know what you're thinking... not another EXC_BAD_ACCESS
question but I'm really struggling and it's driving me crazy. 我(我想)知道你在想什么...不是另一个EXC_BAD_ACCESS
问题,但我真的很挣扎,这让我发疯了。 I've searched high and low all over the net and on here and the issue I'm facing seems have something to do with my memory management. 我在网上搜索高低,在这里,我面临的问题似乎与我的内存管理有关。
Problem: 问题:
Basically I have an NSMutableArray
that keeps track of some cyber food. 基本上我有一个NSMutableArray
跟踪一些网络食物。 Since food can be added and taken away at the same time I have a separate array that checks for items to remove and holds the ones that don't need to be taken out. 由于食品可以添加和在同一时间带走我有一个单独的数组,检查的项目删除,并认为不需要取出来的人。 The idea is to then clear the original array ( _food ) and copy back all the items saved in the temporary array ( foodToKeep ). 我们的想法是清除原始数组( _food )并复制保存在临时数组( foodToKeep )中的所有项目。 Here's the code 这是代码
NSMutableArray *foodToKeep = [[NSMutableArray alloc] init];
for (Food *food in _food) {
if(!food.removeable){
[foodToKeep addObject:food];
[food release];
}
}
if(foodToKeep > 0){
[_food removeAllObjects];
for (Food *food in foodToKeep) {
[_food addObject:food]; // EXC_BAD_ACCESS error here
[food release];
}
}
}
[foodToKeep release];
I get the bad access error when add the food to the original array. 将食物添加到原始数组时,我得到了错误的访问错误。 After searching around it sounds like the _food
array is being released somewhere or somehow ending up as nil . 在搜索它之后听起来像_food
数组正在某处释放或以某种方式结束为零 。 The only place I have a release
for _food is in the dealloc
method so I can't understand why this is happening. 我release
_food的唯一地方是dealloc
方法,所以我无法理解为什么会发生这种情况。
I'm fairly new to Objective-C (so go easy on me!) but as far as I can tell there are no leaks and no accidental releases that are causing this, I think what I need is an experts eye to see what is most likely a trivial mistake on my part :-P 我对Objective-C很新(所以对我来说很容易!)但据我所知,没有任何泄漏,也没有意外释放造成这种情况,我认为我需要的是专家眼睛看看是什么我很可能是一个微不足道的错误:-P
Edit: Food is defined and allocated here: 编辑:食物在这里定义和分配:
@interface MainLayer
{
NSMutableArray *_food;
}
In the class init
method 在类init
方法中
_food = [[NSMutableArray alloc] init];
You are overreleasing here: 你在这里过度释放 :
for (Food *food in _food) {
if(!food.removeable){
[foodToKeep addObject:food];
// [food release]; <--- REMOVE THIS
}
}
And here 和这里
for (Food *food in foodToKeep) {
[_food addObject:food]; // EXC_BAD_ACCESS error here
// [food release]; <--- REMOVE THIS
}
Remove the extra releases and you should be fine. 删除额外的版本,你应该没事。
You are releasing Food
objects you don't own. 您正在发布您不拥有的Food
对象。 Have a look at Objective-C Memory Management Rules . 看看Objective-C 内存管理规则 。
In fact, in the line: 事实上,在行:
for (Food *food in _food) {
You don't own the object food, so you should not release it in the first loop, nor in the second loop. 您不拥有对象食物,因此您不应在第一个循环中释放它,也不应在第二个循环中释放它。
在将数据添加到数组后释放food
,但释放不会平衡retain
调用,因此当您到达[_food removeAllObjects]
可能所有Food对象都会在那里被释放,因此当您稍后再次尝试访问时它会崩溃。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.