11
Submitting Assignments Please delete your build directory before submitting. It makes a 2MB file closer to 100KB or less. Programs with errors or warnings will be sent back without comments unless you explicitly let me know you are having issues you can not fix. SEC - 9/15/10 COMP 446 / ELEC Week 411

13
Memory Management Basics Object Creation + alloc and - init methods Objects take space, space allocated with alloc and initialized with init. Object retention The objects maintain a retention count. [object retain] increases that count by 1 [object release] decrease that count by 1 When count reaches 0, [object dealloc] automatically called. Objects retained by other objects need to be released when finished Either when explicitly finished or in the dealloc method Objects which might get released to 0 by others need to be retained SEC - 9/15/10 COMP 446 / ELEC Week 413

14
Pointers vs. Objects Objects exist in the heap and take up resources Variable names, for example (NSString *) point at objects. Their value is a pointer to the real object. Names can be set to nil (0) when they point to nothing SEC - 9/15/10 COMP 446 / ELEC Week 414

15
Pointers vs. Objects Copying objects, particularly NSString guarantees they won’t be changed from under you You may cast them as an NSString, but it might be an NSMutableString Multiple names can point to the same object (NSMutableString *)mstring1 = [NSMutableString (NSString *)string2 = mstring1; // string2 == mstring1; both point to same object SEC - 9/15/10 COMP 446 / ELEC Week 415

16
Instance Variables and Properties Memory Instance variables take up some space int, float, etc store the actual values (NSString *) iVar saves room for a pointer to an NSString object The space needed by the actual object goes away when the object is deallocated, not when the pointer goes away. Variables initialized to 0 or nil. This includes pointer objects. Instance variables assigned to objects need to be released during object deallocation OK to message nil objects, not OK to release deallocated objects SEC - 9/15/10 COMP 446 / ELEC Week 416

19
Autorelease If you know you are finished with an object and you retained or own it, release it when finished. It will go away if you are the last reference. If you no longer need the object yourself, but want to pass it to someone else, autorelease the object; let them be responsible for retaining it. If you know you will be done with the object during this run loop but can’t tell where in your code you may finish with it, autorelease the object. If you acquire an autoreleased object and believe you will need it on a different run-loop cycle, retain the object and later release it. SEC - 9/15/10 COMP 446 / ELEC Week 419

20
Initial Retention Count Objects received through [class alloc] or [class copy] come with a retention count of 1 By convention, all other objects are owned by others and likely come autoreleased They will be automatically destroyed on next wait loop unless otherwise retained SEC - 9/15/10 COMP 446 / ELEC Week 420