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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T03:09:40+00:00 2026-05-24T03:09:40+00:00

I have an instance variable NSMutableArray* searchResults. First, I initialize it: self.searchResults = [[NSMutableArray

  • 0

I have an instance variable NSMutableArray* searchResults.

First, I initialize it:

self.searchResults = [[NSMutableArray alloc] init];

Then, at the course of my app, it gets reassigned frequently:

NSMutableArray* results = [searcher getResults];
self.searchResults = results;

My question is will this cause a memory leak because the array it pointed to previously may not get deallocated during this reassignment? Do I explicitly need to do this:

NSMutableArray* results = [searcher getResults];
self.searchResults = nil;
self.searchResults = results;

Of course, I release it in dealloc, and set it to nil in viewDidUnload.

  • 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-24T03:09:41+00:00Added an answer on May 24, 2026 at 3:09 am

    Well, you may have a memory leak, but not for the reasons you think. The answer depends on how your property was declared, and whether you’re releasing the object after you allocate it.

    When declaring a property that your object owns (as opposed to something like a delegate, which is owned by a different object), you typically declare it like this:

    @property (nonatomic, retain) NSMutableArray *searchResults;
    

    I’m assuming this is how your property is declared. The retain clause of that declaration means that an object will automatically be retained whenever it is assigned through this property’s setter method (i.e. either through self.searchResults = foo or [self setSearchResults:foo];).

    If the property already references an existing object, then the retain clause causes that existing object to be automatically released when the property gets reassigned. So setting self.searchResults = nil; prior to doing another assignment does indeed cause that existing object to be released, but this really isn’t necessary because self.searchResults = results; also releases any existing object before it retains and stores the new reference.

    You may have a memory leak on this line:

    self.searchResults = [[NSMutableArray alloc] init];
    

    After this line, your retain count is +2. It got +1 for the alloc, and +1 for the property assignment. This creates a memory leak, unless you have [self.searchResults release] elsewhere in your method. The way I’d fix it is like this:

    self.searchResults = [[[NSMutableArray alloc] init] autorelease];
    

    Whenever you’re confused about memory management, it’s always worthwhile to go back and reread the Memory Management Programming Guide.

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

Sidebar

Related Questions

If you have a Property that gets and sets to an instance variable then
I am allocating myMDD in main which contains an NSMutableArray instance variable (alloc/init-ed in
I have an instance variable lat (NSString) in my App Delegate. What i want
A simple question regarding multhreading programming: I have an NSMutableArray instance variable that is
I have this line of code (splits is an instance variable): splits = [[NSMutableArray
I have an instance variable called users defined as NSMutableArray . I use that
I have a custom class that has as an instance variable of a coordinate:
Let's say we have a class foo which has a private instance variable bar
I have a page-scoped component, which has an instance variable List with data, which
I need to mock a GrailsControllerClass interface. Instance should have a static variable defined.

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.