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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T14:49:18+00:00 2026-05-12T14:49:18+00:00

This is a followup question from a previous question, which is hopefully a little

  • 0

This is a followup question from a previous question, which is hopefully a little clearer. I am just curious how the code presented below is working, specifically is the variable myString getting released. It does not look like it is from the output?

CODE

// IMPLEMENT
@implementation CelestialBody
- (void)setName:(NSString *)newName{
    if(name != newName) {
        [name release];
        name = [newName copy];
    }
}
- (void)dealloc{
    [name release];
    name = nil;
    [super dealloc];
}
@end

.

// ------------------------------------------------------------------- **
// MAIN: 30th September 2009
// ------------------------------------------------------------------- **

#import <Foundation/Foundation.h>
#import "CelestialBody.h"

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    CelestialBody *newPlanet = [[CelestialBody alloc] init];
    NSString *myString = [[NSString alloc]initWithFormat:@"go home"];
    NSLog(@"RetainCount_1: %d",[myString retainCount]);

    [newPlanet setName: myString];
    NSLog(@"RetainCount_2: Incremented by copy in setName");    

    // Clean up
    NSLog(@"RetainCount_2: %d -Before Release",[myString retainCount]);
    [newPlanet release];
    [myString release];
    [pool drain];
    NSLog(@"RetainCount_1: %d -After Release",[myString retainCount]);
    return 0;
}
// ------------------------------------------------------------------- **

OUTPUT

Running…
2009-10-01 09:28:50.395 RetainCount_1: 1
2009-10-01 09:28:50.399 RetainCount_2: Incremented by copy in setName
2009-10-01 09:28:50.399 RetainCount_2: 2 -Before Release
2009-10-01 09:28:50.400 RetainCount_1: 1 -After Release
Debugger stopped.

I am currently re-reading the Memeory Management Guide to try and see what I have missed.

many thanks

EDIT

Just added a the release to the dealloc, It looks like that was what I was missing.

- (void)dealloc{
    [name release];
    name = nil;
    [super dealloc];
}

gary

  • 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-12T14:49:18+00:00Added an answer on May 12, 2026 at 2:49 pm

    is the variable myString getting released.

    [myString release];
    

    All signs point to yes.

    It does not look like it is from the output?

    NSLog(@"RetainCount_2: %d",[myString retainCount]);
    [myString release];
    

    Your NSLog statement’s output doesn’t reflect the release message because the release message hasn’t happened yet.

    Also, don’t worry about retain counts. They can be very misleading. As long as you follow Cocoa’s rules and don’t create any ownership cycles (A owns B owns C owns A), you’ll rarely have a problem.

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

Sidebar

Related Questions

A followup to this previous question: Current code: var query = from b in
This is a follow-up question from here . The code is working great, it
This is a followup to my previous question. Parsing file names from a character
this is actually a followup from a previous question but it contains different information
This is a followup question of How to encode characters from Oracle to Xml?
This is a follow up from a question of mine that was just answered
This is a followup on the question: ASP.NET next/previous buttons to display single row
Note that this is a follow-up question from my previous one: How to memorize
All, this is a follow up from a previous question here: C# formatting external
Just a follow up from a previous question. I've got the following form.. <body

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.