tags:

views:

1316

answers:

7

If I make an NSString using the code below, do I need to need to release someString?

NSString *someString = @"somestring";
+4  A: 

No, it's a compile time constant string object, so it doesn't need releasing. It's the moral equiv of char *c = "hello world" -- where the string hello world is in global data, and you're assigning the address of this data to the pointer c.

Brian Mitchell
A: 

No, since it's a compile-time constant string, you do not need to release it. In fact, doing so will likely cause a run-time error.

Barry Wark
A: 

So, please check what Apple does under the section "Insert Data Using a POST Request" at the following link: http://developer.apple.com/safari/articles/creatingrestfulclients.html

I see a "[urlString release];", why?

thanks

Giova
it's a bug, it shouldn't be released
kubi
There, urlString is created using alloc not a constant string.
DyingCactus
Sorry, kubi is right. The POST example has a bug. The others use alloc.
DyingCactus
A: 

incredible, they doesn't even try the code inside their articles oO. Thank you!

Giova
A: 

I checked http://developer.apple.com/safari/articles/creatingrestfulclients.html this case is different from NSString *someThing = @"someThing"; they should release urlString because of
[[NSString alloc] initWithFormat:@"%@%@", baseURLString, queryTerm];

Anywhere you use alloc/init you release it no mater what.

Saleem
A: 

If you created an object via a method call that contains alloc, retain, or copy, or starts with new (N-A-R-C = "narc"), then you are responsible for releasing the object. If this is not the case, then you can ignore the object.

So in the case of strings:

NSString * myString = @"This is a string";

I don't see a call there to a NARC method, so you are not responsible for releasing it. It's really that simple.

Dave DeLong
A: 

Because constant NSString does not get released, would it cause memory problems if used extensively?

Jim