Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6181185
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T00:58:34+00:00 2026-05-24T00:58:34+00:00

The error I receive is as follows: int main(int argc, char *argv[]) { NSAutoreleasePool

  • 0

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:

  1. Why does this crash when I try to release theDescription using autorelease?

  2. Does my code create a memory leak?

This is my very first question on stack overflow! Thank you for your help!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T00:58:34+00:00Added an answer on May 24, 2026 at 12:58 am

    Why does this crash when I try to release theDescription using autorelease?

    Assuming you mean myString, it crashes because myString is 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 to myString are all autoreleased, so none of those objects are leaked.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having a problem where I receive this error: Exception in thread main java.lang.NullPointerException
The error I receive: SEVERE: A message body reader for Java class java.util.List, and
I receive this error when I click on the section of my Tab-Bar application
I receive the error below when running the following function. The catch says the
When I run cake bake all then I receive the error messages shown below.
I have an XML reader but I receive an error when I'm trying to
I'm trying to plotting a function but I receive an error and since I'm
While running the code I've included below I receive the error EntityCommandExecutionException was unhandled
I'm using Unity 2.1 in my application and receive the error in the title
I have the following code, I receive an error at if statement saying that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.