2013-06-18 68 views
0

我有一个关于如何最好地管理数组指针以确保没有内存泄漏发生的问题。我有一个容器类A和一个合成类B.两者都有一个数组属性,并且都对数组做了它们自己的事情。但容器是暴露给公共API的唯一类。所以我设置了classA.someProperty,并在内部像下面那样设置classB.someProperty。我需要做什么样的清理工作? ARC会自动为我处理这个问题吗?IOS - 内存管理指针

class A 

@property(nonatomic,strong) NSMutableArray* someProperty; 
@property(nonatomic,strong) ClassB* classB; 


Class B 

@property(nonatomic,strong) NSMutableArray someProperty; 

并在A类中执行;

classB.someProperty = [self.someProperty mutableCopy] 
//do some other work with self.someProperty 

and in the implementation in Class B; 
//do some work with self.someProperty [Includes modifications to the array] 
+0

如果你在' - dealloc'中设置'strong'属性为'nil',那应该没问题。这是***的参考点***。*** – 2013-06-18 18:11:37

+0

你确定将属性设置为无dealloc - http://stackoverflow.com/questions/7906804/do-i-set-properties-to-nil-in -dealloc-when-arc- – user2453876

+0

@userXXX是的。看过那个咆哮。 BS,最好。 – 2013-06-18 18:14:46

回答

1

您不需要编写任何清理代码,ARC会负责释放内存。 ARC会自动让代码减少定义为强属性的变量的引用计数。

我会建议你想一个变量的参考计数来理解内存管理。 ARC会自动插入语句以在适当的位置减少引用计数。让我们考虑两种方案供您例如:

方案1

没有其他类有一个变量引用类A的someVariable:当类的引用计数为0,那么你可以肯定的是ARC已经把代码使一些变量引用计数为0(在A类的dealloc中)。

方案2

被引用类的someVariable然后引用someVariable的计数将仍然被递减(由ARC插入的代码),但它不会是零另一变量(比如在C类)所以它不会被释放(这是你想要的,因为你想在C类中访问它)。

+0

谢谢你确认我的直觉,在这种情况下我不需要任何清理代码。 – user2453876