Created
March 13, 2014 19:54
-
-
Save Brayden/9535666 to your computer and use it in GitHub Desktop.
P2P Detecting & Fixing Memory Issues (Part 1)
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
@interface ViewController () | |
/** | |
atomic | |
------ | |
- is default behavior | |
- only one thread accesses the variable at a time | |
- is thread safe (read/write safe) | |
- slower in performance (than nonatomic) | |
nonatomic | |
--------- | |
- multiple threads access the variable | |
- is thread unsafe | |
- faster in performance (than atomic) | |
- it may run into unexpected behavior if multiple threads access it simultaneously | |
strong | |
------ | |
- is default behavior | |
- iOS 4 < use to call this "retain" | |
- increases the retain count by one automatically | |
- generally used for parent objects (i.e. UIViewController) | |
weak | |
---- | |
- does not retain object | |
- keeps reference as long as another object has a strong reference to it | |
- generally used for child objects (i.e. IBOutlet views) and delegates (to prevent retain cycles) | |
- when strong reference is deallocated, weak reference is automatically set to nil | |
- use weak to avoid retain cycles | |
unsafe_unretained | |
----------------- | |
- iOS 5 > use "weak" | |
- if your app targets iOS 4 and lower you should use this in place of "weak" | |
- "weak" objects automatically go to nil when they lose their strong reference BUT | |
- "unsafe_unretained" objects do not get set to nil automatically when they lose their strong reference | |
assign | |
------ | |
- performs a variable assignment | |
- good to use with C primitive objects (use weak for Objective-C objects) | |
- examples include: delegates (to prevent retain cycles), int, BOOL, float, char, double, struct, and enum | |
- for delegates if you can use weak instead of assign, then use weak (iOS 5 >) | |
copy | |
---- | |
- specifies the new value sent to it should be sent "copy" and old value sent "release" | |
- acts like "retain", in non-garbage collection environments where you must release it | |
- used when you need an objects value as it is at that specific moment | |
- is required when the object is mutable (to strip mutability and by getting value at that specific moment) | |
readonly | |
-------- | |
- object will not create a setter property automatically | |
- indicates to others that the property should be read only (not written to) | |
- trying to assign/set a value to property generates a compiler warning | |
readwrite | |
--------- | |
- is default behavior | |
- setter and getter are automatically generated | |
- property should be treated as read/write | |
*/ | |
// Below two are identical | |
@property (nonatomic, strong) UIViewController *aViewController; | |
@property (nonatomic) UIViewController *bViewController; | |
// Weak because IBOutlet and its VC has a strong reference | |
@property (nonatomic, weak) IBOutlet UIView *aView; | |
// Below two are identical | |
@property (atomic, copy) NSString *aString; | |
@property (copy) NSString *bString; | |
// Strong because it is mutable | |
@property (nonatomic, strong) NSMutableString *cString; | |
// An example of an (atomic, strong) string | |
@property NSString *string; | |
@end | |
@implementation ViewController | |
- (void)viewDidLoad | |
{ | |
[super viewDidLoad]; | |
// Do any additional setup after loading the view, typically from a nib. | |
[self deadStore]; | |
} | |
- (void)didReceiveMemoryWarning | |
{ | |
[super didReceiveMemoryWarning]; | |
// Dispose of any resources that can be recreated. | |
} | |
#pragma mark - Examples | |
- (void)retainCycle { | |
/** | |
A retain cycle happens when object A has a strong reference | |
to object B, and object B also has a strong reference to | |
object A. | |
To avoid retain cycles: | |
- an object must never retain its parent | |
- no hierarchical parent can be retained | |
- connection objects should not retain their targets | |
- temporary retains _must be_ temporary | |
* for any reason if you have to break the above | |
rules then you should immediately unbreak them | |
*/ | |
} | |
- (void)deadStore { | |
// NSDictionary *dictionary = [NSDictionary dictionary]; // Do NOT do this, see next line for proper way | |
NSDictionary *dictionary = nil; // Creates an empty dictionary | |
dictionary = @{@"key" : @"value"}; // Sets new values to our dictionary | |
NSLog(@"Dictionary: %@", dictionary); | |
} | |
- (void)zombies { | |
/** | |
Zombies often occur with "unsafe_unretained" objects because unlike | |
weak they do not zero (nil) out their value when the strong reference | |
has been lost - it keeps the old value. A zombie is present when you | |
send a message to an already deallocated object. | |
sigabrt and EXEC_BAD_ACCESS | |
*/ | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment