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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T15:07:30+00:00 2026-05-16T15:07:30+00:00

after having spent few months in trying to master the syntax and rules, I

  • 0

after having spent few months in trying to master the syntax and rules, I am going deeper in memory management rules.
One thing I do not understand and causing me confusion is how one creates objects.

Based on what stated in apple memory management guide, the following is a valid approach:

– (NSArray *)sprockets {
    NSArray *array = [NSArray arrayWithObjects:mainSprocket,auxiliarySprocket, nil];
    return array;
}

because I am not causing any memory leaks.
The reason why is that it’s not using alloc for creating array and therefore sprockets is not the owner.
However I am wondering now what’s inside arrayWithObjects.
Because it happens that in my apps I often have factory for creating custom objects using something similiar to:

return [[MyObject alloc] initWithParameter:(id)params]; // possible leak

If I want to change with a static method like:

return [MyObject initWithParameter:(id)params];

what could be in initWithParameter for adhere to memory rules ? And what if MyObject extends some other object ? I also find out that method naming rules are important to properly advise programmer, what are this rule ?
Also could you point out a web link where this is explained (I am not yet good in finding docs on apple web site).

thanks

  • 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-16T15:07:31+00:00Added an answer on May 16, 2026 at 3:07 pm

    This is the page you’re looking for: Memory Management Rules. It all comes down to adhering to the rules of ownership for an object.

    If you create an object using alloc then you own that object and must release it. For example:

    NSString* str = [[NSString alloc] init]; 
    //do something with your str object 
    [str release];
    str = nil; //don't forget to set it to nil, it's still 
               //pointing to the (now unallocated) block of memory
    

    If you create an object using a factory method, for example:

    NSString* str = [NSString stringWithString:@"blah"];
    

    Then what is happening here is that it is creating the string object using alloc for you and then returning the object but first adding it to the local autorelease pool. To use the example in your question.

    return [[MyObject alloc] initWithParameter:(id)params]; // possible leak
    return [[[MyObject alloc] initWithParameter:(id)params] autorelease]; //now autoreleased -- no leak
    

    Here is the page on Autorelease Pools. Apple say it better than me.

    An autorelease pool is an instance of NSAutoreleasePool that “contains” other objects that have received an autorelease message; when the autorelease pool is deallocated it sends a release message to each of those objects. An object can be put into an autorelease pool several times, and receives a release message for each time it was put into the pool. Thus, sending autorelease instead of release to an object extends the lifetime of that object at least until the pool itself is released (the object may survive longer if it is retained in the interim).

    Method naming rules are important as it gives a strong indication as to whether the method returns an autoreleased object. In this example I would name the method:

    [MyObject myObjectWithParameter:(id)param]
    

    Hope this has helped.

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

Sidebar

Related Questions

I'm trying to experiment with xampp. After having some space issues, I wanted to
I'm having trouble trying to set the value of a property after i cast
After having read that QuickSilver was no longer supported by BlackTree and has since
After having looked at each of these two projects, it seems that both are
After having solved the problem of getting the editor to launch when there's a
After having a search around I could not find exactly what I want, I
After having this question answered through a link to an external site , I
After I upgraded to the beta, I'm having trouble with Html.RadioButtonList. Can someone show
I'm suddenly back to WinForms, after years of web development, and am having trouble
We're having a bizarre problem with Indy10 where two large strings (a few hundred

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.