且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在iOS中正确释放内存:永远不会释放内存;指出的潜在的内存泄漏

更新时间:2023-11-18 12:52:22

您需要free个字节,因为NSData不拥有它的所有权:它不知道该数组是临时数组还是动态数组,因此它会复制它.

You need to free the bytes, because NSData does not take ownership of it: it cannot know if the array is a temporary or a dynamic, so it makes a copy of it.

要解决此问题,请替换

return [NSData dataWithBytes:bytes length:c];

使用

NSData *res = [NSData dataWithBytes:bytes length:c];
free(bytes);
return res;