3

For example in low memory conditions. I'm reading third party code and I saw this

NSMutableArray *array = [NSMutableArray array]; if (array != nil) { [array addObject:anObject]; } 

I've never check this after creating an array that way nor alloc+init but now I'm in doubt.

4
  • I have seen that as well, not checking for nil would return an "unrecognized selector". I am confused about this too. Commented May 14, 2013 at 10:58
  • instead of nil, you can check [array count] != 0 or if (array) Commented May 14, 2013 at 11:01
  • 2
    @hajder Messaging nil does not crash with unrecognized selector sent to instance .... Commented May 14, 2013 at 11:11
  • @H2CO3 I recall that defining the array like [NSMutableArray array] yielded that error. If I can reproduce I will post a new question about it. Commented May 14, 2013 at 12:06

3 Answers 3

4

Technically, it could return nil.

The probability of such a failure at that point is incredibly unlikely. Your app would probably crash or abort before a nil were returned. Consider it about as unlikely as malloc returning NULL.

My implementations have used checked object creation for years - I cannot remember ever seeing [NSMutableArray array] return nil in that time in development/testing.

Note that this answer is specific to [NSMutableArray array], not any/every initializer/constructor in existence. [NSMutableArray array] is nearly the most 'consistent' class of initializer/constructor in this regard because there is no reason for it to fail under normal circumstances.

Sign up to request clarification or add additional context in comments.

5 Comments

Yet not checking malloc() != NULL is always frowned upon, unlike this. Why that?
@H2CO3 good question :) minimum operating environment has virtual memory and a good chunk of memory, ability for the system to kill other tasks or your own, and the number of abstraction layers between the implementation and the client?
yeah, maybe. But I would be sooo happy if I didn't have to write my own safe_malloc() { return malloc() || abort() } every time... (especially in my latest project, where malloc() is a bottleneck, and I've literally never encountered it returning NULL...)
@H2CO3 at least you're using malloc in that case, and have the ability to implement your own allocators if needed. a good reason to check malloc is that there is an additional variable -- the size parameter.
That's meaningful. Yes, one cannot definitely alloc an arbitrary-sized memory chunk. Thanks for the explanation!
4

Yes. An object instantiation that fails for any reason should return nil unless the method states otherwise.

Comments

0

There is no need of that nil check. [NSMutableArray array] rarely returns nil. And if it gets to a (memory) situation that this method returns nil, your app is as good as killed.

The only possible case for that method fails is in tight memory constrained situations. iOS will never let memory goes down that much, without doing sufficient actions like memory warnings and purging background apps before.

Also in your example code, even if array is nil, you don't need to check for nil, since method calls on nil are ignored. I am only talking about your example code.

3 Comments

Could you link me a resource/doc to check that?
From my iOs development experience, if OS doesn't have enough memory to allocate an object, the app wouldn't run. Memory warning and background app purging will be already done to avoid such a situation. In any case, that nil check is not doing any help in such a memory constrained situation.
NSMutableArray can be backed by a mutable CFArray instance. CFArrays are created by __CFArrayInit(), which can return NULL. I think saying +[NSMutableArray array] never returns nil is unwarranted: it could if the allocator fails (which might not be just due to running out of memory).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.