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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T11:20:17+00:00 2026-05-24T11:20:17+00:00

@property (nonatomic,copy) NSString *orginalString; .. NSString *tmpString =[[NSString alloc] init]; self.orginalString=tmpString; [tmpString release]; NSString

  • 0
@property (nonatomic,copy) NSString *orginalString;

..
NSString *tmpString =[[NSString alloc] init];
self.orginalString=tmpString;
[tmpString release];
NSString *newString =self.orginalString;  

What happens here to newString?, is this correct what I am doing?

orginalString retain count 1 at first, and when it is referenced with another pointer “newString” its retain count will be 2? do I need to say “self.orginalString=nil” in the end? there are serious memory leaks but dont know it is something related with this.

  • 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-24T11:20:18+00:00Added an answer on May 24, 2026 at 11:20 am
      NSString *tmpString =[[NSString alloc] init];
    

    tmpString is allocated and initialized

      self.orginalString=tmpString;
    

    the string pointed to by tmpString is copied over to self.originalString (because the property is declared copy);

      [tmpString release];
    

    the string pointed to by tmpString is released, correctly; nothing happens to the string pointed to by self.orginalString;

      NSString *newString =self.orginalString;  
    

    a new pointer is created and initalized to point at the same string pointed to by self.orginalString; nothing happens to self.orginalString retain count; it’s just a second pointer pointing to the same object;

    at this point, if you don’t release somewhere self.orginalString, it will be leaked.

    When you are dealing with memory management in OBjective C, my suggestion is not try and reason in terms of “retain count”; retain count is just the mechanism that is used by the ObjC runtime to keep track of objects; it is too low-level and there are too many other objects around to increase or decrease the retain count, so that you immediately loose count.

    The best way, IMO, is reasoning in terms of ownership: when an object wants ownership of another, it will send a retain; when it has done with it, it sends release. Ownership is a local concept to a class, so it is easy to track down.

    So, when you do:

       NSString *newString =self.orginalString;  
    

    newString is just a pointer to a not-owned object; you do not need to balance that assignment with a release; on the contrary, if you do:

       NSString *newString = [self.orginalString retain];
    

    you are making yourself responsible for releasing the object when you have done with it.

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

Sidebar

Related Questions

If I have a class @interface Foo { NSString *temp; } @property(nonatomic, copy) NSString
I have an NSString declared as @property (nonatomic,copy) NSString *text in a class. When
I have a couple strings that I'm synthesizing: .h @property(nonatomic,copy) NSString* bio; .m @synthesize
I have the following problem: in header; GDataXMLDocument *doc; NSString *xmlBody; @property (nonatomic,copy) NSString
I've defined two properties with corresponding ivars and synthesized them: @property (nonatomic,copy) NSString* title;
What does nonatomic mean in this code? @property(nonatomic, retain) UITextField *theUsersName; What is the
I've been reading that if I have something like this: @property (nonatomic, assign) UIView
Do I have this right ... // Reactor.h @property(nonatomic, retain) NSMutableArray *reactorCore; // Reactor.m
This code works: monkey.h @interface monkey : NSObject { NSNumber *monkeyRanch; } @property (nonatomic,
NSString *statusValue; NSString *currentValue; @property(retain, nonatomic) NSString *statusValue; @property(retain, nonatomic) NSString *currentValue; @synthesize statusValue;

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.