Analysis of OBJECTIVE-C memory management and examples of cyclic strong references

Source: Internet
Author: User



Understand


    1. Variable
    2. Scope
    3. Variable abandonment
    4. Holding objects
    5. Releasing objects
    6. Object owner (reference count)
    7. Object abandonment
    8. The effect of variable ownership modifiers on objects


Objects holding each other cause a cyclic strong reference, such as a target object for a Nstimer object that repeatedly executes a task
The timer object is preserved, and if the Nstimer object is held by the target object, a cyclic strong reference must occur because the Nstimer object will hold the target.
The Nstimer object is also the target object's member variable, which is the target object and holds the Nstimer object.
This cyclic strong reference persists until the target object calls the Invalidate method of the Nstimer object to release the Nstimer object
You can use the block and __weak ownership modifiers to resolve the cyclic strong reference by extending the Nstimer class


#import <Foundation / Foundation.h>

@interface NSTimer (BlocksSupport)

+ (NSTimer *) scheduledTimerWithTimeInterval:
(NSTimeInterval) interval
block: (void (^) ()) block
repeats: (BOOL) repeats;

@end


@implementation NSTimer (BlockSupport)

+ (NSTimer *) scheduledTimerWithTimeInterval:
(NSTimeInterval) interval
block: (void (^) ()) block
repeats: (BOOL) repeats
{
return [self scheduledTimerWithTimeInterval: interval
target: self
selector: @selector (blockInvoke :)
userInfo: [block copy]
repeats: repeats];

}

+ (void) blockInvoke: (NSTimer *) timer {
void (^ block) () = timer.userInfo;
if (block) {
block ();
}
}
// When using other Class
-(void) startAction {
__weak Class * weakSelf = self; // Define a weak reference to yourself
_myTimer = [NSTimer scheduledTimerWithInterval: 5.0 block: ^ {
Class * strongSelf = weakSelf; / * The block captures the weak reference pointing to itself, and does not directly capture the ordinary self variable, so the block does not hold the Class object
Then immediately let the __strong variable hold the __weak Class object to ensure that the instance continues to survive during execution,
In other words, strongSelf also holds the Class object, but, however, strongSelf is in the block body
The variables declared in, only have block blocks in their life cycle, and are released after the block block is executed, so they do not hold Class objects * /
[strongSelf excueAction];
} repeats: YES];
}

-dealloc {
[_myTimer invalidate]; / * When the outside world holds the last reference of the Class object to release it, this method will be executed, which will also stop the timer.
If the developer forgets to call the invalidate method in dealloc to stop the timer, the weakSelf in the block executed by the timer will become nil
strongSelf also points to nil, so the timer performs a no-op. * /
} 




Analysis of OBJECTIVE-C memory management and examples of cyclic strong references


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.