copenndthagen
copenndthagen

Reputation: 50810

retain/release issue

Is there an issue in the statement below for retain/release;

xyzProp = [NSMutableString stringWithString:@"Report error"];

I am getting a error which I think is for the above statement

xyzProp is a property and I am using release on it in dealloc.

Upvotes: 0

Views: 168

Answers (4)

Anurag
Anurag

Reputation: 141929

stringWithString with return an autoreleased string.

Although xyzProp is a property, your code is assigning the reference directly without going through the setter of xyzProp which will not increase the retainCount of that string. That means sometime in the future this string will be released which may happen before the dealloc of your object is called.

Declare the string as a retained property.

@property (nonatomic, retain) NSMutableString *xyzProp;

and when setting it, use the setter.

self.xyzProp = [NSMutableString stringWithString:@"Report error"];

The apple generated setter looks somewhat like this,

- (void)setXyzProp:(NSMutableString *)aString {
    if (aString == xyzProp) {
        return;
    }
    NSMutableString *oldValue = xyzProp;
    xyzProp = [aString retain];
    [oldValue release];
}

Calling self.xyzProp = ... is identical to calling, [self setXyzProp:...] ensuring that the setter is called which will then call retain on the passed-in string.

Update

When setting strings, you almost always want to copy instead of retain, especially in this case since a NSMutableString is being passed. The reason being that the string value may get changed from outside your object without the object knowing which could lead to unexpected behavior.

Upvotes: 1

James Bedford
James Bedford

Reputation: 28982

stringWithString: is returning an autoreleased object, and as you're only using assignment here (not the property), then when you try to release it in the dealloc method you will get an error (because you never retained it in this class).

If your xyzProp really is a property, with the retain attribute in its declaration, then you can use the properties setter method as follows:

self.xyzProp = [NSMutableString stringWithString:@"Report error"];

Which is maybe what you meant to do. If you're still confused, I suggest you refer to the properties programming guide by Apple.

Upvotes: 0

Chuck
Chuck

Reputation: 237110

xypProp by itself is not a property — it would have to be the instance variable backing a property. And in that case, yes, that's an error. You're assigning something you don't own to an instance variable, so it will probably vanish out from under you — and releasing it is certainly wrong, since the problem is that you don't own it in the first place. To use the property setter (which would be correct), it would have to be self.xyzProp = [NSMutableString stringWithString:@"Report error"];.

Upvotes: 0

Philippe Leybaert
Philippe Leybaert

Reputation: 171924

You shouldn't release this object because you didn't allocate it.

The "stringWithString" method will return an autoreleased object, so you should either let it be autoreleased, or retain it and then release it.

As a general rule, you should only release objects that you create using:

  • alloc
  • retain
  • copy
  • new

Any other method will return an autoreleased object.

EDIT:

xyzProp is just a member variable, not a property. If you declare your property as (retain), the generated setter method will retain any new value for you. If you set the member variable directly, you should also retain anything that you didn't allocate.

Upvotes: 2

Related Questions