4

If I set a NSString as property

@property (copy) NSString* name;

I always want to use (copy), so that if its value change, all object with such string still have the old value (as specifiedhere).

However, what happens if my NSString is not a class property, but it is just declared in the code ? Is in that case retained or copied everytime I assign a new value ?

thanks

Community
  • 1
  • 1
aneuryzm
  • 63,052
  • 100
  • 273
  • 488

3 Answers3

9

It depends on how you declare it. You should read the documentation on memory management. Basically the rules are:

NSString *aString = [NSString stringWithString:@"Hello"];
NSString *bString = [NSString stringWithFormat:@"%@", @"Hello"];

In these cases, the string is not copied or retained. It is autoreleased, which means it will be automatically deallocated the next time the autorelease pool drains. You do not have to call the release method on them. (So assigning a new value will not cause it to leak.)

NSString *cString = [[NSString alloc] initWithFormat:@"%@", @"Hello"];
[cString release];

According to Objective C convention, methods that use alloc and have a retain count of one are not autoreleased, so you need to release them explicitly. Assigning a new value without releasing the old one will cause a leak.

You can also explicitly call a "copy" method or a "retain" method on a string. In either case, the new string will have a retain count of 1 and will not be autoreleased, so you will need to call the release method on it before you assign a new value.

NSString *dString = [cString retain];
NSString *eString = [cString copy];

...
[dString release];
[eString release];

If it is a property, and you use self.variableName, this will be taken care of for you (through the getters and setters which are generated with @synthesize). If you do it explicitly, you must make sure to call release on variables that you have called retain or copy on.

Edit: As some commentators below have noted, thinking about management in terms of "ownership" is usually the preferred of describing these ideas, rather than retain count.

Matthew Gillingham
  • 3,429
  • 1
  • 22
  • 26
  • Methods which start with `alloc`, not `init`, create ownership, not a retain count of 1. `init` doesn't have any effect on memory, and you should avoid thinking of retain counts as absolute numbers. But I didn't downvote you. – jscs May 13 '11 at 07:53
  • 2
    -1. The problems are: Constant strings e.g. `@"Hello"` are not autoreleased. You don't own them, but they never go away. It's not `-init` that gives you a retained object, but methods that begin with alloc or new or contain copy (read the rules you linked to). Finally, copy and retain do not give you an object with a retain count of 1, they give you an object you own, that's all you should assume. – JeremyP May 13 '11 at 08:00
  • 1
    That is true. I avoided giving a full explanation in terms in ownership because I didn't want to rewrite Apple's documentation. The answer to the question is "It depends on how you declare it." Apple's documentation does both, there is a section describing ownership and a section called "Practical Memory Management" which talks explicitly about retain count. I find understanding both concepts is helpful. But "ownership" is a good model – Matthew Gillingham May 13 '11 at 08:00
  • You are correct about constant strings. I didn't realize that. I will edit my answer. – Matthew Gillingham May 13 '11 at 08:06
  • You really need to fix the statement about `init` affecting retain count. That's flat out false. You're also missing an opening brace in the assignment to `cString`, just as a side note. – jscs May 13 '11 at 08:14
4

If it's not a property and just declared in code, you need to explicitly retain or copy it, ie

NSString myString = [otherString copy];

or

NSString myString = [otherString retain];

Either way you also need to ensure it's released at somepoint.

Tom Jefferys
  • 13,090
  • 2
  • 35
  • 36
2

If you're not using the property's setter, like self.name = @"foo" or [self setName:@"foo"], but rather assign the variable directly, like name = @"foo", it doesn't matter at all how the property is declared.

You have to understand that the property syntax is just a shortcut for writing accessor methods (-name and -setName: in this case). If you're not calling these methods (implicitly by setting the property), it doesn't matter how they work internally (which is what you specify by retain or copy).

omz
  • 53,243
  • 5
  • 129
  • 141
  • @omz First off: so setting a property with self.name = @"foo" is different than setting it with name = @"foo" ? In the first case it is a copy and in the second case a retain ? – aneuryzm May 13 '11 at 08:36
  • I'm not sure if I got the answer to my question by the way. I will ask again: if I DON'T have a property, and I just declaring a NSString variable in my code, assign it to several objects and I assign to it a different value several times, is this value updated in all previous objects containing such variable ? Or the old values remain ? – aneuryzm May 13 '11 at 08:38
  • 1
    Yes, completely different. In the first case, you're actually calling a method (setName), in the second case, you're simply assigning a variable, neither retaining, nor copying the value (comparable to an "assign" property, though not exactly equivalent). – omz May 13 '11 at 08:39
  • Regarding your second comment: First off, NSString is immutable, so an existing NSString can't change, unless you're using NSMutableString, which is a subclass. If you do something like NSString *a = @"foo"; NSString *b = a; a = @"bar"; b will still be @"foo", because @"bar" is a new object. – omz May 13 '11 at 08:44
  • 1
    However, if you use NSMutableString, like NSMutableString *a = [NSMutableString stringWithString:@"foo"]; NSMutableString *b = a; [a setString:@"bar"]; both a and b will be @"bar". – omz May 13 '11 at 08:45
  • 2
    @Patrick: You're talking slightly backwards. A variable is a name; it indicates an object, and you can use any number of variables (names) to refer to that object. If you say `NSString * foo = @"bar";`, then you have an `NSString` object, `@"bar"`, which you can get with the name `foo`. You can assign the object to other variables: `NSString * yak = foo;` and this does not change the memory use at all. It simply gives you a new name for the same object. – jscs May 13 '11 at 08:46