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

查看:17
本文介绍了为什么创建和删除 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:

泄漏:分配:

从图片中可以看出,有大量的 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.

即使您正确释放路径,sprite kit 也不会在内部释放.你无能为力!

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

启动仪器,看着记忆增长!呵呵

Fire up instruments, and watch the memory grow! XD

这个错误出现在 iOS 7.0 中.我不知道它是否已在 7.1 或更高版本中修复,因为我因为这个错误而停止使用 SpriteKit.可以通过简单的测试来验证它是否已修复.

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天全站免登陆