Why does creating and removing SKShapeNode and SKNode repeatedly cause a memory leak?

前端 未结 4 531
迷失自我
迷失自我 2021-02-04 18:48

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

#import \"MyScene.h\"

@interface MyScene ()
@property (nonatomic,stro         


        
相关标签:
4条回答
  • 2021-02-04 19:06

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

    Not really a new idea, more of a repurposed one. This wonderful idea actually allowed me to use SKShapeNodes to my hearts content :)

    POOLING

    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.

    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;
    }
    

    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
    

    When you are done using the shape node, just return it to the pool and set the path to . For example :

    [pool addObject:shape];
    [shape removeFromParent];
    shape.path = NULL;
    

    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.

    0 讨论(0)
  • 2021-02-04 19:15

    I'm using iOS 7.1.1 and had the same problem. However, by setting the SKShapeNode's path property to nil before removing a shape from a parent did fix this issue - no more leaks.

    0 讨论(0)
  • 2021-02-04 19:18

    I've the same problem.

    Scene added one compound node (SKShape with SKSprite) then I removed this node from scene and added again(I got leak two nodes).

    My leak is fixed with recreation compound node after remove from scene

    0 讨论(0)
  • 2021-02-04 19:23

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

    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!

    Fire up instruments, and watch the memory grow! XD

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

    0 讨论(0)
提交回复
热议问题