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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T21:34:35+00:00 2026-06-12T21:34:35+00:00

Line 1: First.h Line 2: @property (nonatomic, retain) NSString *name; Line 3: First.m Line

  • 0
Line 1: First.h
Line 2: @property (nonatomic, retain) NSString *name;

Line  3: First.m
Line  4: @synthesize name;
Line  5: -(void)viewDidLoad()
Line  6: {
Line  7:     name = [NSString stringWithString:@"Hello"];
Line  8:                          OR
Line  9:     self.name = [[NSString alloc]initWithString:@"Hello"]; 
Line 10:                          OR
Line 11:  }
Line 12:-(void)dealloc()
Line 13: {
Line 14: [name release];
Line 14: } 

Question 1:
If I follow line number 7 everything is fine and if I used line number 9 Memory leakage is there. As I know self is point to current object and if I used self.object or simply object no more difference.

Question 2: And if I used

@synthesize name = _name;

Then which one is for setting the value of name and which one is for get the value? Difference between:

name = [NSString stringWithString:@"Hello"];
            OR
self.name = [NSString stringWithString:@"Hello"];
            OR
_name = [NSString stringWithString:@"Hello"];

Question 3:
If I create any property is there any need to alloc in the memory as I allocated showing the memory leakage?

  • 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-06-12T21:34:37+00:00Added an answer on June 12, 2026 at 9:34 pm

    QUESTION 1: if I follow line number 7 everything is fine and if I used line number 9 Memory leakage is there. As I know self is point to current object and if I used self.object or simply object no more difference.

    Line  7:     name = [NSString stringWithString:@"Hello"];
    Line  8:                          OR
    Line  9:     self.name = [[NSString alloc]initWithString:@"Hello"]; 
    

    On line 7 you are using a convenience constructor which returns an autoreleased object and assigning the object directly to your name ivar; now, it is ok to assign an autoreleased object to a retain property, but it is incorrect to assign an autoreleased object directly to an ivar without also explicitly retaining it:

    name = [[NSString stringWithString:@"Hello"] retain];
    

    On line 9 you are using alloc/init which gives you a retained object: it is correct to assign such an object directly to an ivar, but you should autorelease it before assigning to a retain property, e.g.:

    self.name = [[[NSString alloc]initWithString:@"Hello"] autorelease]; 
    

    You can reason about this in terms of the object retain count:

    1. a convenience constructor will set the retain count to 1, but this will be later on automatically decreased by a release call done by the framework;

    2. alloc/init will give you a retain count of 1 that will not be decreased unless you explicitly call release;

    3. When an object retain count goes to 0, then it will be deallocated.

    Reasoning in terms of retain count is just a way to look at this whole matter of memory management and to understand what is going on deep in the framework; in no case this is a proper way to analyze your objects lifecycle, though.

    then which one is for setting the value of name and which one is for get the value? Difference between name = [NSString stringWithString:@"Hello"]; OR self.name = [NSString stringWithString:@"Hello"]; OR _name = [NSString stringWithString:@"Hello"];

    name = [NSString stringWithString:@"Hello"];
    

    and

    _name = [NSString stringWithString:@"Hello"];
    

    are just the same thing in the two cases given. This will bypass the property setter(/getter) method and assign directly to the ivar. In this case, your app would sooner or later crash because you are assigning an autoreleased object to the ivar directly. This would be correct:

    _name = [[NSString stringWithString:@"Hello"] retain];
    

    or

    _name = [[NSString alloc] initWithString:@"Hello"];
    

    Note that in a program where you declare your ivar as _name, you cannot use name to refer to it; you can use name to directly refer to the ivar if you declared only the property without explicitly specifying the ivar like you did for question 1 (in this case, name would be the ivar automatically generated by the compiler for you).

    On the other hand:

    self.name = [NSString stringWithString:@"Hello"]; 
    

    will use the property accessor method (actually, the setter); since your property is declared as retain, it is ok to assign to it the autoreleased variable returned by the convenience constructor stringWithString:.

    QUESTION 3: if I create any property is there any need to alloc in the memory as I allocated showing the memory leakage.

    This is not really clear to me.

    A good document where to read about the basic of memory management is: Memory Management Policy and also Practical Memory Management from Apple’s Advanced Memory Management Programming Guide.

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

Sidebar

Related Questions

I created custom UITableViewCell. @interface AnswerCell : UITableViewCell @property (nonatomic, retain) IBOutlet UILabel *textLabel;
Anyone can explain me what this line mean ? I use to see (nonatomic,retain)
The first line of bin/jekyll of the Jekyll gem uses the following syntax: $:.unshift
#!/bin/bash echo 'first line' >foo.xml echo 'second line' >>foo.xml I am a total newbie
I'm printing the first line from a file: with open(path,r,encoding='utf8') as f: for i,
session_start() should be the first line of web page. Dreamweaver's php templates should be
My GZipStream will only decompress the first line of the file. Extracting the contents
How do you echo only the first line of print print_r? More info: I
I was just reading this line: The first thing the format() method does is
Is it possible to get the first line of text from a UITextView. I

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.