NSMutableArray removeAllObjects crash

北城以北 提交于 2019-12-11 07:00:57

问题


Car class
--------------
price
color

crash code is:

NSMutableArray *list = [[NSMutableArray alloc] init];
Car *car = [[Car alloc] init];
car.price = 10;
car.color = 1;
[list addObject:car];

// some code

[list removeAllObjects]; // Crash here

why crash, how can i resolve it.

app exit with nothing output


回答1:


I dont know what you have in the "someCode" section in your segment. You first comment out that code and check if the app crashes. If still it crashes then only consider what I have given below. I mean you make sure there is nothing wrong with your code before going for workarounds :)

just try this code, and see if it crashes now.I know it doesn't make sense, but it happened to me once too. Once when array count was zero removeAllObjects crashed for me. I doubt an SDK bug somewhere there :(

if([list count]){
     [list removeAllObjects];
}



回答2:


Most likely you are releasing one or more of the objects in the array one too many times. When the NSMutableArray tries to release that object, it crashes because the object has already been disposed of.




回答3:


I just ran into this same thing. In the dealloc method of my object I had:

-(void) dealloc
{   
   [super dealloc];// <--- le' culprit!
   [Image_ID release];
   [Image_Number release];
   [Image_UID release];
   [Series_ID release];
   [Series_UID release];
   [Study_UID release];  
                       // <--- it should be here...
}

I moved [super dealloc] below all the releases..and my [.... removeAllObjects] worked fine..




回答4:


At a guess, I'd say you're releasing list somewhere, and so your call to removeAllObjects is being sent to a deallocated instance. Impossible to be sure without a stack trace and some more detail, though—is it an EXC_BAD_ACCESS error or what?




回答5:


Set the array property as Retain in interface section. Once I did same it worked for me. just try once.



来源:https://stackoverflow.com/questions/5175573/nsmutablearray-removeallobjects-crash

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!