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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T08:01:40+00:00 2026-05-18T08:01:40+00:00

We know about the complete pattern of alloc/init that alloc and init must be

  • 0

We know about the complete pattern of alloc/init that alloc and init must be combined.

NSObject *myObj = [[NSObject alloc] init];

1- init method receives the object from another source(not from a alloc,new,copy or alike or retained) so according to the fundamental memory management rule its not the owner and it must not release it. However, “Allocating and Initializing Objects / The Returned Object” article says that init could free the receiver.

How could this be possible when its against the fundamental rule?

2- Also, from the same article, init could return another object or nil. So, in this case, when we use the complete pattern of alloc/init, we could not release the object returned by alloc but we could only release the object returned from init and, init releases the object it received from alloc instead of us.

But init is not a alloc,new,copy or alike method so we must not release the object returned from it as it does not give us the ownership of object.

How could we release the object returned from init although this is against the fundamental rule?

3- Or, to adhere to the last paragraph of the same article, must we accept init method as a special case and use alloc/init pattern as an exception to the fundamental rule?

Memory Management Fundamental Rule:

  • You only release or autorelease objects you own.
    • You take ownership of an object if you create it using a method whose name begins with “alloc” or “new” or contains “copy” (for example, alloc, newObject, or mutableCopy), or if you send it a retain message.
    • You use release or autorelease to relinquish ownership of an object. autorelease just means “send a release message in the future” (to understand when this will be, see “Autorelease Pools”).
      http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmRules.html

Allocating and Initializing Objects / The Returned Object:

However, in some cases, this responsibility can mean returning a different object than the receiver. For example, if a class keeps a list of named objects, it might provide an initWithName: method to initialize new instances. If there can be no more than one object per name, initWithName: might refuse to assign the same name to two objects. When asked to assign a new instance a name that’s already being used by another object, it might free the newly allocated instance and return the other object—thus ensuring the uniqueness of the name while at the same time providing what was asked for, an instance with the requested name.

In a few cases, it might be impossible for an init… method to do what it’s asked to do. For example, an initFromFile: method might get the data it needs from a file passed as an argument. If the file name it’s passed doesn’t correspond to an actual file, it won’t be able to complete the initialization. In such a case, the init… method could free the receiver and return nil, indicating that the requested object can’t be created.

Because an init… method might return an object other than the newly allocated receiver, or even return nil, it’s important that programs use the value returned by the initialization method, not just that returned by alloc or allocWithZone:. The following code is very dangerous, since it ignores the return of init.

id anObject = [SomeClass alloc];
[anObject init];
[anObject someOtherMessage];

Instead, to safely initialize an object, you should combine allocation and initialization messages in one line of code.

id anObject = [[SomeClass alloc] init];
[anObject someOtherMessage];

http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/ObjectiveC/Articles/ocAllocInit.html

  • 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-18T08:01:40+00:00Added an answer on May 18, 2026 at 8:01 am

    The init method doesn’t receive the object; the object receives the init message. The object does not possess itself; rather, it always knows about itself (through the implicit self argument in every message).

    You’re right that the object does not own itself, though. If alloc and init were fused in a single new method, that method would be its own (super) caller, so it would own the object (until it returns) and so be unquestionably right in releasing it. Since they are separate, and init is not the caller of alloc, you’re right that it does not own the object, so you are right to question this practice.

    This is one of the few cases where it’s OK for one object to release an object (in this case, itself) on behalf of another. The alternative is not to release it, which, if you’re going to either return nil or throw an exception, will be a leak.

    In general, anytime you have an object retain or release itself, you should feel dirty. In this specific case, it’s OK, because you are preventing a bug (a leak) rather than probably creating one.

    2- Also, from the same article, init could return another object or nil. So, in this case, when we use the complete pattern of alloc/init, we could not release the object returned by alloc but we could only release the object returned from init and, init releases the object it received from alloc instead of us.

    But init is not a alloc,new,copy or alike method so we must not release the object returned from it as it does not give us the ownership of object.

    As init releases the old object on behalf of its caller, if it creates a new object, it does that on behalf of its caller. The caller does own the substitute object that init creates or retrieves for it.

    As a corollary to this, if init retrieves a previously existing object, it must retain that, so that the caller will own it.

    Again examining the hypothetical* new method, it would also need to both release the old object and create (owningly) or retain the substitute.

    In all of these cases, it’s init acting on behalf of its caller. It’s normally dodgy for one method to do another’s memory management, but for these cases, init doing it on behalf of its caller is necessary.

    *The new method does exist, but simply sends alloc and init, so there’s no need to implement it separately.

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

Sidebar

Related Questions

I know about SortedSet , but in my case I need something that implements
I know about Application.Exit() but what if I'm not in a windows forms application
I know about the HIG (which is quite handy!), but what programming practices do
I know about this question: Which (third-party) debug visualizers for Visual Studio 2005/2008 do
I know about code-behind files, but what is the best real-world way of Designers
I know about using a -vsdoc.js file for IntelliSense , and the one for
I know about the buttonMode property on a MovieClip (to get the hand cursor
I need to know about Epoll On linux System. Could you recommend manual or
All I know about the constraint is it's name ( SYS_C003415 ), but I
In C I know about the recursive function but I heard about the re-entrant

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.