执行浅拷贝后如何管理内存?

时间:2016-04-15 10:50:05

标签: ios objective-c memory-management copy

我试图理解内存管理概念,并开始从Apple文档和许多关于Objective-C内存管理的文章中挖掘出来。当我开始从Apple文档中搜索时,我被发送回对象复制和消息传递概念。

在阅读有关复制对象时,我遇到了一个问题,我试图找到解决方案,但未能得到满意的解决方案。 如果有人能帮助我,我们将感到荣幸。 问题是:

 array1 = {obj1,obj2,obj3}
 array2 = {obj4,obj5,obj6}
 array2 = array1 -- Shallow Copy

与array2相关的内存发生了什么变化?如果有泄漏怎么发布?在ARC和NonARC环境中执行此类浅拷贝的更好做法是什么?

1 个答案:

答案 0 :(得分:0)

When you assign array2 = array1 you are actually assigning an array reference, you are not copying anything, shallow or otherwise.

The array that was referenced array2 will be released if there is no other strong reference to this array. As a result of the array being released, the objects in the array (obj4, obj5 & obj6) will be released if there is no other variable holding a strong reference to any of them

So, initially you have

array1 = {obj1,obj2,obj3}
array2 = {obj4,obj5,obj6}

After the assignment you have

array1      array2
  \\         //
 {obj1,obj2,obj3}

and objects 4-6 have been released along with the array that held them