The error I receive is as follows:
int main(int argc, char *argv[])
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
int retVal = UIApplicationMain(argc, argv, nil, nil); //breakpoint that says Thread 1: Program Received Signal: "EXC_BAD_ACCESS".
[pool release];
return retVal;
}
My two questions can be found at the bottom of this post 🙂
I am currently working on an assignment for an iOS programming class and have hit a road bump.
I have found a fix, shown below, but it doesn’t make sense to me. Check it out:
@implementation MyClass
// This class method takes an (NSMutableArray *) and returns an NSString with its contents printed out.
+ (NSString *)myString:(NSMutableArray)anArray
{
// NSString *myString = [[NSString alloc] init]; OLD CODE THAT CAUSES MEMORY LEAK
NSString *myString = [[[NSString alloc] init] autorelease]; //NEW CODE THAT RELEASES FIRST ALLOCATION OF myString WHEN THE FIRST stringByAppendingFormat: IS CALLED
NSString *vp = VARIABLE_PREFIX; //#defined above to be @"%
for (id object in anArray) {
if ([object isKindOfClass:[NSString class]]) {
if ([object hasPrefix:vp]) {
myString = [myString stringByAppendingFormat:@"%@",[object substringFromIndex:1]];
}else{
myString = [myString stringByAppendingFormat:@"%@",object];
}
}else if ([object isKindOfClass:[NSNumber class]]) {
myString = [myString stringByAppendingFormat:@"%@",object];
}
}
return myString; //shouldn't I autorelease myString right before this line? NO NOT ANY MORE. THIS myString IS NOT THE ORIGINAL THAT I alloc-init, BUT AN AUTORELEASED OBJECT RETURNED BY THE stringByAppendingFormat: message.
}
When I try to send the message [myString autorelease];, the program crashes with the above error. It is working fine now as shown above, but I do not understand why.
Every time I send a message containing the "magic words" alloc, init, copy I have to call release, it don’t I? Or are the rules different in a Class method (can the Class itself own a file?). I do not call retain in the object that is calling this file.
Here are my two questions:
-
Why does this crash when I try to release
theDescriptionusingautorelease? -
Does my code create a memory leak?
This is my very first question on stack overflow! Thank you for your help!
Why does this crash when I try to release theDescription using autorelease?
Assuming you mean
myString, it crashes becausemyStringis already autoreleased. You got it by calling-stringByAppendingFormat:, which returns an autoreleased string. Now, you’re probably thinking: “But I created it by calling +alloc, so I should release it.” That’s true, but NSStrings are immutable, and when you call-stringByAppendingFormat:you get a different string back, and that string is autoreleased. Autoreleasing it a second time is an error.Does my code create a memory leak?
Yes, but not really. The “leaked” object is the empty string that you allocate in the beginning. You never release that string, so you’ve got a leak. However, NSString is apparently optimized so that
[[NSString alloc] init]returns a singleton, so in this particular case it doesn’t make any difference that the empty string isn’t released. The other strings that are assigned tomyStringare all autoreleased, so none of those objects are leaked.