Sprite Kit泄漏没有任何意义 [英] Sprite Kit leaks don't make sense
问题描述
我一直在运行Instruments,以查看为什么我的SKScene不会取消分配并得到没有任何意义的泄漏". 泄漏"之一在场景中,该场景可以正确地分配并指向该场景:
I have been running Instruments to see why my SKScene won't deallocate and getting "leaks" that don't make any sense. One of the "leaks" is on a scene that properly deallocates and points to this:
border.path = path;
是引起泄漏的那一行,但下一行是:
As the line causing the leak but the very next line is:
CGPathRelease(path);
border.lineWidth = 1.0f;
border.strokeColor = [SKColor yellowColor];
[border setAlpha:0.0f];
[border runAction:[SKAction fadeAlphaTo:1.0f duration:0.2f]];
[self addChild:border];
因此您可以清楚地看到它已发布.我也因以下方法而泄漏":
So you can clearly see it's released.I am also getting "leaks" for methods like:
-(void)explosionShake{
//[self testTargets];
NSArray *objectArray = [self children];
for (SKNode *node in objectArray) {
[node runAction:[SKAction moveBy:CGVectorMake(0.0f, 10.0f) duration:.05] completion:^{
[node runAction:[SKAction moveBy:CGVectorMake(0.0f, -15.0f) duration:.05] completion:^{
[node runAction:[SKAction moveBy:CGVectorMake(0.0f, 5.0f) duration:.05]];
}];
}];
}
}
场景结束后,还有另一个我从其父对象中移除的对象.
After the scene ends, and also for another object which I remove from it's parent.
这些泄漏"是否可能是由于试图在已从其父节点移除的节点上执行操作而引起的?因为当"explosionshake"正在运行时,可以使用另一种方法将其中一个节点从其父节点中移除.那这种方法呢?
Could these "leaks" be caused by trying to run an action on a node that has been removed from it's parent? Because as "explosionshake" is running, one of the nodes may be removed from it's parent in another method. What about this method:
SKEmitterNode *testForExplosion = [NSKeyedUnarchiver unarchiveObjectWithFile:[[NSBundle mainBundle] pathForResource:@"explosionTest" ofType:@"sks"]];
//testForExplosion.position = location;
//[testForExplosion setParticleSpeed:self.frame.size.width/self.gameSpeed];
SKNode *node = [SKNode node];
[self addChild:node];
[node setPosition:location];
[node runAction:[SKAction moveBy:CGVectorMake(-self.frame.size.width, 0) duration:self.gameSpeed]];
[node addChild:testForExplosion];
SKAction *wait = [SKAction waitForDuration:4.0f];
SKAction *remove = [SKAction removeFromParent];
NSArray *array = [NSArray arrayWithObjects:wait,remove, nil];
SKAction *sequence = [SKAction sequence:array];
方法的第一行以红色突出显示,最后一行以绿色突出显示(这只是方法的一部分).我对为什么这些是泄漏"感到非常困惑,但这可能是为什么我的场景没有被释放的原因.
The first line in the method is highlighted in red and the last one in green (that's just a chunk of the method). I am pretty confused about why these are "leaks", but it's probably contributing to why my scene isn't getting deallocated.
如果有人可以给我指出为什么这些方法可能导致内存泄漏的提示,那将非常非常有帮助.当我的代码明确调用removeFromParent
If anybody could give me pointers as to why these methods could possibly be causing memory leaks, that would be very, very helpful. I have plenty more methods which are supposedly leaking SKCSprite
s, when my code clearly calls removeFromParent
推荐答案
1)
SKShapeNode
不是游戏中要使用的最可靠的类.我的意思是,最好将SKSpriteNode
与形状图像一起使用.如果仍然使用SKShapeNode
,请尝试以下代码.可能不起作用,请尝试一下.
1)
SKShapeNode
are not the most solid class to use in the game. I mean, better to use a SKSpriteNode
with a shape image. If still use SKShapeNode
, try code below. May not work, but give it a try.
- (void)dealloc
{
if(self.shapeNode){
[self.shapeNode setPath:NULL];
[self.shapeNode removeFromParent];
self.shapeNode = nil;
}
}
2)
显然,对节点有很强的参考.可能会这样重写:
2)
It's clear that there is a strong reference to the node. May rewrite like:
-(void)explosionShake
{
NSArray *objectArray = [self children];
for (SKNode *node in objectArray) {
// create a weak reference of the node
__weak typeof(node) weaknode = node;
// run action
[node runAction:[SKAction moveBy:CGVectorMake(0.0f, 10.0f) duration:.05] completion:^{
[weaknode runAction:[SKAction moveBy:CGVectorMake(0.0f, -15.0f) duration:.05] completion:^{
[weaknode runAction:[SKAction moveBy:CGVectorMake(0.0f, 5.0f) duration:.05]];
}];
}];
}
}
3)
第三部分似乎缺少一些代码,因为我看不到泄漏.
3)
The third section seem to have some missing code because I don't see a leak.
检查另一个线程,以获取有关如何使用完成块运行动作的示例.
SKAction执行选择器语法错误
希望这会有所帮助.
Check this other thread for an example of how to run an action with a completion block.
SKAction Perform Selector syntax error
Hope this helps.
这篇关于Sprite Kit泄漏没有任何意义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!