adit
adit

Reputation: 33674

removeFromSuperview and release memory management

I have a MBProgressHUD that I allocate as follows:

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

if I call removeFromSuperview then would I have to call progressHUD release again? 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?

Upvotes: 2

Views: 1459

Answers (3)

paiego
paiego

Reputation: 3787

If progressHUD_ is a retain property, then you will have to release it in dealloc. However, the nice thing about a retain property is that you only have to set it to nil to reclaim the memory; 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. e.g. 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).

I also recommend calling self.progressHUD_ = nil; in viewDidUnload which is called during low memory conditions and the view is not showing. 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]; in viewDidLoad rather than in init...

Upvotes: 1

Johannes Fahrenkrug
Johannes Fahrenkrug

Reputation: 44826

How is your progressHUD_ property defined? (btw, the ivar should have a trailing underscore, but not the property name).

In case it is defined as (retain, whatever), you will have to release it again:

  1. When you create it, its retainCount is +1.
  2. When you assign it to your property, its retainCount will be increased by one.
  3. When you add it as a subview to the parent view, its retainCount will be increased by one.
  4. At some point, autorelease will eventually decrease it by 1, but the view and the property still hold on to it.

So you'll have to either set your property to nil or call release on the ivar in your dealloc method.

Also, you probably want to use copy instead of retain when defining an NSString property. And yes: you'll have to release it either way.

Upvotes: 0

Nick Lockwood
Nick Lockwood

Reputation: 41005

No, you don't have to release it again. Views retain their subviews and release them again automatically when you call 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. You have to write those release statements manually, they aren't added automatically (unless you are using ARC of course, which I strongly recommend).

Upvotes: 0

Related Questions