由于使用 AWS iOS 开发工具包的未捕获异常“NSInvalidArgumentException”而终止应用程序

Posted

技术标签:

【中文标题】由于使用 AWS iOS 开发工具包的未捕获异常“NSInvalidArgumentException”而终止应用程序【英文标题】:Terminating app due to uncaught exception 'NSInvalidArgumentException' using AWS iOS SDK 【发布时间】:2014-03-12 19:34:40 【问题描述】:

我在使用 ios SDK 将简单的分段上传到 S3 时遇到问题。无法确定未处理的参数异常发生在剩余大约 65 秒的后台时间([UIApplication sharedApplication].backgroundTimeRemaining)。这是日志中的错误:

2014-03-12 15:16:04.506 Test App[1247:60b] didSendData called: 32768 - 20938752 / 50553329
2014-03-12 15:16:05.308 Test App[1247:60b] didSendData called: 32768 - 20971520 / 50553329
2014-03-12 15:16:09.363 Test App[1247:60b] S3Response.m|-[S3Response connectionDidFinishLoading:]|182|Response:
2014-03-12 15:16:09.364 Test App[1247:60b] S3MultipartUploadOperation_Internal.m|-[S3MultipartUploadOperation_Internal request:didCompleteWithResponse:]|412|UploadPart succeeded: 4
2014-03-12 15:16:09.366 Test App[1247:4a0f] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** NSAllocateMemoryPages(4294967295) failed'
*** First throw call stack:
(0x3038cf03 0x3ab21ce7 0x3038ce45 0x30d180e5 0x30cef675 0x30cbcbe3 0x30cbcb5d 0x30cbcb0b 0x30ce48ef 0xbc6b3 0xbcdd7 0x3b00ad53 0x3b010689 0x3b0108dd 0x3b13bc17 0x3b13badc)
libc++abi.dylib: terminating with uncaught exception of type NSException

这是代码:

    #import "S3Upload.h"
    #import <Cordova/CDV.h>
    #import "S3Constants.h"

    NSString* cordovaCallback;
    NSString* clipName;
    NSString* clipPath;
    NSString* projectBucket;

    @interface S3Upload ()

    @property (nonatomic, strong) S3TransferOperation *uploadFileOperation;
    @property (nonatomic) UIBackgroundTaskIdentifier backgroundUpload;

    @end

    @implementation S3Upload

    - (void)upload:(CDVInvokedUrlCommand*)command
    
        cordovaCallback = command.callbackId;
        clipName = [command.arguments objectAtIndex:0];
        clipPath = [command.arguments objectAtIndex:1];
        projectBucket = [command.arguments objectAtIndex:2];

        NSLog(@"[S3Upload] Initializing the S3 Client");
        // Using a background thread
        [self.commandDelegate runInBackground:^

            // Initialize the S3 Client.
            AmazonS3Client *s3 = [[AmazonS3Client alloc] initWithAccessKey:ACCESS_KEY_ID
                                                             withSecretKey:SECRET_KEY];
            s3.endpoint = [AmazonEndpoints s3Endpoint:US_WEST_2];

            [AmazonLogger verboseLogging];
            NSLog(@"[S3Upload] Initializing the S3TransferManager");

            // Initialize the S3TransferManager
            self.tm = [S3TransferManager new];
            self.tm.s3 = s3;
            self.tm.delegate = self;

            NSLog(@"[S3Upload] UPLOADING: %@", clipName);

            self.uploadFileOperation = [self.tm uploadFile: clipPath bucket: projectBucket key: clipName];

            self.backgroundUpload = UIBackgroundTaskInvalid;
            self.backgroundUpload = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^
                NSLog(@"[S3Upload] Background handler called. Not running background tasks anymore.");
                NSLog(@"[S3Upload] Pausing the transfer");
                [self.uploadFileOperation pause];
                NSLog(self.uploadFileOperation.isPaused ? @"[S3Upload] Operation paused" : @"[S3Upload] Operation not paused");
                NSLog(self.uploadFileOperation.isFinished ? @"[S3Upload] Operation finished" : @"[S3Upload] Operation not finished");
                [[NSNotificationCenter defaultCenter] addObserver:self
                                                         selector:@selector(appBecameActive)
                                                             name:UIApplicationDidBecomeActiveNotification
                                                           object:nil];

                [[UIApplication sharedApplication] endBackgroundTask:self.backgroundUpload];
                self.backgroundUpload = UIBackgroundTaskInvalid;
            ];

            if (UIApplication.sharedApplication.applicationState != UIApplicationStateActive) 
                NSLog(@"[S3Upload] Background time remaining = %.1f seconds", [UIApplication sharedApplication].backgroundTimeRemaining);
            

        ];

    

    - (void)appBecameActive
    
        NSLog(@"[S3Upload] App became active again, checking if need to resume upload");
        NSLog(self.uploadFileOperation.isPaused ? @"[S3Upload] Operation paused" : @"[S3Upload] Operation not paused");
        NSLog(self.uploadFileOperation.isFinished ? @"[S3Upload] Operation finished" : @"[S3Upload] Operation not finished");
        if(!self.uploadFileOperation.isFinished && self.uploadFileOperation.isPaused)
            NSLog(@"[S3Upload] Resuming upload of %@", clipName);
            self.uploadFileOperation = [self.tm resume:self.uploadFileOperation requestDelegate:self];

            self.backgroundUpload = UIBackgroundTaskInvalid;
            self.backgroundUpload = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^
                NSLog(@"[S3Upload] Background handler called. Not running background tasks anymore.");
                NSLog(@"[S3Upload] Pausing the transfer");
                [self.uploadFileOperation pause];

                [[NSNotificationCenter defaultCenter] addObserver:self
                                                         selector:@selector(appBecameActive)
                                                             name:UIApplicationDidBecomeActiveNotification
                                                           object:nil];

                [[UIApplication sharedApplication] endBackgroundTask:self.backgroundUpload];
                self.backgroundUpload = UIBackgroundTaskInvalid;
            ];
        
    

    - (void)endBgTask:(CDVInvokedUrlCommand*)command
    
        CDVPluginResult* result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:@"bg task done"];
        [result setKeepCallbackAsBool:false];
        [self.commandDelegate sendPluginResult:result callbackId:command.callbackId];

        if (UIApplication.sharedApplication.applicationState != UIApplicationStateActive) 
            NSLog(@"[S3Upload] Background time remaining = %.1f seconds", [UIApplication sharedApplication].backgroundTimeRemaining);
        

        if (self.backgroundUpload == UIBackgroundTaskInvalid) 
            NSLog(@"[S3Upload] No Background task to invalidate");
            return;
        

        NSLog(@"[S3Upload] Invalidating background task");
        [[UIApplication sharedApplication] endBackgroundTask:self.backgroundUpload];
        self.backgroundUpload = UIBackgroundTaskInvalid;
        NSLog(@"[S3Upload] Background task invalidated");
    


    #pragma mark - AmazonServiceRequestDelegate


    -(void)request:(AmazonServiceRequest *)request didReceiveResponse:(NSURLResponse *)response
    
        NSLog(@"didReceiveResponse called: %@", response);
    

    -(void)request:(AmazonServiceRequest *)request didReceiveData:(NSData *)data
    
        NSLog(@"didReceiveData called");
    

    -(void)request:(AmazonServiceRequest *)request didSendData:(long long)bytesWritten totalBytesWritten:(long long)totalBytesWritten totalBytesExpectedToWrite:(long long)totalBytesExpectedToWrite
    
        NSLog(@"didSendData called: %lld - %lld / %lld", bytesWritten, totalBytesWritten, totalBytesExpectedToWrite);
    

    -(void)request:(AmazonServiceRequest *)request didCompleteWithResponse:(AmazonServiceResponse *)response
    
        NSLog(@"didCompleteWithResponse called: %@", response);
    

    -(void)request:(AmazonServiceRequest *)request didFailWithError:(NSError *)error
    
        NSLog(@"didFailWithError called: %@", error);
    

    -(void)dealloc
    
        [[NSNotificationCenter defaultCenter] removeObserver:self];
    

    @end

【问题讨论】:

您找到解决方案了吗?干杯 【参考方案1】:

看看这部分的崩溃:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** NSAllocateMemoryPages(4294967295) failed'

您正在尝试在设备的内存中分配 4294967295 字节,即 4GB。 NSAllocateMemoryPages

【讨论】:

以上是关于由于使用 AWS iOS 开发工具包的未捕获异常“NSInvalidArgumentException”而终止应用程序的主要内容,如果未能解决你的问题,请参考以下文章

自己定义Application的未捕获异常处理

由于名称为“OverlayView”的未捕获异常“NSInternalInconsistencyException”而终止应用程序

Swift:以 NSException 类型的未捕获异常终止

使用 CLLocationCoordinate2D 初始化获取 NSException 类型的未捕获异常

Swift 项目以 NSException 类型的未捕获异常终止

以 NSException 类型的未捕获异常终止 - 删除表视图行