Reputation: 1213
When do i have to release objects declared in the .h file and allocated in my .m file viewdidload? Autorelease gives me an error. Do i have to release them in the
-(void)dealloc{
}
method (like properties)?
Thx
Upvotes: 1
Views: 136
Reputation: 1087
If you want to avoid serious problems regarding memory management and memory leaks u need to clarify ur doubts regarding objects allocation and releasing them. Try this link http://iosdevelopertips.com/objective-c/memory-management.html
Upvotes: 0
Reputation: 34912
Yes, you would in general release them in dealloc
. But also if they're created in viewDidLoad
then you might want to release them (and set the ivar to nil
) in viewDidUnload
, especially if the object can be recreated in its current state the next time viewDidLoad
is called.
Of course if you're using ARC then you don't need to explicitly release your ivars in dealloc
as ARC will generate a dealloc
implementation which does this for you.
Upvotes: 0
Reputation: 8944
first, add [super dealloc], it's important:
-(void)dealloc{
[var1 release], var1 = nil;
[var2 release], var2 = nil;
[super dealloc];
}
Second, yes, instance variables that you have retained must be released by you manually.
Upvotes: 4
Reputation: 8512
If you're using ARC (automatic reference counting), you don't. That's done for you automatically. If you're not using ARC then you do it manually in the dealloc like normal. Note that new projects will use ARC automatically and ARC will give you an error if you try to release
, retain
, or autorelease
and object.
Upvotes: 0
Reputation: 118781
If you're using ARC (the default in the latest version of Xcode), then retains/releases are automatically inserted and you only have to worry about the object graph (keeping references when you need to use an object, and avoiding circular references).
If not, you're correct that dealloc
is the right place to release member variables. Although specifically for those created in viewDidLoad
, remember that your view may be loaded/unloaded multiple times for the life of your controller, so consider using viewDidUnload
to release those objects.
Upvotes: 2