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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:18:29+00:00 2026-05-25T10:18:29+00:00

This is a general question about memory leaks. Let’s say you have the following

  • 0

This is a general question about memory leaks. Let’s say you have the following code:

NSObject *object = [[NSObject alloc] init];
NSArray *array = [[NSArray arrayWithObjects:object] retain];
[object release];
[array release];

Is that a memory leak? Like, would I have to enumerate through all the objects in the array and release them one by one before releasing the entire array? Or does NSArray’s dealloc method release all of the objects within it as well as releasing the array itself?

Thanks for any help! Memory management can be quite tricky.

  • 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-25T10:18:29+00:00Added an answer on May 25, 2026 at 10:18 am

    Here are some rules:

    • whenever you call alloc, you must eventually call release

    • for every retain, you should have a release

    When you add an object to an array, it calls retain on that object. If you don’t release your pointer to that object, it will be a leak. When you release the array, it will call release on all of the objects that it holds, since it called retain previously.

    NSObject *object = [[NSObject alloc] init]; 
    // object has retain count 1
    NSArray *array = [[NSArray arrayWithObjects:object] retain]; 
    // array is autoreleased but has a retain, so has retain count 1
    // object now has retain count 2
    [object release];
    // object now has retain count 1
    [array release];
    // array is now set to autorelease, 
    // once that happens, array will be sent dealloc and object will be released
    

    Hence no memory leaks.

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

Sidebar

Related Questions

I have a general question about data templates in WPF. Let's say I have
I have a general question about objects, memory, and retaining. I am not a
I have a general question about large in-memory objects and distributed computing. I have
Yesterday I asked this general question about decimals and their internal precisions. Here is
This is a general question about MVC as a pattern, but in this case
This is a general question about character encoding when using MD5 libraries in various
Ok this is a general question about how to solve this issue, not to
This question is about 64-bit Java apps in general, although I came upon it
This is a general question about backbone.js and javascript, I'm intermediate in javascript: If
This is not a theoretical or general question about when to use pass by

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.