0
NSString* str = @"foo";
str = [NSString stringWithFormat:@"%@%@", str, @"bar"];

Will @"foo" be autoreleased? That is, will its retain count go to 0 when str is reassigned?

rob
  • 4,069
  • 3
  • 34
  • 41

2 Answers2

2

Yes, it will be released. You didn't alloc/init/new/copy it.

Edit: I suppose it would be more correct to say "no, it won't leak", since it is in fact a constant. The alloc/init/new/copy advice still applies though.

James J
  • 6,428
  • 6
  • 35
  • 45
1

@"foo" is a compile-time constant object in memory, so its retain count is irrelevant. It is safe to reassign this way, even when reusing it in a stringWithFormat: as you are doing.

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356