so I'm working on a little app, and for right now, to get the interface working correctly, I am adding some static objects to an NSMutableArray, but when that finishes, it adds the right number of objects, but they are all the same as the last object added.

Item *tempItem = [[Item alloc] init];
// make a new item
tempItem.category = @"Groceries";
// all the other properties are set
[list addObject:tempItem];
// same code for adding 3 more objects

If I have calls to NSLog before adding objects, it shows that tempObject is what I want it to be. When that is all done, I can see that list has 4 objects in it, but they are all the same as the last object added.

I feel like this is probably very easy to fix, but searching on Google, I couldn't find anything that worked. I tried releasing, and autoreleasing the tempObject after adding to the list and before changing it to the new object without success.

As everyone suspected you are using the same item over and over. At the second point where it says "// make a new item" you have not made a new item at all. tempItem is still pointing at the item you created with alloc/init. You need to alloc/init each and every new item.

Ok guys thanks for the help, I've got it working now. And lostp, the autorelease was just something that was left in from trying things last night. Also, those comments about making a new item just meant changing the attributes, I didn't know I needed to alloc/ init each time!

Ok, now I'm having another issue. On my interface, I have a popup button which I want to fill with Item.name and I have a textview which I want to fill with the category, store and item name for each item. The app itself has a NSMutableArray holding Items called list. I wrote two methods:

Then, after I build the Item objects and put them into list, I call those two methods:

Code:

[self updateAllItemsView:list];
[self updateItemsPopup:list];

My problem is EXC_BAD_ACCESS. From putting in some breakpoints, it seems like by passing list to the first method, list gets deleted? If I only call one of those methods, then it will run fine (although, after checking, list is still being deleted).

By deleting, I mean when I go in the debugger, before either of the method calls, I can see list, after one of the method calls, its gone. I know I need to read the memory management guide, I'm assuming I can find that in the developer site? I have garbage collection turned on, the one cocoa book I followed never went over memory management because it always had you turn on the garbage collection.

Where do you see the leaks? I'm guessing you mean where I tried making a copy of list? If so, I don't have that any more, I was just trying it out.

- You create a new Item, pointed to by tempItem. This is fine.
- You then point tempItem at a different item from the items array. This is bad. The Item you created in the first line has just been leaked!
- Finally, you release the item tempItem points to. Since tempItem points to an Item in the items array, this line is now actually releasing the item in the array!

By deleting, I mean when I go in the debugger, before either of the method calls, I can see list, after one of the method calls, its gone. I know I need to read the memory management guide, I'm assuming I can find that in the developer site? I have garbage collection turned on, the one cocoa book I followed never went over memory management because it always had you turn on the garbage collection.

Where do you see the leaks? I'm guessing you mean where I tried making a copy of list? If so, I don't have that any more, I was just trying it out.

Dan

I still am not sure what you mean by "it's gone" here. When you print with the debugger the pointer is 0/nil? The length is 0? Something else? The variable itself can't disappear.

Also, if you have GC on none of the retain/release stuff should matter. As such, this shouldn't be the problem. With that said, I still think memory management is important to understand.

Well now I feel dumb. Turns out when I said I had garbage collection enabled, it was enabled on an older version of this project. Its on now though. Also, before turning it on, whooleytoo's suggestions did the trick!

As for it being gone, if I had a breakpoint set just before calling updateAllItemsView, in the debugger, I could see the list array. After going through the method, it was gone, but that might be due to the program having the EXC_BAD_ACCESS?