I have the following code:
.h
NSString *mainString;
.m
case 0:
case 1:
case 2:
if ([typeTo textAlignment] == UITextAlignmentRight) {
typeTo.text = [NSString stringWithFormat:@""];
mainString = @"";
[typeTo setTextAlignment:UITextAlignmentLeft];
typeTo.text = [NSString stringWithFormat:@"%@%d", typeTo.text, [sender tag]];
mainString = [NSString stringWithFormat:@"%@%d", mainString, [sender tag]];
} else {
typeTo.text = [NSString stringWithFormat:@"%@%d", typeTo.text, [sender tag]];
mainString = [NSString stringWithFormat:@"%@%d", mainString, [sender tag]];
}
NSLog(@"%@",mainString);
break;
Crashes on this line usually.
mainString = [NSString stringWithFormat:@"%@%d", mainString, [sender tag]];
Code works one then crashes.
both typeTo.text and mainString start as @""
And text alignment starts left.
What am I doing wrong?
If you are not using ARC, then you need to either retain the created string or create it with
alloc. So either:or better yet:
This of course means you also need to
releaseit before assigning a new value.The reason for the crash is likely because you assign the autorelease instance to the pointer, then the object gets autoreleased but the pointer still points to that now-dead object.
Another way would be to use a property with
retainorcopykeyword. For strings,copyis usually the better solution because you could accidentally pass aNSMutableStringand then later modify it.Edit to answer comments:
In this case, to avoid a memory leak, the following should be done:
The reason why this is necessary is because the
mainStringis used as an argument to create a new object, which is then in turn assigned tomainString. So before theinitWithFormat:line,mainStringpointed to a string object A. After that line, it now points to a new string object B. But you need to make sure to clean up A, which is why the autorelease is necessary. If you don’t you’d have a memory leak and eventually your app will run out of memory.Alternatively, you could also do:
The difference is that
autoreleasesays: I need this object for a short while, but some time after I leave this method it must be cleaned up if possible.releasesays: I don’t need the object any more, please clean it up now if possible.