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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T02:26:29+00:00 2026-05-22T02:26:29+00:00

I’m not sure I understood how alloc and retain work. Recently I discovered that

  • 0

I’m not sure I understood how alloc and retain work.

Recently I discovered that the NSString properties were not retained and I had to add [myString copy] when I set them. Which makes me wonder if I misunderstood the whole way of using retain/alloc

Please, may someone tell me if I’m doing it correctly? I read a lot and had a look on open source projects, this let me thing that I may have been wrong since the beginning.

Here is my way of doing it:

/**** VIEW.h *****/
#import "MyClass.h"
@interface MyViewController : UIViewController  {
      //Is the following line really necessary?
    MyClass *myObject;
}
@property (nonatomic, retain) MyClass *myObject;
- (void)defineObject;
@end

.

/**** VIEW.m *****/
#import "VIEW.h"
@implementation MyViewController
@dynamic myObject;

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self defineObject];
    NSLog(@"My object's name is: %@", myObject.name);
}

- (void)defineObject
{
      //Here particularly, Why doesn't it work without both alloc and init
      //shouldn't "@property (nonatomic, retain) MyClass *myObject;" have done that already?
    myObject = [[MyClass alloc] initPersonalised];
    [myObject setName:@"my name"];
}

.

/**** MyClass.h *****/
@interface MyClass : NSObject {
      //not sure if this line is still necessary
    NSString *name;
}

@property (nonatomic, retain) NSString *name;

- (id)initPersonalised;
- (void)setName:(NSString *)name;
- (NSString *)name;

@end

.

/**** MyClass.m *****/
#import "MyClass.h"
@implementation MyClass

@dynamic name;

(id)initPersonalised{   
    self = [super init];
    name = @"Undefined";
}
- (void)setName:(NSString *)name{
    self.name = [name copy];
}

- (NSString *)name{
    return [self.name copy];
}

@end

I hope you can bring a bit of light, after months of programming this way, I’m less and less sure of doing it well.

  • 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-22T02:26:30+00:00Added an answer on May 22, 2026 at 2:26 am

    This is indeed a topic that every Objective C programmer stumbles upon. There are a few things one needs to know:

    Instance variable vs. property access

    Within MyViewController,

    myObject = xxx;
    

    and

    self.myObject = xxx;
    

    are two different things. The first directly assigns to the instance variable and does neither release to old referenced insance nor retain the newly assigned instance. The latter one uses the property setter and thus releases the old and retains the new value.

    Deallocation

    Even when you have declared an implemented a property that takes care of retaining and releases the values, it won’t take care of deallocation when your object (MyViewController in your case) is released. So you must explicitly release it in dealloc:

    -(void) dealloc {
      [myObject release];
      [super dealloc];
    }
    

    Now to your code:

    The snippet:

    myObject = [[MyClass alloc] initPersonalised];
    

    is perfectly okay. When you create an object, you use the pair of alloc and initXXX. The always create an instance with the reference count set to 1. So by directly assigning it to the instance variable, you create a clean constellation. I don’t see no other way of creating the instance.

    In MyClass you could use @synthesize name instead of @dynamic. Then the compiler would implement name and setName: automatically and you wouldn’t need to do it yourself.

    Finally, your missing dealloc.

    Update:

    If you use:

    self.myObject = [[MyClass alloc] initPersonalised];
    

    then you have a memory leak because initPesonalised sets the reference count to 1 and the setter of myObject increases it to two. If you want to use the setter, then I has to be:

    MyClass* mo = [[MyClass alloc] initPersonalised];
    self.myObject = [[MyClass alloc] initPersonalised];
    [mo release];
    

    It would be different if you weren’t using initXXX to create a new instance. The class NSString for example has many methods called stringXXX, which create a new instance (or return a shared one) that has (conceptually) a reference count of 1 that will later automatically decreased by one. Then you better use the setter:

    self.name = [NSString stringWithFormat: @"instance %d", cnt];
    

    If you want to use copy instead of retain for your string property (which is good practice), then you can simply declare your property like this:

    @property (nonatomic, copy) NSString *name;
    

    When you then use @synthesize to implement the getter and setter, the compiler will generate them using copy instead of retain.

    And NSString *name; is necessary even if you use @property and/or @synthesize to implement the property.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I want to count how many characters a certain string has in PHP, but

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.