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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T20:13:36+00:00 2026-05-11T20:13:36+00:00

Here is the gist of some code I’m writing. I’m concerned that I am

  • 0

Here is the gist of some code I’m writing. I’m concerned that I am not properly addressing the retain/release issues with the array class method on NSMutableArray. Is the following actually leaking memory?

for(a while) { 
    // do stuff
    NSMutableArray *a = nil;
    // do stuff
    if (!a) {
        a = [NSMutableArray array];
    }
} // for(a while)
  • 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-11T20:13:36+00:00Added an answer on May 11, 2026 at 8:13 pm

    You wouldn’t leak memory in this code, and releasing the array yourself will cause a crash when the array is autoreleased at the end of the run loop.

    Most Cocoa classes provide a couple of ways of making a new object, and are very consistent with this convention:

    1. [[NSSomeObject alloc] init] : you are responsible for releasing the object (instance method).

    2. [NSSomeObject someObject] : the object will be autoreleased for you, usually at the end of the run loop (class method). It’s roughly equivalent to [[[NSSomeObject alloc] init] autorelease].

    The proper use of the instance method would be:

    a = [[NSMutableArray alloc] init];
    // do stuff
    [a release];
    

    The proper use of the class method would be:

    a = [NSMutableArray array];
    // do stuff, array is in the autorelease pool
    

    Note that Apple has recommended you stay away from the convenience methods as much as possible to improve performance. This is controversial advice, may not save much processor time, and separates the alloc-init from the release on an object you may not actually care much about keeping.

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

Sidebar

Related Questions

Here is my (censored) config/deploy.rb: https://gist.github.com/eedf6c976f632c1242cf For some reason, any changes to my assets,
Here is the code: create table `team`.`User`( `UserID` bigint NOT NULL AUTO_INCREMENT , `Username`
I found this https://gist.github.com/2945472 but I need an implementation that does not depend on
I have some code that is a result of pretty much my first effort
In my answer to my own question here I posted some code and @Dave
I had some 3D code that I noticed wouldn't render in a strict core
The script I am working with is here: https://gist.github.com/2759751 I want to modify some
Here's the gist of what I'm trying to do. I have a list of
here is the gist of the problem. If you want then you can read
I am having a little trouble trying to accomplish this. Here is the gist

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.