Whilst I don’t have no ANALYSE issues with my iPhone code, when I run Instruments looking for Memory Leaks I seem to see a lot. One is basically as custom class I use, say MyCustomClass. Instruments basically says:
Instruments Summary from Leaked Blocks – History
- Malloc – when I create it
- Autorelease – which I do on the same like as the alloc
- Retain – seems to be where I add this to an NSMutableArray somewhere else
- Release – but from GrahicServices – GSEventRunModal (this is not me…)
I had already auto-released the object, so I wonder if there an issue in the way I added it to an NSMutableArray via addObject
QUESTIONS – My overall question is whether is, when doing an “addObject” to an NSMutableArray, for an object which is auto-released, are there any specific steps the code that receives the array needs to perform when finishing up? Is it the case perhaps that even through my object is autoreleased, adding it to an array does a retain, so perhaps when I’m finishing up with the NSMutableArray I need to manually iterate through all the objects in the array and release them?
thanks
If you’ve invoked
autoreleaseon all of the objects that you’ve added to your array, there’s no additional work for you to do in terms of memory management. TheNSMutableArraywill manage the in-memory state of the objects that are added to it (as in retaining them when added and releasing them when the object is removed from the array, either by explicitly releasing it or when the array is released). All you need to do is make sure you release the array when you’re done with it.