3

I have a couple of arrays that I am trying to clear all objects from, but using removeAllObjects crashes the app and returns sigabrt. During my research I've found that although I am creating NSMutableArrays I could be creating an instance of a NSArray, but I am not sure if I am doing this or not... Here is everything I do to the arrays

ballArray = [[NSMutableArray alloc] init];

ballVelocityArray = [[NSMutableArray alloc] init];

[ballArray addObject:MyUIImageView];

[ballVelocityArray addObject:[NSValue valueWithCGPoint:myCGPoint]];

[ballVelocityArray replaceObjectAtIndex:SomeIndex withObject:[NSValue valueWithCGPoint:NewVelocity]];

[ballArray removeAllObjects];

[ballVelocityArray removeAllObjects];

That is everything I have done and I can't figure out why it keeps crashing... if there is only one object in the arrays it works fine, otherwise it crashes

Any help would be greatly appreciated!!

12
  • Which of the two arrays causes a crash? Commented Oct 21, 2011 at 7:13
  • Which array is it crashing on? It looks like there is no relationship between the two arrays, so the code for the array that isn't crashing could be removed. Commented Oct 21, 2011 at 7:14
  • I believe both, I will try again, but i believe I commented one at a time out and it crashed both times Commented Oct 21, 2011 at 7:18
  • 2
    You should also go over your variable naming - MyUIImageView and SomeIndex look like class names, and you mix it with the right naming myCGPoint ... but that's of course not causing a crash. Commented Oct 21, 2011 at 7:19
  • what does it say when it crashes? Something like referencing a released object or so? Commented Oct 21, 2011 at 7:20

2 Answers 2

1

It's most likely because you are not managing memory correctly on one of the objects the array contains. When you remove an object from an array its retain count is decremented once.

You can put a break point on the line where you clear the array and use the debugger to see which object in there is invalid.

Sign up to request clarification or add additional context in comments.

Comments

0
ballArray = [[NSMutableArray alloc] init];

ballVelocityArray = [[NSMutableArray alloc] init];

After allocating object you are also releasing the object in dealloc function or somewhere else check this.If you are doing so then I would like to inform you that

[ballArray removeAllObjects];

[ballVelocityArray removeAllObjects];

removeAllObjects is not only removing all objects of array but also release the array object from memory so if again you are releasing the array object the memory pointer will reach on -1 and the application will crash.

So, make sure that You have not to release array object it you are already using

[ballArray removeAllObjects];

[ballVelocityArray removeAllObjects];

functions.

Comments

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.