1
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

3
  • 10
    Bet you $10 that if you actually run that code verbatim, it won't crash. The error is in the "some code." Commented Mar 3, 2011 at 1:37
  • I think it would be a nice idea to create a simple rule: gor crash - provide the stack trace. Commented Mar 3, 2011 at 1:41
  • It's resolved that I released price in [Car dealloc] which caused the crash Commented Mar 4, 2011 at 0:58

5 Answers 5

7

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];
}
Sign up to request clarification or add additional context in comments.

3 Comments

thank you, It's resolved that I released price in [Car dealloc] which caused the crash
@WangYang you didn't provide a stack trace so it's hard to tell (though that seemed to be the case). Just to add to it, the stack trace from a similar crash I experienced was this: Fatal Exception: NSRangeException *** -[__NSArrayM removeObjectAtIndex:]: index 0 beyond bounds for empty array Maybe that will help someone trying to see if this is the case for them.
Thanks! I wasn't finding it but then I realised I had instantiated a NSArray and not a NSMutableArray.
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.

Comments

1

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..

Comments

0

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?

Comments

0

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

1 Comment

I set it from (nonatomic, copy) to (nonatomic, strong), and it worked.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.