如何在内存管理中发生保留周期? [英] How Retain cycles occurs in Memory Management?

查看:83
本文介绍了如何在内存管理中发生保留周期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有些人可以用示例程序解释保留周期问题吗?

Can some explain the retain cycles problem with a sample program?

推荐答案

请考虑以下事项:

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

b 插入<$ c时当它插入<$时,保留了$ c> a , b ,同样 a C $ C> b 。由于两者现在彼此之间都有一个强引用,因此除非你手动打破周期,否则它们都不会被解除分配。从另一个中删除。

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.

请注意,Cocoa内存管理指南还包含关于保留周期的部分并包含 weak的解释参考,这有助于解决这些问题。

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.

这篇关于如何在内存管理中发生保留周期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆