问题是:
Memory is never released; potential leak of memory pointed to by 'p'
在Code中,它在返回此单行方法时发生:
- (NSString *)encodeBase64:(const uint8_t *)input length:(NSInteger)length
{
return [NSString stringWithUTF8String:base64_encode(input, (size_t)length)];
}
base64_encode功能:
char* base64_encode(const void* buf, size_t size)
{
static const char base64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
char* str = (char*) malloc((size+3)*4/3 + 1);
char* p = str;
unsigned char* q = (unsigned char*) buf;
size_t i = 0;
while(i < size) {
int c = q[i++];
c *= 256;
if (i < size) c += q[i];
i++;
c *= 256;
if (i < size) c += q[i];
i++;
*p++ = base64[(c & 0x00fc0000) >> 18];
*p++ = base64[(c & 0x0003f000) >> 12];
if (i > size + 1)
*p++ = '=';
else
*p++ = base64[(c & 0x00000fc0) >> 6];
if (i > size)
*p++ = '=';
else
*p++ = base64[c & 0x0000003f];
}
*p = 0;
return str;
}
答案 0 :(得分:2)
将free(str)
转换为char *
后,您应该NSString
:
- (NSString *)encodeBase64:(const uint8_t *)input length:(NSInteger)length
{
char *base64 = base64_encode(input, length);
NSString *ret = [NSString stringWithUTF8String:base64];
free(base64);
return ret;
}
答案 1 :(得分:0)
使用malloc
功能分配的内存必须与free
一起发布,您必须更改行
*p = 0;
到
free(p);