0

I've seen some answers but those didn't expand enough and I think they were before ARC was introduced.

So if I have two NSSstring can I set

string1 =string2;

or

NSString * a1=@"String";
al=@"Lead";

without causing memory leaks or other problems?

Edit: What about view controller or delegate properties set to (copy, nonatomic)?

Edit2: It should be NSString *al=@"String". I hope this doesn't change the idea that it doesn't leak.

justin
  • 104,054
  • 14
  • 179
  • 226
user1515993
  • 69
  • 1
  • 7
  • This is not a discussion forum. You're supposed to ask one focused question, and choose the answer that you deem answers your question. Read the FAQ for how the site is supposed to work. :) http://stackoverflow.com/faq – Almo Aug 13 '12 at 21:26
  • I see you have been on the site recently, and still have not accepted any answers. – Almo Aug 21 '12 at 13:18

3 Answers3

1

No, assigning and re-assigning string literals will not cause a memory leak. You only need to worry about memory leaks when not using ARC and assigning something that uses alloc, retain, copy, mutableCopy or a method prefixed with new.

NSString a1=[@"String" mutableCopy];  
al=@"Lead";//This will cause a leak since you called copy above.

See the Advance Memory Management Rules for details.

Joe
  • 56,979
  • 9
  • 128
  • 135
  • I forgot to add pointers to it so it should say NSString *al = @"String". Does that change it? – user1515993 Aug 13 '12 at 20:54
  • The pointer is required, otherwise you would get compiler errors. This does not change things as I was assuming a pointer was there. – Joe Aug 13 '12 at 20:55
  • Oh and I do have properties set to nonatomic and copy. Are those OK? – user1515993 Aug 13 '12 at 20:55
  • @user1515993 See [here](http://stackoverflow.com/questions/387959/nsstring-property-copy-or-retain) for the discussion about `copy`. As for `nonatomic` you should add that if you do not plan on your your class being accessed by multiple threads. – Joe Aug 13 '12 at 20:57
0

Your code is safe and will not leak. Cocoa and NSStrings are quite smart about how they deal with these issues.

You can end up with other interesting things happening because NSStrings are immutable, and you can get weirdness if you try to worry about the pointers themselves. But your examples don't suffer from these issues.

Almo
  • 15,538
  • 13
  • 67
  • 95
  • That is beyond the scope of this question. But you can read more elsewhere, for example here: http://stackoverflow.com/questions/9154288/why-does-nsstring-sometimes-work-with-the-equal-sign – Almo Aug 13 '12 at 20:56
0

neither would leak in ARC.

however, you might want to be aware that you are dealing with a reference in this case -- no implicit copy is performed (unless you use a copy or copy-qualified property).

Edit: What about view controller or delegate properties set to (copy, nonatomic)?

for NSStrings? yes, copy should be the default for NSStrings. nonatomic should be the default for everything. of course, there are exceptions to this -- when you might deviate from the defaults.

justin
  • 104,054
  • 14
  • 179
  • 226
  • @user1515993 "Yes" the `NSString` variable/property should be 'copy'. also - neither would leak under ARC. – justin Aug 13 '12 at 22:54