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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T17:37:28+00:00 2026-05-19T17:37:28+00:00

I’ve read apple documentation to assert that I was doing the memory management correctly

  • 0

I’ve read apple documentation to assert that I was doing the memory management correctly but some questions raised.

Question 1:

They exhibit this code snippet as wrong :

in obj implementation:
- (NSString *)method1 {
    NSString *string = [[NSString alloc] initWithFormat:@"%@ %@", firstName, lastName];
    return string;
}

... later ...

NString* myString = [obj method1];
NSLog("%@", myString);
[myString release];

Apple Doc :

You own the string returned by alloc, but lose the reference to it before you get a chance to relinquish ownership. Following the memory management rules, this would result in a memory leak, since the caller has no indication that they own the returned object.

Since I’m taking care of releasing the object that has been allocated previously, there is no memory leak, right ? What do they mean by “lose the reference to it” ?

It is wrong only regarding apple’s memory management recommendations (the caller has no indication that they own the returned object) or this is also technically wrong ?

Question 2 :

This is about autoreleased objects availability :

Example code:

in obj1 implementation:
- (NSString *)methodA {
    NSString *string = [[NSString alloc] initWithFormat:@"%@ %@", firstName, lastName];
    return [string autorelease];
}

in obj2 implementation:
- (NSString *)methodB:(NSString *)inputString {
    NSLog("%@",inputString)
    //*other call of methods with arg inputString*//
}
... later ...

NString* myString = [obj1 methodA];

[obj2 method2:myString];

How far (or deep) following my functions calls will the autorelease object returned by obj1 will be available. Regarding apple’s documentations “Autorelease objects will be available within their variable scope”. Should I retain it at some point ?

  • 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-19T17:37:28+00:00Added an answer on May 19, 2026 at 5:37 pm

    Q1: It is wrong only regarding apple’s memory management recommendations (the caller has no indication that they own the returned object) or this is also technically wrong ?

    Technically this is correct as you release myString after using it. However if you follow Apples guidelines for method naming (strongly recommended), this is clearly wrong:

    You own any object you create.
    You “create” an object using a method whose name begins with “alloc” or “new” or contains “copy” (for example, alloc, newObject, or mutableCopy).

    method1 doesn’t contain create, alloc, new or copy – thus per the guideline the caller doesn’t own the object and doesn’t have to release it.

    Q2: How far (or deep) following my functions calls will the autorelease object returned by obj1 will be available. Regarding apple’s documentations “Autorelease objects will be available within their variable scope”. Should I retain it at some point ?

    Auto-released objects will be alive until the closest Autorelease Pool is drained, see Autorelease Pools:

    An autorelease pool is an instance of NSAutoreleasePool that “contains” other objects that have received an autorelease message; when the autorelease pool is deallocated it sends a release message to each of those objects. An object can be put into an autorelease pool several times, and receives a release message for each time it was put into the pool. Thus, sending autorelease instead of release to an object extends the lifetime of that object at least until the pool itself is released (the object may survive longer if it is retained in the interim).
    …
    The Application Kit automatically creates a pool at the beginning of an event cycle (or event-loop iteration), such as a mouse down event, and releases it at the end, so your code normally does not have to worry about them. There are three cases, though, where you might use your own autorelease pools:
    …

    So, if you need your instances to stay alive after the corresponding autorelease pool was drained, take ownership by retaining them. Otherwise you usually just let them be handled by the pool.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
Seemingly simple, but I cannot find anything relevant on the web. What is the
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I need to clean up various Word 'smart' characters in user input, including but
I'm looking for suggestions for debugging... If you view this site in Firefox or
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

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.