NSMutableArray wont let me remove its content

Apprentice
Posts: 7
Joined: 2011.03
Post: #1
Hi all,

I'm new to iphone game programming, and i still in the process of learning Objective-c along the way. I have encounter a road block in my current game development and I'm hoping to get some help from more experience folks.
So here is the problem, I've created a NSMutableArray to collect objects being loaded when the game level is initialized (from a tmx file created using Tiled Map Editor).
i.e.
Code:
gameObjects = [[NSMutableArray alloc]init];
Box *aBox = [[Box alloc]initWithImage];
[gameObjects addObjet: aBox];
[aBox release];

everything works ok and the game runs fine until when the current level is completed and I want to clean up the gameObjects array for the next level.
i.e.
[gameObjects removeAllObjects];

However, the above line is causing my game to crash with a error message:GameEngine(893,0xa0707540) malloc: *** error for object 0x3: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug

I've also tried to use [gameObjects release] and i got the same result.

Could anyone please help me out or point me to the right direction as how to properly remove items from a NSMutableArray?

Thanks in advance for any insight.
Quote this message in a reply
⌘-R in Chief
Posts: 1,237
Joined: 2002.05
Post: #2
You are properly removing them.

But when you remove an item from an array, the array releases its. If that release causes the object's retain count to go to zero, it's going to be deallocated. If you're deallocating something incorrectly, then you're going to get an error of some sort. It's not a problem with removing the object from the array, it's just a problem highlighted by doing so.

The error being described says you're freeing a pointer that's not allocated. You probably just did something silly with a free. As it says, stick a break point in malloc_error_break by adding it in the Breakpoints window. You'll find the source of the problem immediately.
Quote this message in a reply
Apprentice
Posts: 7
Joined: 2011.03
Post: #3
Thanks SethWillits.

I managed to track down the problems. It turned out that those box objects weren't initialized using alloc/init, so there was no need to include an object release statement in the dealloc section in the Box class.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Applescript: Get message content from Mail Yoda 2 4,122 Apr 1, 2006 11:52 AM
Last Post: Yoda
  How to remove this __CFNumber* warning Zaelsius 4 3,872 Jun 4, 2005 05:53 AM
Last Post: Zaelsius