AngelClient(38032,0x10a155340) malloc: *** error for object 0x600000016d40: Invalid pointer dequeued from free list
*** set a breakpoint in malloc_error_break to debug
给byte数组不足位补零操作,然后报上述错误
#pragma mark - 字符串转为长度为length的data,不足位自动补零
+(NSData *)stringToData:(NSString *)string length:(NSUInteger)length{
NSStringEncoding enc = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingUTF8); ;
NSData *data = [string dataUsingEncoding:enc];
Byte *byte = (Byte *)[data bytes];
//错误写法
for (int i = (int)data.length; i < length; i++) {
byte[i] = 0;
}
NSData * data16 = [[NSData alloc] initWithBytesNoCopy:byte length:length deallocator:^(void * _Nonnull bytes, NSUInteger length) {
}];
return data16;
}
正确的写法是
#pragma mark - 字符串转为长度为length的data,不足位自动补零
+(NSData *)stringToData:(NSString *)string length:(NSUInteger)length{
NSStringEncoding enc = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingUTF8); ;
NSData *data = [string dataUsingEncoding:enc];
Byte *byte = (Byte *)[data bytes];
//先开辟空间,然后重新给byte赋值
NSData * data16 = [[NSData alloc] initWithBytes:byte length:length];
Byte *byte1 = (Byte *)[data16 bytes];
for (int i = (int)data.length; i < length; i++) {
byte1[i] = 0;
}
return data16;
}