在创建具有自定义形状的 Sprite Kit 物理主体时,我有一个奇怪的内存泄漏。这就是我的实现的样子:
CGFloat offsetX = self.frame.size.width * self.anchorPoint.x;
CGFloat offsetY = self.frame.size.height * self.anchorPoint.y;
CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path, NULL, 4 - offsetX, 3 - offsetY);
CGPathAddLineToPoint(path, NULL, 66 - offsetX, 3 - offsetY);
CGPathAddLineToPoint(path, NULL, 35 - offsetX, 57 - offsetY);
CGPathCloseSubpath(path);
self.physicsBody = [SKPhysicsBody bodyWithPolygonFromPath:path];
CGPathRelease(path);
一切都在SKSpriteNode
方法内部进行。Instruments 在创建此类主体后告诉我一些内存泄漏:
Leaked object:
Malloc 32 Bytes
Size:
32 Bytes
Responsible Library:
PhysicsKit
Responsible Frame:
std::__1::__split_buffer<PKPoint, std::__1::allocator<PKPoint>&>::__split_buffer(unsigned long, unsigned long, std::__1::allocator<PKPoint>&)
这CGPathRelease(path);
条线是必要的 - 没有它,我会得到更多的内存泄漏,CGPath
这是可以理解的。当我改为使用此实现时(出于测试目的):
CGFloat radius = MAX(self.frame.size.width, self.frame.size.height) * 0.5f;
self.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:radius];
...一切运行良好,没有内存泄漏。我想知道这是 Sprite Kit 错误还是我做错了什么。