我正在尝试使用 RNCryptor 在 iOS 上加密和解密大文件(600+MB)。在github 上,我找到了有关如何在流上异步使用库的示例代码。此代码类似于 Rob Napier 关于同一主题的问题的答案。
然而,虽然我认为我正确地实现了代码,但该应用程序使用了高达 1.5 GB 的内存(在 iPad 6.1 模拟器中)。我认为代码应该阻止应用程序在内存中保留多个数据块?那么出了什么问题呢?
在我的控制器中,我创建了一个“CryptController”,我用加密/解密请求发送消息。
// Controller.m
NSString *password = @"pw123";
self.cryptor = [[CryptController alloc] initWithPassword:password];
//start encrypting file
[self.cryptor streamEncryptRequest:self.fileName andExtension:@"pdf" withURL:[self samplesURL]];
//wait for encryption to finish
NSDate *timeout = [NSDate dateWithTimeIntervalSinceNow:1];
do {
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode
beforeDate:timeout];
} while (![self.cryptor isFinished]);
在 CryptController 我有:
- (void)streamEncryptionDidFinish {
if (self.cryptor.error) {
NSLog(@"An error occurred. You cannot trust decryptedData at this point");
}
else {
NSLog(@"%@ is complete. Use it as you like", [self.tempURL lastPathComponent]);
}
self.cryptor = nil;
self.isFinished = YES;
}
- (void) streamEncryptRequest:(NSString *)fileName andExtension:(NSString *)ext withURL:(NSURL *)directory {
//Make sure that this number is larger than the header + 1 block.
int blockSize = 32 * 1024;
NSString *encryptedFileName = [NSString stringWithFormat:@"streamEnc_%@", fileName];
self.tempURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];
self.tempURL = [self.tempURL URLByAppendingPathComponent:encryptedFileName isDirectory:NO];
self.tempURL = [self.tempURL URLByAppendingPathExtension:@"crypt"];
NSInputStream *decryptedStream = [NSInputStream inputStreamWithURL:[[directory URLByAppendingPathComponent:fileName isDirectory:NO] URLByAppendingPathExtension:ext]];
NSOutputStream *cryptedStream = [NSOutputStream outputStreamWithURL:self.tempURL append:NO];
[cryptedStream open];
[decryptedStream open];
__block NSMutableData *data = [NSMutableData dataWithLength:blockSize];
__block RNEncryptor *encryptor = nil;
dispatch_block_t readStreamBlock = ^{
[data setLength:blockSize];
NSInteger bytesRead = [decryptedStream read:[data mutableBytes] maxLength:blockSize];
if (bytesRead < 0) {
//Throw an error
}
else if (bytesRead == 0) {
[encryptor finish];
}
else {
[data setLength:bytesRead];
[encryptor addData:data];
//NSLog(@"Sent %ld bytes to encryptor", (unsigned long)bytesRead);
}
};
encryptor = [[RNEncryptor alloc] initWithSettings:kRNCryptorAES256Settings
password:self.password
handler:^(RNCryptor *cryptor, NSData *data) {
//NSLog(@"Encryptor received %ld bytes", (unsigned long)data.length);
[cryptedStream write:data.bytes maxLength:data.length];
if (cryptor.isFinished) {
[decryptedStream close];
//call my delegate that i'm finished with decrypting
[self streamEncryptionDidFinish];
}
else {
readStreamBlock();
}
}];
// Read the first block to kick things off
self.isFinished = NO;
readStreamBlock();
}
当我使用分配工具进行分析时,我看到持续增长的分配类别是malloc 32.50 KB
、和。尤其是增长很大,超过 1 GB。负责的调用者是
负责的调用者是
。malloc 4.00 KB
NSConcreteData
NSSubrangeData
malloc 32.50 KB
[NSConcreteData initWithBytes:length:copy:freeWhenDone:bytesAreVM:]
NSConcreteData
-[NSData(NSData) copyWithZone:]
当我使用泄漏仪器进行分析时,没有发现泄漏。
我是 Objective-C 的新手,据我了解,新的 ARC 应该处理内存的分配和释放。在搜索任何与内存相关的内容时,我发现的所有信息都是假设您不使用 ARC(或者在撰写本文时它不存在)。我确定使用的是 ARC,因为当我尝试手动释放内存时会出现编译错误。
如果有人可以帮助我,将不胜感激!如果需要更多信息,我很乐意提供:) 另外,我是 StackOverflow 的新手,所以如果我忽略了我应该做的任何事情,请告诉我!