开发者

How Retain cycles occurs in Memory Management?

开发者 https://www.devze.com 2023-01-07 03:38 出处:网络
Can some explain the retain cycles problem with a sample program?开发者_StackOverflow中文版 Consider the following:

Can some explain the retain cycles problem with a sample program?开发者_StackOverflow中文版


Consider the following:

NSMutableArray *a = [NSMutableArray array];
NSMutableArray *b = [NSMutableArray array];
[a addObject:b];
[b addObject:a];

When b is inserted into a, b is retained, likewise a when its inserted into b. As both now have a strong reference to each other, neither will get deallocated unless you manually break the cycle by e.g. removing one from the other.

Note that the Cocoa Memory Management guide also contains a section on retain cycles and includes an explanation of weak references, which help with these problems.


I've got just the right article(s) for you:

http://cocoawithlove.com/2010/06/assign-retain-copy-pitfalls-in-obj-c.html

http://cocoawithlove.com/2009/07/rules-to-avoid-retain-cycles.html

http://developer.apple.com/iphone/library/documentation/Cocoa/Conceptual/MemoryMgmt/MemoryMgmt.html

http://www.markj.net/iphone-memory-management-tutorial-video/

0

精彩评论

暂无评论...
验证码 换一张
取 消