Can I trust the Cocoa API to not fail, or do I need to fully check?

Based on the background of Symbian, I am somewhat concerned about the apparent lack of error handling in Cocoa. Cocoa has many methods that, as far as I see, do not have error messages and can still fail.

For example, if the NSMutableString appendString has a void return type and does not throw exceptions (at least the documentation does not mention)? Of course, if I add a line that is long enough, in theory I might run out of memory. Is it paranoid for me to check the length of an NSMutableString before and after adding to verify that the application is working?

+3
source share
2 answers

My test is on Mac OS X, and I think you're talking about the iPhone platform.

The fact is, I don’t see how returning an error from the appendString method will help, because the platform is at this point in such a state that it cannot satisfy the malloc requests for your process.

To get around this problem, you can probably malloc your own address space and use this managed process memory as storage for your lines. I think Carbon CFString (free paid up to NSString) allows you to use your own memory allocator.

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[]) 
{
    NSAutoreleasePool * pool = [NSAutoreleasePool new];
    NSMutableString * m = [NSMutableString stringWithCapacity:100000];

    int i;
    for(i=0;i<1000000;i++)
        [m appendString:@"ABCDE..."]; //1400 characters long
    [pool release];
}



cd:tmp diciu$ ./a.out 
a.out(2216) malloc: *** mmap(size=1220067328) failed (error code=12)
*** error: can't allocate region
*** set a breakpoint in malloc_error_break to debug
[..]
2009-08-13 16:45:44.163 a.out[2216:10b] *** Terminating app due to uncaught exception     'NSMallocException', reason: 'Out of memory. We suggest restarting the application. If you     have an unsaved document, create a backup copy in Finder, then try to save.'
2009-08-13 16:45:44.165 a.out[2216:10b] Stack: (
    2494541803,
    2485014075,
    2435399864,
    2494157025,
    2494172776,
    2434276628
)
Trace/BPT trap
+6
source

I modified the diciu code for testing NSMutableString appendStringto make the error faster and try to catch any exceptions. Here is the main section:

    NSMutableString * m = [[NSMutableString alloc] initWithCapacity:1000];
    NSMutableString * n = [[NSMutableString alloc] initWithCapacity:1000];
    @try {
        [m appendString:@"1234567890"];
        [n appendString:m];
        int i;
        for(i=0;i<100;i++) {
            [m appendString:n];
            [n appendString:m];
        }
    }
    @catch (id exception) {
        NSLog(@"!!Exception");
    }
//    @catch (NSException *exception) {
//        NSLog(@"!!Exception: %@", [exception name]);
//    }
    @finally {
        [m release];
        [n release];
        NSLog(@"Finally...");
    }

:

Mac OS X 10.5.8 Xcode 3.1.3. iPhone 3.0. (NSException, NSMallocException). , , NSMallocException .

iPhone 3.0.1: .

, iPhone , .

, , !

, - .

+2

Source: https://habr.com/ru/post/1715073/


All Articles