May 27, 2014

Fast food objective-c (part 4) : Basic Memory Management

Basic Memory Management

learnobjectivec-objectwithrefcount
If you're writing an application for Mac OS X, you have the option to enable garbage collection. In general, this means that you don't have to think about memory management until you get to more complex cases. 
However, you may not always be working with an environment that supports garbage collection. In that case, you need to know a few basic concepts. 
If you create an object using the manual alloc style, you need to release the object later. You should not manually release an autoreleased object because your application will crash if you do. 
Here are two examples:
// string1 will be released automatically
NSString* string1 = [NSString
string]; // must release this when done
NSString* string2 = [[NSString
alloc] init];
[string2
release];
For this tutorial, you can assume that an automatic object will go away at the end of the current function. 
There's more to learn about memory management, but it will make more sense after we look at a few more concepts.

No comments:

Post a Comment