gpt4 book ai didi

iphone - 验证 Controller 问题

转载 作者:行者123 更新时间:2023-11-28 22:39:54 25 4
gpt4 key购买 nike

问题是:

Memory is never released; potential leak of memory pointed to by 'p'

在代码中,它发生在这个单行方法的返回上:

- (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;
}

最佳答案

在将 char * 转换为 NSString 之后,您应该 free(str):

- (NSString *)encodeBase64:(const uint8_t *)input length:(NSInteger)length
{
char *base64 = base64_encode(input, length);
NSString *ret = [NSString stringWithUTF8String:base64];
free(base64);
return ret;
}

关于iphone - 验证 Controller 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14829083/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com