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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T20:31:10+00:00 2026-06-13T20:31:10+00:00

Apple’s NSCopying docs state that copyWithZone: returns an object that’s implicitly retained by the

  • 0

Apple’s NSCopying docs state that copyWithZone: returns an object that’s implicitly retained by the sender. So when that object is added to an NSMutableArray it seems like the object should be sent an autorelease message to keep the retain count balanced (since the array will retain the object).

So to deep copy the contents of one array to another I’d expect something like:

NSMutableArray *destination = [NSMutableArray array];

// assume MyObject adopts NSCopying
for (MyObject *obj in myArray) 
    [destination addObject:[[obj copy] autorelease]];

However I noticed a different approach in this answer. It seems like [ret addObject:[val copy]] is a memory leak. However I’m brand new to NSCopying so I thought I’d ask: When adding a copied object to an array should the object be sent an autorelease message to keep the retain count balanced?

Edit – more info: Clang reports a potential memory leak after removing the autorelease. Perhaps the linked answer assumes copy returns an object that’s not implicitly retained by the sender.

  • 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-13T20:31:11+00:00Added an answer on June 13, 2026 at 8:31 pm

    Yes it does need to be released but I wouldn’t use autorelease in a loop like that, do it manually with each iteration

       for (MyObject *obj in myArray)
        {
            MyObject *copy = [obj copy];
            [destination addObject:copy];
            [copy release];
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Apple seems to say that this image should have no extension. But how is
Apple developer document has the QTKit examples. I have noticed that it is only
Apple's multithreading docs don't list NSIndexPath as threadsafe or not! As an immutable class,
Apple's documentation for UIDocumentInteractionController presentOpenInMenuFromBarButtonItem:animated: method states that If there are no registered apps
Apple added the new IOSurface framework as part of the 10.6 Mac OS X
Apple Http Live Streaming Overview document clearly states that streaming videos exceeding 10 minutes
Apple says that there is a visibleRect method on the mac ( Source ),
Apple says that this is a good idea for saving memory. What would that
Apple suggests that prior to submitting to the Mac application store, the installation process
Apple seems only to have included a retina version of the iPhone 5 (4-inch)

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.