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 3455960
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T09:40:41+00:00 2026-05-18T09:40:41+00:00

I am just reading through the Practical Memory Management guide. I am somewhat confused

  • 0

I am just reading through the Practical Memory Management guide.

I am somewhat confused by this block of code:

- (void)printHello {
    NSString *string;
    string = [NSString stringWithFormat:@"Hello"];
    NSLog(@"%@", string);
}

It seems to me that string is going to have a reference count of 0. Is this true?

What stops string from being deallocated before we call NSLog(string)?

Is this somehow equivalent to this:

- (void)printHello {
    NSString *string;
    string = [[[NSString stringWithFormat:@"Hello"] retain] autorelease];
    NSLog(@"%@", string);
}

Edit: Similarly this code is given in the Practical Memory Management
guide:

- (NSString *)fullName {
    NSString *string = [NSString stringWithFormat:@"%@ %@", firstName, lastName];
    return string;
}

When and how does the return value get freed? Who is the owner? Does the caller of fullName need to release the string returned by full name?

  • 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-18T09:40:42+00:00Added an answer on May 18, 2026 at 9:40 am

    Strictly speaking,

    - (void)printHello {
        NSString *string;
        string = [NSString stringWithFormat:@"Hello"];
        NSLog(@"%@", string);
    }
    

    Is not equivalent to

    - (void)printHello {
        NSString *string;
        string = [[[NSString stringWithFormat:@"Hello"] retain] autorelease];
        NSLog(@"%@", string);
    }
    

    The convention is that a method should autorelease any object it returns. The only exception (AFAIK) is for constructors, which return an object with a +1 retain count. Since [NSString stringWithFormat:] returns an object. In first snippet, stringWithFormat: returns an already autoreleased object. the second snippet, you’re retaining it once more and it’ll be released twice (which has the same effect, but the second retain/autorelease pair is redundant).

    Ok, now to answer your question. Essentially, every time UIKit calls your code, it creates an NSAutoreleasePool object. Every time you autorelease an object, its added to this pool. Finally, when your code returns back to UIKit, it calls the drain method on the pool (i.e [pool drain]) and that releases every object which has been added to the pool and deallocates the pool. Also, autorelease pools can be nested, so you can create your own pools and drain them if you’re going to be creating a lot of autoreleased objects. It isn’t as complicated as it sounds.

    I’d highly recommend that you read the Autorelease Pools chapter in the Memory Management Guide (Which incidentally, comes right after the Practical Memory Management chapter).

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

Sidebar

Related Questions

I noticed while reading through my code that I have a static member function
Reading through the SendAsync , BeginAsync method illustrations of Socket s, I realized that
OK thought I understood IDipose but just reading the best practices section of Accelerated
I'm reading through K&R and came to the small section on register variables, and
I would like to do an algebraic curve fit of 2D data points, but
in my goal to adopt better coding practices I have a few questions in
I tried creating a disembodied DOM element with jQuery (I avoid ever touching the
I am learning Python (I have a C/C++ background). I need to write something
I'm working on an application that does processing at what I'd call fairly high
I USED to be a developer and part-time db designer, but it's been many

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.