首页 > 解决方案 > 应用程序因 Objective-C 中的错误“Invalid pointer dequeued from free list”而崩溃

问题描述

当我们为 AES 加密/解密生成密钥时,我收到以下错误。

malloc: *** error for object 0x1c421b840: Invalid pointer dequeued from free list
*** set a breakpoint in malloc_error_break to debug

下面添加了生成密钥的代码:

- (NSData*)generateKey:(NSString*)childKey {
    unsigned char saltChar[16];
    for (int i=0; i<16; i++) {
        saltChar[i] = 0;
    }
    NSData *salt = [NSData dataWithBytes:saltChar length:16];
    size_t bufferSize =  kCCBlockSizeAES128;
    void *buffer = malloc(bufferSize);
    int result = CCKeyDerivationPBKDF(kCCPBKDF2,            // algorithm
                                      childKey.UTF8String,  // password
                                      [childKey lengthOfBytesUsingEncoding:NSUTF8StringEncoding],  // passwordLength
                                      salt.bytes,           // salt
                                      salt.length,          // saltLen
                                      kCCPRFHmacAlgSHA1,    // PRF
                                      65,         // rounds
                                      buffer, // derivedKey
                                      bufferSize*8); // derivedKeyLen

    NSLog(@"Unable to create AES key for password: %d", result);
    NSData *data = nil;
    if (result == kCCSuccess) {
        data = [[NSData alloc] initWithData:[NSData dataWithBytesNoCopy:buffer length:16]];
    }
    free(buffer);
    return data;
}

上面的代码出了什么问题,我对这个错误感到非常沮丧。请帮助任何人。

提前致谢。

标签: objective-ccommoncrypto

解决方案


推荐阅读