All the tutorials I have done say that I set stuff to nil in viewDidUnload and then release in dealloc. However I am being told that this would cause a memory leak since viewDidUnload gets called before a dealloc and so I am unable to release anything that is set to nil.
我所做的所有教程都说我在viewDidUnload中将stuff设置为nil,然后在dealloc中释放。但是我被告知这会导致内存泄漏,因为在dealloc之前调用viewDidUnload,因此我无法释放任何设置为nil的内容。
Can someone help clarify?
有人可以帮忙澄清一下吗?
2 个解决方案
#1
4
If you create an object (with alloc
and init
) in viewDidLoad
, then you should release
it in viewDidUnload
. The reason is that sometimes viewDidUnload
is called to save memory, but dealloc
is not called. Then, later, viewDidLoad
may be called again. In general, release anything you allocate in the inverse method, e.g.
如果在viewDidLoad中创建一个对象(使用alloc和init),则应该在viewDidUnload中释放它。原因是有时调用viewDidUnload来节省内存,但不调用dealloc。然后,稍后可以再次调用viewDidLoad。通常,释放您在逆方法中分配的任何内容,例如
If you allocate an object in init
, then release it in dealloc
.
如果在init中分配对象,则在dealloc中释放它。
If you allocate an object in viewDidLoad
, then release it in viewDidUnload
.
如果在viewDidLoad中分配对象,则在viewDidUnload中释放它。
If you allocate an object in viewWillAppear
(or viewDidAppear
), then release it in viewWillDisappear
(or viewDidDisppear
).
如果在viewWillAppear(或viewDidAppear)中分配对象,则在viewWillDisappear(或viewDidDisppear)中释放它。
#2
0
Have a look at the answer given by @Sean in this previous SO question. He clearly states the purpose of releasing in viewDidUnload
看看@Sean在之前的SO问题中给出的答案。他明确说明了在viewDidUnload中释放的目的
One of the most important reasons for implementing it is that UIViewController subclasses commonly also contain owning references to various subviews in the view hierarchy. These properties could have been set through IBOutlets when loading from a nib, or programmatically inside -loadView, for instance.
实现它的最重要原因之一是UIViewController子类通常还包含对视图层次结构中各种子视图的拥有引用。例如,当从nib加载时,或者通过编程方式在-loadView中加载时,可以通过IBOutlets设置这些属性。
The additional ownership of subviews by the UIViewController means that even when its view is removed from the view hierarchy and released to save memory, through which the subviews are also released by the view, they will not actually be deallocated because the UIViewController itself still contains its own outstanding retaining references to those objects as well. Releasing the UIViewController's additional ownership of these objects ensures they will be deallocated as well to free memory.
UIViewController对子视图的额外所有权意味着即使从视图层次结构中删除其视图并释放以保存内存(视图也通过其释放子视图),它们实际上也不会被释放,因为UIViewController本身仍然包含它对这些对象也拥有出色的保留参考。释放UIViewController对这些对象的额外所有权可确保它们被释放以释放内存。
The objects that you release here are usually recreated and set again when the UIViewController's view is re-loaded, either from a Nib or through an implementation of -loadView.
通常在Nib或通过-loadView的实现重新加载UIViewController的视图时,重新创建并重新设置此处释放的对象。
Also note that the UIViewController's view property is nil by the time this method is called.
另请注意,调用此方法时,UIViewController的view属性为nil。
#1
4
If you create an object (with alloc
and init
) in viewDidLoad
, then you should release
it in viewDidUnload
. The reason is that sometimes viewDidUnload
is called to save memory, but dealloc
is not called. Then, later, viewDidLoad
may be called again. In general, release anything you allocate in the inverse method, e.g.
如果在viewDidLoad中创建一个对象(使用alloc和init),则应该在viewDidUnload中释放它。原因是有时调用viewDidUnload来节省内存,但不调用dealloc。然后,稍后可以再次调用viewDidLoad。通常,释放您在逆方法中分配的任何内容,例如
If you allocate an object in init
, then release it in dealloc
.
如果在init中分配对象,则在dealloc中释放它。
If you allocate an object in viewDidLoad
, then release it in viewDidUnload
.
如果在viewDidLoad中分配对象,则在viewDidUnload中释放它。
If you allocate an object in viewWillAppear
(or viewDidAppear
), then release it in viewWillDisappear
(or viewDidDisppear
).
如果在viewWillAppear(或viewDidAppear)中分配对象,则在viewWillDisappear(或viewDidDisppear)中释放它。
#2
0
Have a look at the answer given by @Sean in this previous SO question. He clearly states the purpose of releasing in viewDidUnload
看看@Sean在之前的SO问题中给出的答案。他明确说明了在viewDidUnload中释放的目的
One of the most important reasons for implementing it is that UIViewController subclasses commonly also contain owning references to various subviews in the view hierarchy. These properties could have been set through IBOutlets when loading from a nib, or programmatically inside -loadView, for instance.
实现它的最重要原因之一是UIViewController子类通常还包含对视图层次结构中各种子视图的拥有引用。例如,当从nib加载时,或者通过编程方式在-loadView中加载时,可以通过IBOutlets设置这些属性。
The additional ownership of subviews by the UIViewController means that even when its view is removed from the view hierarchy and released to save memory, through which the subviews are also released by the view, they will not actually be deallocated because the UIViewController itself still contains its own outstanding retaining references to those objects as well. Releasing the UIViewController's additional ownership of these objects ensures they will be deallocated as well to free memory.
UIViewController对子视图的额外所有权意味着即使从视图层次结构中删除其视图并释放以保存内存(视图也通过其释放子视图),它们实际上也不会被释放,因为UIViewController本身仍然包含它对这些对象也拥有出色的保留参考。释放UIViewController对这些对象的额外所有权可确保它们被释放以释放内存。
The objects that you release here are usually recreated and set again when the UIViewController's view is re-loaded, either from a Nib or through an implementation of -loadView.
通常在Nib或通过-loadView的实现重新加载UIViewController的视图时,重新创建并重新设置此处释放的对象。
Also note that the UIViewController's view property is nil by the time this method is called.
另请注意,调用此方法时,UIViewController的view属性为nil。