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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T21:48:31+00:00 2026-05-15T21:48:31+00:00

This is probably a question that is more about object alloc/retain/release, but I’ll use

  • 0

This is probably a question that is more about object alloc/retain/release, but I’ll use NSString as an example. I’m aware that I can do:

NSString* myString = [[NSString alloc] initWithString:@"Test"];

to essentially allocate and initialize a string referenced by my variable myString which I should later call [myString release] upon. However, if after I do this, I set it to some other string such as:

myString = someOtherString;

does that essentially create a memory leak because I’ve reassigned my pointer to another object and lost all reference to the original one I allocated?

If I simply want to personally allocate and release a string and then change its value at various times, should I be using a different syntax other than ‘=’ or is that overloaded to properly change the contents of the object that is originally represented by myString when I use =.

  • 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-15T21:48:31+00:00Added an answer on May 15, 2026 at 9:48 pm

    Yes, in your example you are leaking the memory.

    NSString* myString = [[NSString alloc] initWithString:@"Test"];
    myString = someOtherString;
    

    You this should be done like so:

    NSString* myString = [[NSString alloc] initWithString:@"Test"];
    [myString release];
    myString = someOtherString;
    

    The best way I have heard the whole retain/release thing described is imagine you are walking a dog for someone. Your “object” is a dog and “retain” puts a leash on the dog and “release” takes a leash off. You can have as many leashes on the dog as you want but you want to keep at least one leash on the dog so he doesn’t run free (leak) and you want to take all the leashes off the dog once you get it back to the owner (you want to get rid of the object). In your example, you can think of it as letting go of the leash on the dog mid-walk and picking up the leash of a different dog.

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

Sidebar

Related Questions

This is probably a pretty basic question, but just something that I wanted to
This is probably a newb question so i'll come right out and say that.
Note: I realize that this is probably an open-ended question, so go ahead and
This is probably a multi-part question. Background: we have a native (c++) library that
This is probably a basic html/css question... I have a simple one-button form that
Warning: Note that this is a dumb question of something that I would probably
Im sorry for this probably dumm question, but I want to simply open modals
I'm developing an Android app (Android 1.6), but this is probably a more general
I thought about asking this question in the Mathexchange, but it is less about
Hey, so this is one of those questions that seems obvious, and I'm probably

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.