Home » IOS » IOS can easily cause references to scenarios

IOS can easily cause references to scenarios

Scene 1: NStimer

timer is an object that can execute the method we specify at a certain point in the future, or periodically, from now on,

NSTimer implementation of the necessary conditions: corresponding to the thread of the RunLoop to open, mode to correspond to

Here's the circular reference of timer:

- (void) setBlock: (TestBlock) block{
_block = [block copy];
- - (id) init
if (self = [super, init]) {
NSLog (@, do, ing, );
self.myTimer = [[NSTimer scheduledTimerWithTimeInterval:1
Selector:@selector (handleTimer:)
Repeats:YES] autorelease];
return self;
- - (void) handleTimer: (id) sender {
NSLog (@ "say: % @ testTimer!", [self class]);
self.testCountView.layer.borderColor = [[UIColor, whiteColor], CGColor];
- - (void) clearTimer {
[_myTimer invalidate];
_myTimer = nil;
- - (void) dealloc
// [self clearTimer];
NSLog (@,%s, is, dealloced, , __FUNCTION__);
[super dealloc];

In figure

, we write a class that, when we instantiate this class will have a timer open, and then when we go to the release of the current class, not the dealloc function, because the timer on the current count +1 timer self self, and then hold, hold timer caused the death cycle the memory leak and break the circular reference is Invalidate.

Scene two: [self performSelector:

when the method has not been executed, to return to the parent view to release the current view, the self count has not been reduced to 0, resulting in a failure to call the dealloc method and a memory leak


1.[NSObject cancelPreviousPerformRequestsWithTarget:self]

2.[NSObjectcancelPreviousPerformRequestsWithTarget:self selector:@selector (method1:) object:nil]

Scene three: addObserver


1. after addObserver in NSNotificationCenter, is there a reference count for this object plus 1 operations?

2. what happens in a release of the NSObject class and a ViewController class,
3., [[NSNotificationCenter, defaultCenter], removeObserver:, name:, object:nil];


How does [[NSNotificationCenter defaultCenter] removeObserver:] select


we first solve second problems, in an already released NSObject and a ViewController NSObject crash issued a notice, will prompt a wrong address, notice that only remember his address pointer, technology is not 1, and the ViewController issued a notice will not happen crash, why
That is: ViewController will call [[NSNotificationCenter defaultCenter] removeObserver:self] at dealloc;

But how are we going to verify it, that is to add a NSNotificationCenter class, override the removeObserver:self method, it will be easy to find, you will also find notice will be removed at the time of dealloc system, apple is actually doing so.

The last question: how to make a choice? Is in addition to outside Dealloc do not call the removeObserver:self method instead of using a single [[NSNotificationCenter defaultCenter] removeObserver: name: object:nil] to remove

Scene four: Try has risks and Catch needs to be cautious


// may throw an exception code
@catch (NSException, *exception) {
// handles exception
The //finally block is optional
//, but if you write finallyblock, no exceptions, the code in block will be executed

and then we can play it:

 @try {
// may throw an exception code
TestTryctch*object = [[TestTryctch, alloc], init];
[object doSmMayThrowException]; / / abnormal function of
[object release];
@catch (NSException, *exception) {
// handles exception
NSLog (@, throw, an, exception:%@, , exception.reason);
@finally {
NSLog (@, finally, execution, );

is there any exception to the above code?
Of course, [object!, doSmMayThrowException]; / / abnormal function will directly skip the [object release],

creates a memory leak;
Scene five: dead loop

CATransition*transition = [CATransition animation];

Transition.duration= 0.5;

Tansition.repeatCount= HUGE_VALL;

"[self.view.layeraddAnimation:transition forKey:", "myAnimation"]

If HUGE_VALL is a big number, it will leak memory solution:

- - (void) viewWillDisappear: (BOOL) animated{

[self.view.layer removeAllAnimations];


copyright notice: This article is original article for blogger. Without permission from blogger,.

must not be reprinted