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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T23:54:42+00:00 2026-05-11T23:54:42+00:00

I’m slowly teaching myself cocoa for the iPhone(through the Stanford Class on iTunes U

  • 0

I’m slowly teaching myself cocoa for the iPhone(through the Stanford Class on iTunes U) and I’ve just gone through the part on memory management, and I wanted to hopefully get some confirmation that the assumptions I’m making on how memory is handled and how [release] and [autorelease] work. Since memory management is a really basic and fundamental, but very essential part of the programming experience, I’d like to make sure I’m doing it right.

I understand that anything with an alloc, new, or copy needs to be released.
If I do this:

NSString *temp = [[NSString alloc] initWithString:@"Hello World"];

Then I need to add [temp release/autorelease] eventually, since I have an alloc.

However, if I do this:

NSString *temp = @"Hello World";

Then it doesn’t seem to need a release statement. Does the NSString class call autorelease automatically as part of the assignment?

Also, is there any difference between the two *temp objects here after these statements? They both contain the same string, but are there memory/usage ways where they differ?

Secondly, with properties, I’m assuming that the autorelease is handled automatically. If I have this:

@interface Person : NSObject
{
    //ivars
    NSString *firstName;
    NSString *lastName;
}

//properties
@property NSString *firstName;
@property NSString *lastName;

///next file

@implementation Person

@synthesize firstName;
@synthesize lastName;

- (void) dealloc
{

    //HERE!!!!

    [super dealloc];
}

I’m assuming I don’t need to add [firstName release] and [lastName release] (at //HERE!!!!), since that’s automatically handled by the properties. Is that correct?

I do understand that if I do this in code(assuming I’ve defined initWithFirstName):

Person *Me = [[Person alloc] initWithFirstName: @"Drew", lastName:"McGhie"];

that later I’m going to have to use [Me release/autorelease];

Any help confirming or correcting my understanding so far is greatly appreciated.

POST ANSWER WRITE-UP

I thought I’d write this all up after going over all the answers and testing out the suggestions and talk about what worked.

I do need to add the [firstName release], [lastName release], but I also need to add (retain) to the property descriptions. Not adding the (retain) caused warnings because it assumes (assign). Here’s how I finally set up the class

@interface Person : NSObject
    {
        //ivars
        NSString *firstName;
        NSString *lastName;
    }

    //properties
    @property (retain) NSString *firstName;
    @property (retain) NSString *lastName;

    ///next file

    @implementation Person

    @synthesize firstName;
    @synthesize lastName;

    - (void) dealloc
    {
        [firstName release];
        [lastName release];
        [super dealloc];
    }
  • 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-11T23:54:42+00:00Added an answer on May 11, 2026 at 11:54 pm

    The rule is simple: if you alloc, copy or retain, it’s your responsibility to release. If you didn’t, it’s not. However, if you need to rely on an object staying around, you have to retain (and subsequently release).

    We can treat the string literal according to the rules – you don’t need to release it because you don’t own it. That’s simple; there’s no need to worry about whether they’re special cases or not, just follow the rules and you’ll be fine.

    I wrote up a blog post with a collection of articles about the Cocoa memory management rules; I’d recommend following up some of the references.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I am trying to loop through a bunch of documents I have to put
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string

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.