I set in my interface.h a property as
#porperty(nonatomic, retain) *foo;
If i don't use this ivar in my implementation should I release it in dealloc ?
Yes:
- (void)dealloc
{
// Other release code
[_foo release];
[super dealloc];
}
EDIT: Thanks to #borrrden for the tip about avoiding setters during dealloc.
yes you should release
for release you can write like this :
- (void)dealloc
{
// other release stuff
if(foo != nil)
{
[foo release];
foo = nil;
}
[super dealloc];
}
Related
This question already has answers here:
app crash in the dealloc
(2 answers)
Closed 9 years ago.
I'm facing small problem,
I declare an array in .h file and allocate it in viewDodLoad method. In dealloc method I check if array not equal to nil then array=nil. But it's crashing in iOS 5.1.1. I can't understand reason for this crash.
My code,
#interface SampleApp : UIViewController
{
NSMutableArray *objArray;
}
#end
#implementation SampleApp
- (void)viewDidLoad
{
[super viewDidLoad];
objArray=[[NSMutableArray alloc]init];
}
-(void)dealloc
{
[super dealloc];
if (objArray!=nil)
{
[objArray removeAllObjects];
[objArray release];objArray=nil;
}
}
Add [super dealloc]; at the end of dealloc method and not at beginning. It is recommended by Apple in its documentation for dealloc method.
When not using ARC, your implementation of dealloc must invoke the superclass’s implementation as its last instruction.
Modify your code as below,
-(void)dealloc
{
if (objArray!=nil)
{
[objArray removeAllObjects];
[objArray release];objArray=nil;
}
[super dealloc];
}
Also, you need not call [objArray removeAllObjects] when you are releasing the entire array. When array is released, internally it will call release on all contained objects.
Hope that helps!
[super dealloc] method must be call in end of this method. Because you can not access variables of the superclass anymore because they are released when you call [super dealloc]. It is always safe to call the superclass in the last line.
-(void)dealloc
{
// ----------- your stuff ------------
[super dealloc];
}
With manual memory management, your -dealloc method looks as follows:
-(void)dealloc
{
[objArray release]; // objArray *may* be nil, and this is
// sufficient to release all elements as well.
// call super at the end
[super dealloc];
}
Additionally, you have a potential memory leak in your method -viewDidLoad. If you do it like your example:
- (void)viewDidLoad
{
[super viewDidLoad];
objArray=[[NSMutableArray alloc]init];
}
you may assign objArray a new pointer even if objArray already holds a valid object. The new pointer value will simply override the old, and thus you cannot release the old anymore.
One way is to check whether objArray is not nil and then release it before assigning it a new value:
- (void)viewDidLoad
{
[super viewDidLoad];
if (objArray) {
[objArray release], objArray = nil;
}
objArray = [[NSMutableArray alloc]init];
}
The better approach however is to employ "lazy initializing properties":
First, define an "internal property" for your array (unless you want the array publicly accessible). In your .m file:
// In your implementation file define a private property in a class extension:
#interface SampleApp ()
#property (nonatomic) NSMutableArray* objArray;
#end
#implementation SampleApp
#synthesize objArray = _objArray; // this will create the setter
-(void)dealloc
{
[_objArray release];
[super dealloc];
}
// Lazy init property: (this is the getter)
- (NSMutableArray*) objArray {
if (_objArray == nil) {
_objArray = [[NSMutableArray alloc] init];
}
return _objArray;
}
- (void) viewDidLoad {
[super viewDidLoad];
// When needing the array, simply *and always* access it
// through the property "self.objArray":
NSUInteger count = [self.objArray count];
}
...
Lazy initialization of properties are quite handy. Basically, you won't worry anymore if they are initialized or not - they simply are, when you use the property accessor.
I have an array for IBOutlet collection
.h
#interface UpisiRezultat : UIViewController {
NSArray *buttons;
}
#property (nonatomic, retain) IBOutletCollection(UIButton) NSArray *buttons;
.m
#synthesize buttons;
- (void)viewDidLoad
{
[self setValue:[UIFont fontWithName:#"NeverSayNever" size:22] forKeyPath:#"buttons.font"];
[super viewDidLoad];
}
- (void)viewDidUnload
{
buttons = nil;
}
- (void)dealloc
{
[buttons release]; --> Error
[super dealloc];
}
Why does my program crash when I have [buttons release]; in dealloc?
Without it, it doesn't crash...
updated(Dec1) code and Tested.
- (void)dealloc {
self.buttons = nil;
[super dealloc];
}
you should not release them.
http://www.bobmccune.com/2011/01/31/using-ios-4s-iboutletcollection/
If you have made a connection to your buttons with Interface Builder, it's your view that owns it and will release it.
Since buttons is an NSArray and it is explicitly retained, then it must be released and then set to nil in -dealloc.
See Darren's answer at: Settings IBOutlets to nil in dealloc
See an IBOutletCollection example at: http://www.bobmccune.com/2011/01/31/using-ios-4s-iboutletcollection/.
For one of my custom classes, I have defined a method called initialize to set some instance variables at the same time as the init. The code is below. The analyzer tool is reporting a leak in viewDidLoad on the line with [[Employee alloc]..
Since I am releasing the variable in the dealloc, I thought that this should be fine..what could be the issue? thanks in advance.
#interface testViewController : UIViewController <UITextFieldDelegate>{
Employee *employee;
}
- (void)viewDidLoad {
if(employee ==nil)
employee = [[Employee alloc] initialize:#"John"];
if (![employee.entityName isEqualToString:#"Test"]) { //The leak is reported here for object allocated above
///...
}
}
- (void)viewDidUnload {
[super viewDidUnload];
employee = nil;
}
- (void)dealloc {
[super dealloc];
[employee release];
}
//In the Employee class
-(id) initialize:(NSString*) name{
self = [super init];
self.entityName = name;
return self;
}
In your viewDidUnLoad you need to release employee before it gets set to nil. Else in your dealloc, you are just releasing nil.
ie
- (void)viewDidUnload {
[super viewDidUnload];
[employee release];
employee = nil;
}
i use a UIView subclass with an NSMutableArray of other Views to indicate values as bars.
I init it in my initWithFrame. The Instruments tells after a few create and remove of my UIView subclass that there is a leak on the alloc of the NSMutableArray.
Thats why i framed it with the if to avoid multiple objects. But doesn't help
- (id) initWithFrame :(CGRect)frame
{
self = [super initWithFrame:frame];
if (self.uiValueSubviews == nil){
self.uiValueSubviews = [[NSMutableArray alloc]init];
}
return self;
}
- (void)dealloc {
[self.uiValueSubviews release];
[super dealloc];
}
Am i doing something wrong with the dealloc?
Thanks for your Help
Two problems I see with memory management involving your property.
Properties should always be set to
an autoreleased object or an
object you will be releasing on your
own.
Never send release directly to a property. I prefer to release underlying variable if possible (ex. [_uiValueSubviews release];)
Change the code to the following.
- (id) initWithFrame :(CGRect)frame
{
self = [super initWithFrame:frame];
if (self.uiValueSubviews == nil){
//Set to autoreleased array
self.uiValueSubviews = [NSMutableArray array];
}
return self;
}
- (void)dealloc {
//nil the value
self.uiValueSubviews = nil;
[super dealloc];
}
You should do like this :
- (id) initWithFrame :(CGRect)frame
{
if ((self = [super initWithFrame:frame]))
{
if (self.uiValueSubviews == nil){
uiValueSubviews = [[NSMutableArray alloc]init];
}
return self;
}
- (void)dealloc
{
self.uiValueSubviews = nil;
[super dealloc];
}
You uiValueSubviews is probably a retain property so when you alloc, your retainCount is +1 and self. +1 too.
An other way, avoiding autoreleased objects, would be:
// ...
if (self.uiValueSubviews == nil)
{
NSMutableArray *uiValueSubviews_tmp = [[NSMutableArray alloc] init];
// maybe do something with uiValueSubviews_tmp
self.uiValueSubviews = uiValueSubviews_tmp;
[uiValueSubviews_tmp release];
}
// ....
As far as I know, that's how Apple does it in their examples.
If I am having a variable defined with "Assign" property then is it ok to setting them to nil in dealloc method?
#property (nonatomic, assign) id test;
- (void)dealloc {
self.test = nil;
}
It's better to release the ivar directly. If a subclass overrides the setter methods of a property, your object might leak because your setter is not called. Consider:
#interface ClassA
#property (readwrite, retain) id anObject;
#end
#interface ClassB : ClassA
#end
#implementation ClassA
#synthesize anObject;
- (void)dealloc {
self.anObject = nil;
[super dealloc];
}
#end
#implementation ClassB
- (void)setAnObject: (id)anObject {
// do nothing!
}
#end
Instances of ClassB will leak anObject!
Depends how you do it, if you do it via the property setter (not recommended), then yes.
If you do direct assignment, then no, because the retained object will leak.
So this is fine:
- (void) dealloc {
self.test = nil;
[super dealloc];
}
But this is a no-go:
- (void) dealloc {
test = nil;
[super dealloc];
}
My advice is to just send the release message to all of your retained ivars in -dealloc, this will work nicely because if test happens to be nil, then nothing will happen.
Trust me. Send release directly in -dealloc. That is all.
- (void) dealloc {
[test release];
[super dealloc];
}