A noob question.
if i have a string @"Hello i am a string". is @"" an object? what is it ? where is it stored?
What will happen if i release an object i did not create? or what are its complications?
NSString *string1 = [[NSString alloc] initWithString:@"string"];
NSString *string2 = string1;
[string2 release];
also another question what's the difference between?
NSString *string1 = [[NSString alloc] initWithString:@"string"];
NSString *string2 = @"string";
I know 开发者_如何学运维i'm a noob but i just wanna know.
Thanks in advance
EDIT:
i meant to say alloc initWithString instead of stringWithString
Never release objects you don't own. The rule is quite simple: you only own an object if you obtained it by using new
, alloc
, retain
or copy
(NARC for short).
Objects obtained by any other method should be returned autoreleased. What this means is that the object will be released automatically on the next autorelease pool drain. If you want to keep the object, you need to retain
it then release
it. If you don't care if the object gets deallocated after you finish using it in your current scope, then don't do anything with it.
So, to sum it up:
- You must pair any NARC with a
release
- You must not pair any non-NARC with a
release
- Non-NARC methods will return
autorelease
d objects - You must
retain
anyautorelease
d object if you need it beyond the current scope
To address your specific questions:
@"Hello i am a string"
is an object. It is an instance ofNSConstantString
and it gets created at compile time. You cannot and should notrelease
it. It will live on throughout the life of the application. Take a look at this question for more about it.- If you release an object you don't own, then you will decrement its retain count and you will cause complications with code that assumes it must not retain it.
NSString *string2 = string1;
That does not create a new object. string1
is not an object, it is a pointer to an object. that assignment merely makes a copy of the pointer and places it in string2
.
Due to an implementation detail (an internal implementation detail that may not be true in all cases and should never be relied upon), this is true:
NSString *string1 = [NSString stringWithString:@"string"];
string1 == @"string"; // this is a true expression
String constants -- @"dddd" -- are compiled into the binary by the compiler. They can't be released and there is only one constant per unique string. stringWithString:
recognizes constants and does not make a new copy.
Now, if you were to do:
NSMutableString *string1 = [NSMutableString stringWithString:@"string"];
NSMutableString *string2 = string1;
[string2 release];
That would copy the @"string" into a new instance of NSMutableString and string1 would point to that object.
That code, though, would be an over-release ([string1 release];
would be, too) since stringWithString:
returns an autoreleased object.
Now, if you were to:
NSMutableString *string1 = [[NSMutableString alloc] initWithString:@"string"];
NSMutableString *string2 = string1;
[string2 release];
Then that code is technically correct in terms of the retain counts; the string object would be deallocated. But after the release
, both string1
and string2
would be dangling pointers -- that is, the address they hold no longer points to a viable object and attempting to message either would yield undefined results.
Releasing an object you didn't create will eventually get you and EXC_BAD_ACCESS exception when the owner of the object tries to release it and it's already deallocated.
Regarding your second question, there's a good discussion here.
When you use the @""
syntax, you are creating an object. It's shorthand for the creation of an immutable string, also known as a NSConstantString
under the covers. String constants are stored in the data segment portion of the application binary and are loaded into memory along with the code and referenced during runtime.
As others have mentioned, any time you don't explicitly allocate space for an object, you shouldn't release it. Any time you over-release an object, you'll introduce unexpected, and often random, issues to your application. @Radu's explanation is spot on.
精彩评论