3

If I have a method

- (void) myMethod:(NSString *)string {
    [Object anothermethodWithString:string];
}

and I call

[Object myMethod:@"this is a string with no alloc statement"]

Do I need to do something like

- (void) myMethod:(NSString *)string {
    NSString *string2 = [[NSString alloc] initWithFormat:@"%@", string];
    [Object anothermethodWithString:string2];
    [string2 release];
}

instead of the way I had myMethod before? I have misbehaving code that seems to be caused by a string being auto-released while a second method within another method is being called (like in the example). The second way I have myMethod fixed all of my problems.

So is a "non-alloc" string an auto-released string? I asked this question as a follow up to another question (which was completely unrelated and is why I am creating this post), and a few sources said that I don't need to reallocate the string. I am confused, because the behavior of my code tells me otherwise.

vodkhang
  • 18,639
  • 11
  • 76
  • 110
Ayaka Nonaka
  • 856
  • 1
  • 10
  • 22
  • Did you invoke `new`, `alloc`, `retain`, or `copy` to create the string? If not, then you are not responsible for releasing it. Constant strings (`@""`) are global and permanent. – Dave DeLong Jul 17 '10 at 03:18
  • No I did not invoke any of those methods on the string that is passed into myMethod. My permanent @"" string seems to be getting overwritten. – Ayaka Nonaka Jul 17 '10 at 03:22

2 Answers2

4

Dave's got it right. You only need to worry about calling release on objects that you new, alloc, retain, or copy.

The above rule works very well, but if you're curious and want to get into a lot of detail, I suggest reading the Memory management Programming Guide from Apple's docs. It's free and goes from basic concepts into a lot of details.

Neal L
  • 4,329
  • 8
  • 34
  • 39
2

If you use : NSString *str = @"". It is kind of a constant, you don't need to do any memory management.

If you call from a method : NSString *str = [NSString stringWithFormat:@""], the str is already autoreleased.

If you manually alloc, init. You need to call release, or autorelease yourself.

The general memory convention is : if you do something with new, alloc, retain, or copy, you need to release it yourself, any other cases, the object is autoreleased, don't release it

vodkhang
  • 18,639
  • 11
  • 76
  • 110
  • "If you use : NSString *str = @"". It is kind of a constant, you don't need to do any memory management." Does that mean it could get auto-released where I don't want it to? – Ayaka Nonaka Jul 17 '10 at 03:15
  • 1
    @vodkhang That's not the rule. The rule is: if the method used to make the object contains one of `new`, `alloc`, `retain`, or `copy`, then you must `release` it. `init` is no where in that list. You have to release it because you `alloc`'d it, not because you `init`'d it. – Dave DeLong Jul 17 '10 at 03:15
  • 1
    @ayanonagon strings created via `@""` are constant strings and *cannot* be deallocated. They exist in the global memory section of your process and will be there from `exec` to `exit`. – Dave DeLong Jul 17 '10 at 03:16
  • @Dave DeLong Ah! That makes more sense. Hmmm, so what happens when I create a string via @"" in a method parameter? Can that get lost before exit? – Ayaka Nonaka Jul 17 '10 at 03:20
  • 1
    @Dave Delong: ah yeah, sorry for the wrong rule. Sometimes, I forgot that, thanks for reminding me. @ayanonagon : how can you create a @"" via a method parameter? The rule is that the string is there and can be reused by runtime but if you have no pointer to it, you can not get it directly – vodkhang Jul 17 '10 at 03:24
  • @ayanonagon if you create a constant string, it will always exist. end of story. if something's going wrong, your problem lies elsewhere (I promise you haven't found a compiler bug). – Dave DeLong Jul 17 '10 at 03:25
  • @vodkhang Can't I call something like [Object myMethod:@"Hello"] to a method - (void) myMethod:(NSString *)string { NSLog(@"%@", string); } that prints "Hello"? Or is that my problem? – Ayaka Nonaka Jul 17 '10 at 03:28
  • ah, I just misunderstand your ones. That's absolutely ok to do. Can you post more code – vodkhang Jul 17 '10 at 03:48
  • @ayanonagon: you can send release to a constant string as often as you like. It'll never get deallocated. – JeremyP Jul 17 '10 at 09:58