为什么创建和删除SKShapeNode和SKNode会反复导致内存泄漏? [英] Why does creating and removing SKShapeNode and SKNode repeatedly cause a memory leak?

查看:462
本文介绍了为什么创建和删除SKShapeNode和SKNode会反复导致内存泄漏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Xcode附带的sprite kit模板,我将场景修改如下:

Using the sprite kit template that comes with Xcode, I modify the scene to be as follows :

#import "MyScene.h"

@interface MyScene ()
@property (nonatomic,strong)SKNode *floor;
@end

@implementation MyScene

-(id)initWithSize:(CGSize)size {    
    if (self = [super initWithSize:size]) {
        /* Setup your scene here */
    }
    return self;
}

-(void)update:(CFTimeInterval)currentTime {
    /* Called before each frame is rendered */
    [self removeAllChildren];
    self.floor = nil;
    self.floor = [SKNode node];

    CGMutablePathRef path = CGPathCreateMutable();
    CGPathMoveToPoint(path, nil, 0, 10);

    for(int i = 2; i<self.frame.size.width; i+=2)
    {
        CGPathAddLineToPoint(path, nil, i, 10);
    }

    self.floor.physicsBody = [SKPhysicsBody bodyWithEdgeChainFromPath:path];
    SKShapeNode *shape = [SKShapeNode node];
    shape.path = path;
    shape.strokeColor = [UIColor redColor];

    [self.floor addChild:shape];
    [self addChild:self.floor];

    CGPathRelease(path); 
}

@end

该应用似乎继续使用更多内存,直到它挂起或崩溃(达到约180MB后)。使用泄漏和分配工具,我发现了以下内容:

The app seems to keep using more memory, until it either hangs or crashes (after reaching about 180MB). Using the leaks and allocations tools, I have found the following:

泄漏:

分配:

Leaks: Allocations:

从图像中可以看出,有大量使用内存的Malloc调用。我不直接调用Malloc - 似乎这些调用是由SpriteKit完成的。同样,存在许多内存泄漏,这似乎也是由于SKShapeNode,SKNode或其他Sprite Kit对象造成的。

As can be seen from the images, there are a large number of Malloc calls using memory. I do not call Malloc directly - it seems these calls are made by SpriteKit. Likewise, there are a number of memory leaks, which also seem to be due to SKShapeNode, SKNode or other Sprite Kit objects.

如何解决或解决此内存(泄漏)问题?我必须每帧创建SKShapeNodes和SKNodes。这段代码只是用来说明问题的一个例子 - 我的实际项目使用动态生成的路径要复杂得多(不像本例中那样静态)。

How do I work around or solve this memory(leak) problem? I have to create SKShapeNodes, and SKNodes every frame. This code is just a sample to illustrate the problem - my actual project is much more complex with dynamically generated paths (not static like the one in this example).

推荐答案

这是精灵工具包中的一个错误。问题不仅在于SKShapeNode,还在于SKPhysicsBody。

This is a bug in sprite kit. The problem isn't just with SKShapeNode, it is also with SKPhysicsBody.

使用CGPath创建物理主体或形状会导致内存泄漏。
您可以通过注释物理主体或形状,以及运行具有泄漏,分配和内存监视器的仪器来验证这一点。

Creating either a physics body, or a shape, using a CGPath causes a memory leak. You can verify this by commenting out either the physics body, or the shape, and running instruments with leaks, allocations and memory monitor.

即使您发布路径正确,精灵工具包不在内部。你无能为力!

Even if you release the path properly, sprite kit doesn't internally. Nothing you can do!

启动仪器,观察内存增长! XD

Fire up instruments, and watch the memory grow! XD

编辑:iOS 7.0中存在此错误。我是不是已经修复了7.1或更高版本,因为我已经停止使用SpriteKit因为这个bug。可以通过简单测试来验证它是否已修复。

This bug was present in iOS 7.0. Whether it has been fixed in 7.1 or later is not known to me as I stopped using SpriteKit because of this bug. One can verify if it is fixed by simply testing it.

这篇关于为什么创建和删除SKShapeNode和SKNode会反复导致内存泄漏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆