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

  • SEARCH
  • Home
  • 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 3596396
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T19:58:58+00:00 2026-05-18T19:58:58+00:00

I just discovered the following: As I expected, releasing my object before I return

  • 0

I just discovered the following:
As I expected, releasing my object before I return it causes the app to crash:

   + (NSString *)descriptionOfExpression:(NSArray *)anExpression {
    NSMutableString *expressionDescription;
    expressionDescription = [[NSMutableString alloc] init];  
    for (id object in anExpression) {
    //Do stuff to expressionDescription
    }

    [expressionDescription release];
    return expressionDescription;
}

However, I did not expect that the following would cause a memory leak:

    + (NSString *)descriptionOfExpression:(NSArray *)anExpression {
    NSMutableString *expressionDescription;
    expressionDescription = [[NSMutableString alloc] init];  
    for (id object in anExpression) {
    //Do stuff to expressionDescription
    }

    return expressionDescription;
    [expressionDescription release];
}

Ultimately, my solution was to do this, instead:

    + (NSString *)descriptionOfExpression:(NSArray *)anExpression {
    NSMutableString *expressionDescription;
    expressionDescription = [[NSMutableString alloc] init];  
    for (id object in anExpression) {
    //Do stuff to expressionDescription
    }

    [expressionDescription autorelease];
    return expressionDescription;
}

I understand why autoreleasing it works, but how is a leak being caused by releasing after the value is returned?

My second question is very related: Are memory leak detection systems always right?

I realize that the programmers who developed Instruments and the Build and Analyze feature of XCode are far more experienced on this matter than I am, so for now I will assume that they are always right. However, I have difficulty understanding how a program like Instruments can “know” that memory is being leaked. I think it should depend entirely on how long I, the programmer, want to use an object.

This is my understanding of what a “leak” is:

Human definition: Memory is being leaked if I have memory allocated when I am not using it.

Programming Definition Using Counts: Memory is being leaked when memory is allocated but there are no active objects with a retain count on the object in question.

Programming Definition Using Accessibility: Memory is being leaked when there is allocated memory that can not be reached from wherever I am in the program right now.

  • 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-18T19:58:58+00:00Added an answer on May 18, 2026 at 7:58 pm

    The problem with your second block is that no code is run after return. I would have expected Xcode to warn you about that (look at and try to fix your warnings, as well as errors)

    Your understanding of leaks is correct. Build and Analyze can be fooled — it relies on coding conventions being followed. If you stray from that, B&A won’t know (or will flag leaks that aren’t true).

    The Leak detector instrument inserts code into your program to check your Accessibility definition. It can possibly be fooled by casting, but if you just do fairly straightforward allocations, assignments, and releases, I would take everything it flags very seriously unless you are absolutely sure it is wrong.

    When you return an object that you allocate, do one of the following

    1. Call autorelease on it before returning it — in this case the caller is responsible for retaining it if it wants it longer. An autoreleased object is released after the entire callstack is unwound back to the iOS call that called you — that’s where the “pool is drained” — you should retain it before returning back to iOS.

    2. Name your message allocSOMETHING or newSOMETHING and don’t call autorelease. In this case, your message is understood to be returning an object with a retain count of one and the caller is responsible for releasing it (or autoreleasing it).

    If you do either of these things, Build and Analyze will understand and help you get it right.

    EDIT: added newSOMETHING based on comment

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

Sidebar

Related Questions

I just discovered the following: As I expected, releasing my object before I return
I just discovered that the following code executes without error (Chrome): console.log(fa); function fa(){}
I just discovered that if I do the following: Click the button that animates
I have just discovered that my app doesn't run in 4.3 even though the
I have a Rails app where users can follow one another. I've just discovered
I just discovered a very strange behavior with Type.GetInterface and nested Types. The following
I was just fooling around in Xcode and I discovered that the following statement
I've just discovered that Oracle lets you do the following: SELECT foo.a, (SELECT c
I just ran the Leaks tool in Instruments and discovered the following leaks: basically
I just discovered zen-coding. I have the following snippet of code I want to

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.