简体   繁体   中英

When retained property is released?

I have property like this:

@property(nonatomic,retain) NSString *porpertyList;

@synthesize porpertyList = _porpertyList;

- (void)dealloc
{
[_porpertyList release];
}

And if i do this _porpertyList = @""; in my app. Property is released ?

//Edited Now i don't understand when i should use this @synthesize porpertyList = _porpertyList; ?

Depends on what memory model you are using. If you are using ARC, there's no need to write a dealloc to release retained properties, this is done for you. If you are not using ARC, you want to release the variables for the retained properties:

- (void) dealloc {
    [_propertyList release];
    [super dealloc];
}

Two things to note here:

  1. You want to release the variable here, not set the property to nil. This avoids side-effects that could occur when using setters (custom behavior, kvo notifications).
  2. Don't forget to call [super dealloc];

If you access property like this

self.property=@"";

you are in fact using setter method( which is auto-created thanks to @synthesize). So, in this case, the old object is released and new one is assigned and retained.

If you synthesized your property using

@synthesize property= _property;

then if you call

_property=@"";

then you just assign new value to the property. Nothing is being released then.

So, in your dealloc method you have some choices:

-(void)dealloc
{
   self.property=@"";//old value released, new value is @""
   self.property=nil;//old value released, new value is nil
   [_property release]; //old value released
   [super dealloc];
}
@synthesize porpertyList = _porpertyList;

Whenever you synthesize an property... you up its retain count by 1..so that's why you have release in your dealloc.

Using self.propertyList = something

and

 porpertyList = something 

are very different things and the latter one should be avoided when using properties.. That is why porpertyList = _porpertyList; is there..so that you don't use propertyList instead of self.porpertyList

The reason is .. that popertyList is a pointer..

when you do self.porperty = something ..you make a separate copy of that object for yourself(not in case of @"") but if you do popertyList = something .. you make it point to another object thus messing with the whole retain count it had initially which can make your program behave strangely..

if you use the @property option for variable name you should assign to it using the self.propertyList = @"" rather then _propertyList = @"" . using self.propertyList will release any previous memory it was using when u assign to it

_porpertyList = @"" will not release anything. If you want to release, you can use self. porpertyList = nil self. porpertyList = nil . This will release it properly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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