我已将我的项目转换为 ARC。后来我发现了一个与我正在加载的 PDF 相关的内存泄漏。实际发生的情况是,当我返回时,一旦 PDF 视图从我的导航控制器中弹出,内存就没有正确释放。然后我使用 Instrument 查看导致此内存泄漏的代码行。
在下文中,我已经解释了正在发生的事情。
首先,我检查我的 pdf 是否已加密(已加密)。然后我使用 StringEncryption Class 解密它
StringEncryption *crypto = [[StringEncryption alloc]init];
self.stringEncryption = crypto;
[self.stringEncryption setDelegate:self];
NSData *encryptedPDFData = [NSData dataWithContentsOfFile:pdfPath];
[crypto decryptUsingBGProcess:encryptedPDFData key:[key dataUsingEncoding:NSUTF8StringEncoding]];
decryptUsingBGProcess 方法如下
@autoreleasepool {{
CCOptions padding = kCCOptionPKCS7Padding;
NSMutableData *decryptedData =[self doCipher:plainText key:aSymmetricKey context:kCCDecrypt padding:&padding ];
if(!decryptedData)NSLog(@"0001 , EVT,%@, %@,Decryption failed ",[[self class] description],NSStringFromSelector(_cmd));
if(delegate && [delegate respondsToSelector:@selector(decryptionCompleted:)]){
if([delegate isKindOfClass:[PDFAnnotationSampleViewController class]] )
[((PDFAnnotationSampleViewController*)delegate) performSelectorOnMainThread:@selector(decryptionCompleted:) withObject:decryptedData waitUntilDone:NO];
else [delegate decryptionCompleted:decryptedData];
}
}
docyper方法如下
- (NSMutableData *)doCipher:(NSData *)plainText key:(NSData *)aSymmetricKey
context:(CCOperation)encryptOrDecrypt padding:(CCOptions *)pkcs7
{
CCCryptorStatus ccStatus = kCCSuccess;
// Symmetric crypto reference.
CCCryptorRef thisEncipher = NULL;
// Cipher Text container.
NSMutableData * cipherOrPlainText = nil;
// Pointer to output buffer.
uint8_t * bufferPtr = NULL;
// Total size of the buffer.
size_t bufferPtrSize = 0;
// Remaining bytes to be performed on.
size_t remainingBytes = 0;
// Number of bytes moved to buffer.
size_t movedBytes = 0;
// Length of plainText buffer.
size_t plainTextBufferSize = 0;
// Placeholder for total written.
size_t totalBytesWritten = 0;
// A friendly helper pointer.
uint8_t * ptr;
// Initialization vector; dummy in this case 0's.
uint8_t iv[kChosenCipherBlockSize];
memset((void *) iv, 0x0, (size_t) sizeof(iv));
//NSLog(@"doCipher: plaintext: %@", plainText);
NSLog(@"doCipher: key length: %d", [aSymmetricKey length]);
plainTextBufferSize = [plainText length];
//LOGGING_FACILITY(plainTextBufferSize > 0, @"Empty plaintext passed in." );
NSLog(@"pkcs7: %d", *pkcs7);
// We don't want to toss padding on if we don't need to
if(encryptOrDecrypt == kCCEncrypt) {
if(*pkcs7 != kCCOptionECBMode) {
if((plainTextBufferSize % kChosenCipherBlockSize) == 0) {
*pkcs7 = 0x0000;
} else {
*pkcs7 = kCCOptionPKCS7Padding;
}
}
} else if(encryptOrDecrypt != kCCDecrypt) {
NSLog(@"Invalid CCOperation parameter [%d] for cipher context.", *pkcs7 );
}
// Create and Initialize the crypto reference.
ccStatus = CCCryptorCreate(encryptOrDecrypt,
kCCAlgorithmAES128,
*pkcs7,
(const void *)[aSymmetricKey bytes],
kChosenCipherKeySize,
(const void *)iv,
&thisEncipher
);
//LOGGING_FACILITY1( ccStatus == kCCSuccess, @"Problem creating the context, ccStatus == %d.", ccStatus );
// Calculate byte block alignment for all calls through to and including final.
bufferPtrSize = CCCryptorGetOutputLength(thisEncipher, plainTextBufferSize, true);
// Allocate buffer.
bufferPtr = malloc( bufferPtrSize * sizeof(uint8_t) );
// Zero out buffer.
memset((void *)bufferPtr, 0x0, bufferPtrSize);
// Initialize some necessary book keeping.
ptr = bufferPtr;
// Set up initial size.
remainingBytes = bufferPtrSize;
// Actually perform the encryption or decryption.
ccStatus = CCCryptorUpdate(thisEncipher,
(const void *) [plainText bytes],
plainTextBufferSize,
ptr,
remainingBytes,
&movedBytes
);
//LOGGING_FACILITY1( ccStatus == kCCSuccess, @"Problem with CCCryptorUpdate, ccStatus == %d.", ccStatus );
// Handle book keeping.
ptr += movedBytes;
remainingBytes -= movedBytes;
totalBytesWritten += movedBytes;
// Finalize everything to the output buffer.
ccStatus = CCCryptorFinal(thisEncipher,
ptr,
remainingBytes,
&movedBytes
);
totalBytesWritten += movedBytes;
if(thisEncipher) {
(void) CCCryptorRelease(thisEncipher);
thisEncipher = NULL;
}
if (ccStatus == kCCSuccess)
cipherOrPlainText = [NSData dataWithBytes:(const void *)bufferPtr length:(NSUInteger)totalBytesWritten];
else
cipherOrPlainText = nil;
if(bufferPtr) free(bufferPtr);
return cipherOrPlainText;
}
// this added by David
- (NSData*) md5data: ( NSString *) str
{
const char *cStr = [str UTF8String];
unsigned char result[CC_MD5_DIGEST_LENGTH];
CC_MD5( cStr, strlen(cStr), result );
NSString* temp = [NSString stringWithFormat:
@"02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
result[0], result[1], result[2], result[3], result[4],
result[5], result[6], result[7],
result[8], result[9], result[10], result[11], result[12],
result[13], result[14], result[15]
];
return [NSData dataWithBytes:[temp UTF8String] length:[temp length]];
}
解密后使用委托调用decryptionCompleted:方法。该方法如下
-(void)decryptionCompleted:(NSData*)decryptedData{
/* PDF has decrypted successfully */
NSLog(@"0403 , EVT,%@, %@,Paper is Decrypted successfuly ",[[self class] description],NSStringFromSelector(_cmd));
NSMutableData *decryptedMData = [[NSMutableData alloc] initWithData:decryptedData];
APPDFDocument *pdfFile = [[APPDFDocument alloc] initWithPDFData:decryptedMData information:self.pdfInfo];
self.passwordEncrypted = [pdfFile isEncrypted];
NSLog(@"0404 , EVT,%@, %@,Paper is Decrypted paper has password encryption = %@ ",[[self class] description],NSStringFromSelector(_cmd),self.passwordEncrypted?@"YES":@"NO");
[self loadPDF:pdfFile];
}
这是显示内存泄漏发生位置的地方。请参阅分析器的以下屏幕截图。
(在这里我放大了问题区域) 可以帮助我找出问题所在。否则请指导我如何做进一步的分析