3 ms·
Regarding the learn Objective C part, can anyone reccomend some good tutorials on memory management? I still need some rules to follow regarding what to releas
by tocomment 18y ago
Regarding the learn Objective C part, can anyone reccomend some good tutorials on memory management? I still need some rules to follow regarding what to release, what not to, how to release it, and when. Is there a simple model I could keep in my head while programming so it all makes sense to me?
- yan 18y agoThis document helped me out a bit: http://developer.apple.com/documentation/Cocoa/Conceptual/MemoryMgmt/MemoryMgmt.html http://developer.apple.com/documentation/Cocoa/Conceptual/Me... Also, this has a few rules you can follow: http://www.stepwise.com/Articles/Technical/2001-03-11.01.html http://www.stepwise.com/Articles/Technical/2001-03-11.01.htm... Just remember that adding elements to containers bumps their retain counts. So the following is a common pattern for me: MyClass *c = [[MyClass alloc] init]; [arr addObject:c]; [c release]; If you're returning an object from a method, you probably want to autorelease it.
- hboon 18y agoA. It uses reference counting. -retain (+1 now), -release (-1 now), -autorelease (-1 later). B. Creation/allocation is +1, thus: +alloc (+1), -copy (+1). C. Your goal is to keep the count balanced at 0. If you do a +1, make sure you do a -1 either now (-release) or later (-autorelease). (C) is the guiding principle, thus in this order: 1. If you +alloc/-copy something in a function and is done with it, make sure you have a corresponding -release. 2. If you +alloc/-copy something in a function and return it, -autorelease before you return, so that counts are 0 (in the end anyway). 3. If you didn't +alloc/-copy an object, (1) and (2) don't apply. E.g. NSMutableString's +string. 4. If you -retain, make sure you -release or -autorelease to pair it up. (remember balanced to 0). 5. If you get an object from calling another function, you shouldn't -release/-autorelease it because someone else or you should have done (2). But don't forget (4). The rest is about memory management in accessors and IB outlets. PS: I give up on formatting this. Hope it's readable.
- wallflower 18y agoFellow aspiring dev sent me this link on Monday: http://www.mobileorchard.com/iphone-memory-management/ http://www.mobileorchard.com/iphone-memory-management/