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

  • Home
  • SEARCH
  • 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 8691329
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T00:06:14+00:00 2026-06-13T00:06:14+00:00

While looking for an NSStack or similar I came across this code sample .

  • 0

While looking for an NSStack or similar I came across this code sample. In the pop method the author explicitly retains returnObject and then returns an autorelease object. Is this really necessary? I’m under the impression that the runtime will destroy the object removed from contents via removeLastObject at the end of a run-loop iteration (and not somewhere in the middle).

  • 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-13T00:06:15+00:00Added an answer on June 13, 2026 at 12:06 am

    Is this really necessary?

    It is if you’re still using manual memory management. Without that retain, returnObject could be deallocated as soon as you remove it from the array. A mutable array retains the objects that it contains and releases them upon removal. Retaining an object before removing it prevents the object from being deallocated; autoreleasing it allows the object to stick around long enough for the calling method to receive it.

    If you’re using ARC (and there aren’t many reasons not to), you don’t have to worry about any of that. The compiler will figure out when to retain and when to release for you.

    I’m under the impression that the runtime will destroy the object
    removed from contents via removeLastObject at the end of a run-loop
    iteration

    That’s what would happen if NSMutableArray autoreleased objects when it removes them, but I don’t see any reason to think that it does. You could count on the removed object being autoreleased if -removeLastObject returned the removed object, but it doesn’t. You should never make assumptions about what another object does with the objects it owns.

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

Sidebar

Related Questions

While looking through some old code I came across this gem: MyObject o =
While looking at the source code of the asyncore module I came across this
While looking through some example C code, I came across this: y -= m
While looking at some conceptual questions in C,I came across this question in a
While looking over the doc's for urldecode() I came across this note: The superglobals
While looking at the source code of System.ServiceModel.Channels.BufferManager, I noticed this method: void TuneQuotas()
While looking for sample code for Dropbox integration, i happened on this: https://github.com/jessegrosjean/DropboxSync This
I came across this question while looking for an answer, but it uses ctypes
While looking in the code of the method: Integer.toHexString I found the following code
While looking across the Django documentation, I came across the FormPreview . The description

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.