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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T14:55:55+00:00 2026-06-14T14:55:55+00:00

I want to execute a for loop that looks like this: for (id object

  • 0

I want to execute a for loop that looks like this:

 for (id object in [[MyClass methodReturningSet] allObjects]) {
      //do something
}

that methodReturningSet looks something like this:

- (NSSet *)methodReturningSet {
         MyObject *object1 = [[MyObject alloc] initWithCustomInfo:info1] autorelease];
         MyObject *object2 = [[MyObject alloc] initWithCustomInfo:info2] autorelease];
         MyObject *object3 = [[MyObject alloc] initWithCustomInfo:info3] autorelease];
         MyObject *object4 = [[MyObject alloc] initWithCustomInfo:info4] autorelease];

         return [NSSet setWithObjects: object1, object2, object3, object4, nil];
}

My question, is this safe in terms of memory management?

My current understanding is that ‘object’ will be sent a release message after the completion of the run loop.

My first question is, does the entire for loop execute within one single run loop?

My second question is, does it matter if the objects get sent release messages via the autorelease pool since the array we’re looping over holds a strong reference to all the objects it contains?

hope that was clear . . . any thoughts?

  • 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-14T14:55:57+00:00Added an answer on June 14, 2026 at 2:55 pm

    My first question is, does the entire for loop execute within one
    single run loop?

    Yes.

    My second question is, does it matter if the objects get sent release
    messages via the autorelease pool since the array we’re looping over
    holds a strong reference to all the objects it contains?

    The NSSet will retain the objects you pass to it. After adding the objects to it, you can release your own references if you don’t need them anymore. This can either be normal release calls after adding them to the set (see below), or autorelease calls, as you did it. When you release the NSSet, it will also release (and in this case dealloc) all its objects.

    You could use normal release messages to like this:

    - (NSSet *)methodReturningSet {
         MyObject *object1 = [[MyObject alloc] initWithCustomInfo:info1];
         MyObject *object2 = [[MyObject alloc] initWithCustomInfo:info2];
         MyObject *object3 = [[MyObject alloc] initWithCustomInfo:info3];
         MyObject *object4 = [[MyObject alloc] initWithCustomInfo:info4];
    
         NSSet *set = [NSSet setWithObjects: object1, object2, object3, object4, nil];
    
         [object1 release];
         [object2 release];
         [object3 release];
         [object4 release];
    
        return set;
    }
    

    That being said, consider using Automatic Reference Counting (ARC).

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

Sidebar

Related Questions

I have a scipt with a loop that looks a bit like this [CODE]
I want to execute an application (executable ending with an .exe extension ) that
I want to execute a program which takes in 1 text file(say like Text.exe),
I want to execute one mysql statement where I have a select that is
I have a for loop that I want (for every item in a ListBox
I have a situation where I have a callback that I want to execute
I have asp.net generated html code. I want execute jQuery click function when user
I want to execute a code helloword.cpp which takes in some argument from console
I want to execute a program in memory on Windows. I do not want
I want to execute a javascript on already loaded webpage in Webview. I am

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.