Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do we need to check that objects were actually allocated?

Tags:

objective-c

In most of the Objective-C examples I've been reading, as well in the Objective-C documentation, objects are always checked for successful completion before use.

For example:

MyObject *myObject = [[MyObject alloc] init];
if (myObject){
    //do stuff with the object
}
//stuff beyond here is probably not going to work if myObject is nil

Won't this make it hard to trace bugs? I'm not sure why allocation would fail other than low memory, but I've never seen a check like this done in Java (the language I'm migrating from) right after initializing an object.

I suppose it would make an app very convoluted if you checked every single allocation and tried to react to failed allocations. I guess you could show an error message to the user in many cases, but the number of branches in the program would probably double.

But in the examples I've seen, they don't do anything when allocation fails, beside skipping some code that would be necessary for the user to see what they expect on screen. It seems like if failed allocations really happen much at all in practice, and you're following this coding style, the result would be blank screens, unsaved documents that the user thinks got saved, corrupted data that the user and programmer are unaware of, etc.

EDIT: Here's an example from Apple's tutorial "Your Third iOS App: iCloud".

- (NSMetadataQuery*)textDocumentQuery {
    NSMetadataQuery* aQuery = [[NSMetadataQuery alloc] init];
    if (aQuery) {
        // Search the Documents subdirectory only.
        [aQuery setSearchScopes:[NSArray
                    arrayWithObject:NSMetadataQueryUbiquitousDocumentsScope]];

        // Add a predicate for finding the documents.
        NSString* filePattern = [NSString stringWithFormat:@"*.%@",
                    STEDocFilenameExtension];
        [aQuery setPredicate:[NSPredicate predicateWithFormat:@"%K LIKE %@",
                    NSMetadataItemFSNameKey, filePattern]];
    }

    return aQuery;
}
like image 714
Tenfour04 Avatar asked Sep 05 '13 03:09

Tenfour04


People also ask

How are objects memory allocated?

The memory for that object is allocated by the operating system. The name you declare for the object can then be used to access that block of memory. When you use dynamic memory allocation you have the operating system designate a block of memory of the appropriate size while the program is running.

What happens when memory is allocated?

Memory allocation is a process by which computer programs and services are assigned with physical or virtual memory space. Memory allocation is the process of reserving a partial or complete portion of computer memory for the execution of programs and processes.

How memory is allocated to the objects of a class?

In Java, when we only declare a variable of a class type, only a reference is created (memory is not allocated for the object). To allocate memory to an object, we must use new(). So the object is always allocated memory on the heap (See this for more details).


1 Answers

Initializers should return nil if initialization fails, this could for example happen if initWithFile: is given a non-existent path.

In Objective-C you can send messages to nil (unlike e.g. C++ where you can't call a method on NULL), so checking if (object) is not necessary before operating on that object. On the other hand, if you want to add the object to an array you should check that it is not nil:

SomeClass *anObject = [SomeClass alloc] initializerThatMightReturnNil]
[anArray addObject:anObject]

This would throw an exception, if anObject is nil

So to answer your question: It is generally not necessary to check for nil after every initialization, just make sure you understand what happens if the object is nil.

like image 157
Sebastian Avatar answered Oct 11 '22 12:10

Sebastian