简体   繁体   English

removeFromSuperview并释放内存管理

[英]removeFromSuperview and release memory management

I have a MBProgressHUD that I allocate as follows: 我有一个MBProgressHUD,我将其分配如下:

 self.progressHUD_ = [[[MBProgressHUD alloc] initWithView:self.view] autorelease];

if I call removeFromSuperview then would I have to call progressHUD release again? 如果我调用removeFromSuperview,那么我是否必须再次调用progressHUD release? Also if I declare a property with something like this: 另外,如果我声明的属性是这样的:

NSString * title_;

@property (nonatomic, retain) NSString * title_;

then it is guaranteed that in my dealloc I should have a release on title right? 那么可以保证在我的dealloc中我应该有标题的发行权吗?

If progressHUD_ is a retain property, then you will have to release it in dealloc. 如果progressHUD_是保留属性,则必须在dealloc中释放它。 However, the nice thing about a retain property is that you only have to set it to nil to reclaim the memory; 但是,retain属性的好处是您只需将其设置为nil即可回收内存。 making sure to use "self." 确保使用“自我”。 before. 之前。

e.g.

self.<property_name> = nil;

// or in your case

self.progressHUD_ = nil;

// the following will not release it because it's not accessing the property:

progressHUD_ = nil

I do not recommend using [progressHUD_ release] because it can cause problems. 我不建议使用[progressHUD_ release]因为它可能会引起问题。 eg if elsewhere you had released progressHUD_ and not set it to nil, you may accidentally release a pointer which is no longer allocated (dangling pointer). 例如,如果您在其他地方释放了progressHUD_且未将其设置为nil,则可能会意外释放不再分配的指针(悬空指针)。

I also recommend calling self.progressHUD_ = nil; 我还建议调用self.progressHUD_ = nil; in viewDidUnload which is called during low memory conditions and the view is not showing. 在内存不足的情况下调用的viewDidUnload ,该视图未显示。 It doesn't kill your class instance, but just unloads the view. 它不会杀死您的类实例,而只是卸载视图。 And of course this assumes that you call self.progressHUD_ = [[[MBProgressHUD alloc] initWithView:self.view] autorelease]; 当然,这假定您调用self.progressHUD_ = [[[MBProgressHUD alloc] initWithView:self.view] autorelease]; in viewDidLoad rather than in init... viewDidLoad而不是init...

No, you don't have to release it again. 不,您不必再次释放它。 Views retain their subviews and release them again automatically when you call removeFromSuperview. 视图保留其子视图,并在调用removeFromSuperview时自动再次释放它们。 As long as the view has been autoreleased when you attach it to the view, it will be released when it is removed from the view. 只要在将视图附加到视图时已将其自动释放,则将其从视图中删除后将被释放。

I didn't quite understand your second question, but yes, you have to release any properties of type "retain" or "copy" in your dealloc statement. 我不太理解您的第二个问题,但是是的,您必须在dealloc语句中释放“ retain”或“ copy”类型的所有属性。 You have to write those release statements manually, they aren't added automatically (unless you are using ARC of course, which I strongly recommend). 您必须手动编写这些发布语句,它们不会自动添加(除非您当然使用ARC,我强烈建议这样做)。

How is your progressHUD_ property defined? 如何定义progressHUD_属性? (btw, the ivar should have a trailing underscore, but not the property name). (顺便说一句,该ivar应该有一个下划线,但没有属性名)。

In case it is defined as (retain, whatever) , you will have to release it again: 如果将其定义为(retain, whatever)必须再次释放它:

  1. When you create it, its retainCount is +1. 创建它时,它的retainCount为+1。
  2. When you assign it to your property, its retainCount will be increased by one. 当您将其分配给您的媒体资源时,其keepCount将增加一。
  3. When you add it as a subview to the parent view, its retainCount will be increased by one. 当您将其作为子视图添加到父视图时,其retainCount将增加一。
  4. At some point, autorelease will eventually decrease it by 1, but the view and the property still hold on to it. 在某个时候,自动释放最终会将其减少1,但是视图和属性仍然保留在该位置。

So you'll have to either set your property to nil or call release on the ivar in your dealloc method. 因此,您必须将属性设置为nil或在dealloc方法中的ivar上调用release

Also, you probably want to use copy instead of retain when defining an NSString property. 同样,在定义NSString属性时,您可能想使用copy而不是retain And yes: you'll have to release it either way. 是的:您必须以任何一种方式释放它。

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

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