使用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

最佳答案

看一下崩溃的这一部分:

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

您试图在设备的内存中分配4294967295字节(4GB)。
NSAllocateMemoryPages

关于ios - 使用AWS iOS SDK由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22362015/

10-12 14:35
查看更多