SKShapeNode的内存增长无限 [英] SKShapeNode has unbounded memory growth
问题描述
如果我在SKScene子类初始化方法中运行此代码
If I run this code in a SKScene sublass init method
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];
}
每次我在SKView控制器中运行此代码
and everytime I run this code in my SKView controlling controller
SKView * skView = (SKView *)self.view;
// Create and configure the scene.
SKScene * scene = [ZTMyScene sceneWithSize:skView.bounds.size];
scene.scaleMode = SKSceneScaleModeAspectFill;
// Present the scene.
[skView presentScene:scene];
我的内存使用量一直在增长,直到内存已满并崩溃。如果我使用SKSpriteNode,则不会发生这种情况。有没有人解决这个问题?
my memory usage grow until the memory is full and crashes. This does not happen if I use a SKSpriteNode. Has anyone a fix for this?
总结:我创建了精灵工具包模板项目,添加了很多SKShapeNodes并用新的替换旧的SKScene。
Summary: I create the sprite kit template project, add a lot of SKShapeNodes and replace the old SKScene with a new one.
我向github添加了一个示例项目 https://github.com/ zeiteisen / MemoryTest
I added a sample project to github https://github.com/zeiteisen/MemoryTest
推荐答案
我在阅读另一篇文章时意识到了这个问题。我用 SKShapeNode
搞砸了一下,确实验证了这里指出的内存泄漏问题。
I became aware of this issue while reading another post. I messed around with SKShapeNode
a bit and indeed verified the memory leak issue pinpointed here.
在这样做时,我有一个想法...
While doing that, I had an idea...
不是一个新想法,更多是一个改变用途的想法。这个奇妙的想法实际上允许我使用SKShapeNodes到我心中的内容:)
Not really a new idea, more of a repurposed one. This wonderful idea actually allowed me to use SKShapeNodes to my hearts content :)
POOLING
是的......我刚创建了一个SKShapeNodes池,我根据需要重用了它。这有什么区别:) :)
Yep... I just created a pool of SKShapeNodes that I reused as needed. What a difference that makes :)
您只需在需要时重新定义路径,使用返回游泳池完成后,它将等待您玩游戏稍后再次。
You simply redefine the path whenever needed, when done using return to your pool, and it'll be waiting there for you to play with again at a later time.
在 SKScene中创建一个ivar或属性
NSMutableArray
/ code>调用pool并在初始化 SKScene
时创建它。您可以在初始化期间使用形状节点填充数组,也可以根据需要创建它们。
Create a ivar or property NSMutableArray
in your SKScene
called pool and create it when you init the SKScene
. You can either populate the array with your shape nodes during init, or you can create them as needed.
这是我创建的用于从中获取新节点的快速方法游泳池:
This is something quick method I created for grabbing a new node from the pool :
-(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,你都可以这样做:
So wherever in the scene you need a SKShapeNode you'd do this :
SKShapeNode *shape = [self getShapeNode];
// do whatever you need to do with the instance
当你完成使用shape节点,只需将其返回到池并将路径设置为NULL,例如:
When you are done using the shape node, just return it to the pool and set the path to NULL, for example :
[pool addObject:shape];
[shape removeFromParent];
shape.path = NULL;
我知道这是一种解决方法而不是理想的解决方案,但对于任何人来说,这肯定是一种非常可行的解决方法想要使用大量的SKShapeNodes而不是出血记忆。
I know it's a workaround and not an ideal solution, but certainly this is a very viable workaround for anyone wanting to use a large number of SKShapeNodes and not bleed memory.
这篇关于SKShapeNode的内存增长无限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!