views:

370

answers:

2

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.

A: 

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
"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?
ayanonagon
@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
@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
@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?
ayanonagon
@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
@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
@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?
ayanonagon
ah, I just misunderstand your ones. That's absolutely ok to do. Can you post more code
vodkhang
@ayanonagon: you can send release to a constant string as often as you like. It'll never get deallocated.
JeremyP
+2  A: 

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