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
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:
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.
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).