I’m looking at someone’s code and I know the general rule is if you have alloc/init, you need to release that memory. He uses a lot of NSMutableArrays, alloc/inits them, but does not release them. Can I simply send the autorelease message to the array that gets created if I do not see any other release/autorelease message getting sent to that array? I basically don’t want to get his code to crash and stop working either :P.
With NSMutableArrays, when you send the message addObject and the object in that array increases its retain account, if that array gets released, but the object never gets sent a release or removeObject from the array, is that also a memory leak? Thanks.
You need to either
-releaseor-autoreleaseanything you-retain,+alloc,-copy,+allocWithZone:or-copyWithZone:. (And, if you retain something twice you also need to release it twice.)When an
NSMutableArray(orNSArray,NSSet, orNSDictionaryand mutable subclasses) object is dealloc’d (retain count reaches zero), it releases anything it contains. When you add an object to anNSMutableArray, the array retains the object (it does not copy it like some people claim).I highly recommend the Memory Management Programming Guide to both you and the someone you referred to in the question.
I hope this answer helps you and someone. Good luck. 🙂
Also, enable the Clang Static Analyser in the build settings. This will tell you at compile time when a leak is going to happen (and much, much more). In fact, it’s the first thing I always do when I start a new project. The analyzer never lied to me.