Memory management SKShapeNode具有无限的内存增长

Memory management SKShapeNode具有无限的内存增长,memory-management,ios7,sprite-kit,Memory Management,Ios7,Sprite Kit,如果我在SKScene子类init方法中运行此代码 for (int i = 0; i < 100; i++) { SKShapeNode *shape = [SKShapeNode node]; shape.antialiased = NO; CGMutablePathRef path = CGPathCreateMutable(); CGPathAddEllipseInRect(pa

如果我在SKScene子类init方法中运行此代码

for (int i = 0; i < 100; i++) {

            SKShapeNode *shape = [SKShapeNode node];

            shape.antialiased = NO;

            CGMutablePathRef path = CGPathCreateMutable();

            CGPathAddEllipseInRect(path, NULL, CGRectMake(arc4random()%320, arc4random()%320, 10, 10));

            shape.path = path;

            [shape setStrokeColor:[UIColor blackColor]];

            CGPathRelease(path);

            [self addChild:shape];

            [shape removeFromParent];

        }
我的内存使用量不断增长,直到内存已满并崩溃。如果使用SKSpriteNode,则不会发生这种情况。有人能解决这个问题吗

小结:我创建了sprite工具包模板项目,添加了很多SKShapeNodes,并用一个新的替换了旧的SKScene


我向github添加了一个示例项目

您正在添加该形状,删除它之后,为什么

[self addChild:shape];

[shape removeFromParent]

我在阅读另一篇文章时意识到了这个问题。我把
SKShapeNode
搞砸了一点,确实验证了这里指出的内存泄漏问题

当我这么做的时候,我有了一个想法

这不是一个新的想法,更像是一个重新调整用途的想法。这个奇妙的想法实际上让我心满意足地使用了SKShapeNodes:)

联营

是的。。。我刚刚创建了一个SKShapeNodes池,可以根据需要重用。这有什么不同:)

只要在需要时使用return to your pool重新定义路径,它就会在那里等待您稍后再次使用

在调用的
SKScene
池中创建ivar或属性
NSMutableArray
,并在初始化
SKScene
时创建它。可以在初始化期间使用形状节点填充阵列,也可以根据需要创建它们

这是我创建的用于从池中获取新节点的快速方法:

-(SKShapeNode *)getShapeNode
{
    if (pool.count > 0)
    {
        SKShapeNode *shape = pool[0];
        [pool removeObject:shape];
        return shape;
    }

    // if there is not any nodes left in the pool, create a new one to return
    SKShapeNode *shape = [SKShapeNode node];

    return shape;
}
因此,无论在场景中的何处,您都需要使用SKShapeNode:

SKShapeNode *shape = [self getShapeNode];
// do whatever you need to do with the instance
使用完形状节点后,只需将其返回到池中并将路径设置为NULL,例如:

[pool addObject:shape];
[shape removeFromParent];
shape.path = NULL;

我知道这是一个解决方案,不是一个理想的解决方案,但对于希望使用大量SkshapeNode且不释放内存的任何人来说,这无疑是一个非常可行的解决方案。

如果您可以在一个最小的项目中隔离此问题,并验证此行为,然后将其作为一个bug报告给苹果,大概是为了以最简单的形式证明他所遇到的问题。
[pool addObject:shape];
[shape removeFromParent];
shape.path = NULL;