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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:10:04+00:00 2026-05-23T15:10:04+00:00

To me, the following code looks like it will create a leak — I

  • 0

To me, the following code looks like it will create a leak — I might be wrong about this though:

-(NSString*) myString{

   NSString* foo  = @"bar";
   return foo;

}

My question is:

1) Will is create a memory leak as foo is not released?

2) If it IS a memory leak then how do I avoid it?

  • 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-23T15:10:04+00:00Added an answer on May 23, 2026 at 3:10 pm

    Short answer. This code will not give a leak.

    Long answer:

    With NSString it is not always visible leak, because of strings intern and because you do not call alloc/new/copy methods. But yes, this is a classic point of memory leak in general.

    There are two ways of dealing with it.

    • tracing all objects that you are returning from this (or similar) method and releasing them. That’s extremely buggy possibility and a very bad one almost every time.
    • returning autoreleased instance. In fact, you did something like it implicitly here. This string assignment is similar to:
    NSString *foo = [NSString stringWithString:@"bar"];

    And this one is similar to:

    NSString *foo = [[[NSString alloc] initWithString:@"bar"] autorelease];
    

    So, you will return an object, that has retain count 1, but is autoreleased. So, when NSAutoreleasePool will be drained, this object will go away.

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

Sidebar

Related Questions

Is it possible to transform following code to Linq that it will look like
I have code that looks like the following: //unrelated code snipped resolver.reset(new tcp::resolver(iosvc)); tcp::resolver::query
I have code that looks like the following: <form id=MyForm name=MyForm method=post action=index.php> <input
I have a simple assembly code file called exit.s that looks like the following:
There's a piece of code that looks like this. The problem is that during
EDIT2 Looks like I can't read properly, disregard this post, I will try to
I have a BUSINESS table that looks like this: BUSINESS_ID | BRN | CODE
I got following code from net and it looks everything is proper but i'm
I have the following code in Groovy 1.8.5 and to me it looks valid.
I've read the following post. My Code looks exactly the same, but does not

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.