Last active
December 14, 2015 18:49
-
-
Save romyilano/5132343 to your computer and use it in GitHub Desktop.
Memory Management Notes from Erica Sadun
She kicks ass!!! =D
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Manual Reference counting (MRR) - just like Maximum Rock'n'Roll! | |
| /* | |
| With MRR you have to ensure that every object you allocate | |
| Moves from the start to the finish of its life cycle without being prematurely | |
| released and to guarantee that it does get finally released when it is time to do so | |
| Complicating matters is objective-C's MRR autorelease pool. | |
| If some objects are autoreleased and others must be released manaually how do you best control your objects? | |
| */ | |
| // create objects | |
| id myObject = [[SomeClass alloc] init]; // now has count of +1 | |
| // locally scoped variable: if I don't release this object before the | |
| // end of this method, the object leaks. Reference to that memory disappears | |
| // but the memory itself remains allocated | |
| // retain count remains at +1 | |
| -(void)leakyMRRMethod | |
| { | |
| // this is leaky in MRR | |
| NSArray *array = [[NSArray alloc] init]; | |
| } | |
| -(void)properMRRMethod | |
| { | |
| NSArray *array = [[NSArray alloc] init]; | |
| // use the array here | |
| [array release]; | |
| } | |
| // autoreleasing doesn't require an explicit release statement for locally scoped variables | |
| // in fact avoid doing so | |
| -(void)anotherProperMRRMethod | |
| { | |
| NSArray *array = [[[NSArray alloc] init] autorelease]; | |
| // this won't crash the way release would | |
| printf("Retain count is %d\n", [array retainCount]); // don't use retainCount in real code! | |
| // use the array here | |
| } | |
| // Object-creation class methods (both MRR + ARC) return an autoreleased object | |
| // +(NSArray *)array returns a newly initialized array already set for autorelease | |
| -(void)yetAnotherProperMethod | |
| { | |
| // +(NSArray *)array | |
| NSArray *array = [NSArray array]; // this is an autoreleased object | |
| // use the array here | |
| } | |
| // at the end of this method the autoreleased array can return to the general memory pool | |
| // whenever you ask another method to create an object it's good programming practice | |
| // to return that object autoreleased. | |
| -(Car *)fetchACarInMRR | |
| { | |
| Car *myCar = [[Car alloc] init]; | |
| return [myCar autorelease]; | |
| } | |
| // this holds esp true for class method | |
| // by convention all class methods that create new objects return autoreleasing objects | |
| // "conveniecne methods" | |
| // this is not autoreleased in Manual Reference | |
| Car *car1 = [[Car alloc] init]; | |
| // this is autoreleased in MRR | |
| Car *car2 = [[[Car alloc] init] autorelease]; | |
| // this -should- be convenitionally an autoreleased object in MRR | |
| Car *car3 = [Car car]; | |
| // to create a convenience method at the class level ensure to define with + and return the object after sending autorelease to it | |
| +(Car *) car | |
| { | |
| // MRR requires an explicit autorelease call | |
| return [[[Car alloc] init] autorelease]; | |
| } | |
| // RETAINING AUTORELEASING OBJECTS | |
| -(void)anotherLeakyMRRMethod | |
| { | |
| // after returning you lose the olcao reference to | |
| // array and cannot release | |
| NSArray *array = [NSArray array]; // has count of +1 | |
| [array retain]; // now count is +2 | |
| // whent he method ends and the autorelease pool drains | |
| // the object receives a single release message and count returns to +1 | |
| // then it will be stuck. | |
| } | |
| // object stuck with a +1 count and has no reference left to point to the objct | |
| // can't be sent the final release message it needs to finisht he life cycle. | |
| // that's why it's critical to build references to retained objects. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment