I will post downside 3 variant of a sample method that uses autorelease famous method stringByAppendingString.(non-arc sample project in xcode 4.6.2.IOS project)
Sample Block 1: returns nothing.just a weird points to nothing.but not null.
Sample Block 2: returns nothing too!.
Sample Block 3: returns A1A2A3. as expected but I think it has leak.
My Questions are:
a) sample method 1 working as expected in MAC project.But not in IOS Project.
b) is sample block 3 - includes a leak ?
c) look at comments in Sample Block 2. word_ becomes A1,A1A2 and nothing.why ?
d) what would you code your-own of same method in a different ways?.Im looking for the safe standart method.
thanks.
-(NSString*)sampleMethod
{
NSString *word_=@"";
NSString *a1=@"A1";
NSString *a2=@"A2";
NSString *a3=@"A3";
word_=[word_ stringByAppendingString:a1];// word_ is A1
word_=[word_ stringByAppendingString:a2];// word is nothing but another pointer
word_=[word_ stringByAppendingString:a3];// word is nothing too but pointer changed.
return word_;
}
SAMPLE BLOCK 2
-(NSString*)sampleMethod
{
NSString *word_=@"";
NSString *a1=@"A1";
NSString *a2=@"A2";
NSString *a3=@"A3";
word_=[word_ stringByAppendingString:a1];// word_ is A1
word_=[[word_ stringByAppendingString:a2]retain];// word is A1A2
word_=[[word_ stringByAppendingString:a3]retain];// word is nothing !
return word_;
}
SAMPLE BLOCK 3
-(NSString*)sampleMethod
{
NSString *word_=@"";
NSString *a1=@"A1";
NSString *a2=@"A2";
NSString *a3=@"A3";
word_=[[word_ stringByAppendingString:a1]retain];// word_ is A1
word_=[[word_ stringByAppendingString:a2]retain];// word_ is A1A2
word_=[[word_ stringByAppendingString:a3]retain];// word_ is A1A2A3
return word_;//returns as expected but I think leaks method in this method.
}
but somehow it is solved after clean project.hope this may save hours for someone.or dont know maybe something went wrong in memory chip addresses.
The first one is the only method that gets the memory management correct. The second one leaks two and the third one three NSString instances. If you don't get the string "A1A2A3" from the first method the error is somewhere outside that method.
For every retain message you send you also have to send a release or autorelease message in the same method, unless your method name starts with alloc or copy. In that case the caller needs to release the returned object.