简体   繁体   English

iOS中插座的内存管理

[英]Memory management for Outlets in iOS

Hi I have this in my application : The question is, should I put self.label =nil ; 嗨,我在我的应用程序中有这个:问题是,我应该把self.label =nil ; in viewDidUnload ? viewDidUnload If yes, why? 如果是,为什么?

//.h
@interface MyClass

@property (nonatomic, retain) IBOutlet UILabel *label;

@end

//.m
@implementation Myclass

@syntesize label = label_;

- (void)dealloc
{
   self.label =nil;
}

@end

Yes, you should set the label property to nil both in viewDidUnload and in dealloc . 是的,您应该在viewDidUnloaddealloc都将label属性设置为nil viewDidUnload is called in low memory situations which enables the app to purge unneeded memory. 在内存不足的情况下调用viewDidUnload ,使应用程序可以清除不需要的内存。

Not setting it to nil in viewDidUnload will not usually cause a memory leak in, but it will prevent the app from saving memory when needed. viewDidUnload不将其设置为nil通常不会导致内存泄漏,但是会阻止应用程序在需要时保存内存。

You should do. 你应该做。

viewDidUnload is called in low memory condition. 在内存不足的情况下调用viewDidUnload So if you want to clean up call self.yourOutlet = nil also in this method. 因此,如果您想清理此方法,也可以调用self.yourOutlet = nil Furthermore it allows you to save extra memory for your app. 此外,它还可以为您的应用程序节省额外的内存。

The next time (after viewDidUnload method is called) your view will be loaded into memory again ( viewDidLoad will be called) and your outlet will be set up correctly. 下次(调用viewDidUnload方法之后),您的view将再次加载到内存中(将调用viewDidLoad ),并且将正确设置出口。

As a rule of thumb any IBOutlet s you release in dealloc , should also be released (reference set to nil like self.label = nil ) in this method. 根据经验,在此方法中,您在dealloc释放的任何IBOutlet都应被释放(引用设置为nil,如self.label = nil )。

A note 一张纸条

You should not call self.label = nil; 您不应调用self.label = nil; in dealloc . dealloc Instead do [label_ release]; 取而代之的是[label_ release]; as documented in Apple Memory Management Guide . Apple内存管理指南中所述

In addition, Stack Overflow search is your friend: 另外,Stack Overflow搜索是您的朋友:

When is UIViewController viewDidUnload called? 何时调用UIViewController viewDidUnload?

When should I release objects in -(void)viewDidUnload rather than in -dealloc? 什么时候应该在-(void)viewDidUnload中释放对象,而不是在-dealloc中释放对象?

Hope that helps. 希望能有所帮助。

Edit 编辑

if you not use ARC (I think not) you should call also [super dealloc]; 如果您不使用ARC(我认为不是),则也应该调用[super dealloc]; like the following: 如下所示:

- (void)dealloc
{
   [label_ release];

   [super dealloc];
}

You should. 你应该。 Although not necessary in most cases, it is considered good practice to set all your pointers to objects to nil on viewDidUnload. 尽管在大多数情况下不是必需的,但是将所有指向对象的指针设置为viewDidUnload上的nil被认为是一种好习惯。 Paul Hegarty explains that on CS193P lecture number 8, Controller Lifecycle. Paul Hegarty在CS193P的第8号演讲中解释了控制器生命周期。

You can watch it here: http://itunes.apple.com/itunes-u/ipad-iphone-application-development/id473757255?mt=2 您可以在这里观看: http : //itunes.apple.com/itunes-u/ipad-iphone-application-development/id473757255?mt=2

You can put this, but you can also put [label release], self.label = nil; 您可以放这个,但也可以放[label release],self.label = nil;。 or just [label release]; 或者只是[标签发布];

It's memory management so that the memory reserved for that object be garbage collected [memory released]. 这是内存管理,以便为该对象保留的内存被垃圾回收[释放内存]。 Very important on old iphone 3g, because it has less memory for the user to run programs but minimal in newer iphone 4+/ios 5.x since it uses arc and you can skip that if you use ARC projects. 在旧版iphone 3g上非常重要,因为它为用户运行程序提供了较少的内存,但在较新的iphone 4 + / ios 5.x中却很少,因为它使用的是arc, 如果使用ARC项目, 可以跳过。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM