SKPhysicsBody bodyWithPolygonFromPath memory leaks

2019-01-19 00:51发布

问题:

I have a strange memory leaks when creating Sprite Kit physics bodies with custom shapes. This is how my implementation looks:

CGFloat offsetX = self.frame.size.width * self.anchorPoint.x;
CGFloat offsetY = self.frame.size.height * self.anchorPoint.y;

CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path, NULL, 4 - offsetX, 3 - offsetY);
CGPathAddLineToPoint(path, NULL, 66 - offsetX, 3 - offsetY);
CGPathAddLineToPoint(path, NULL, 35 - offsetX, 57 - offsetY);
CGPathCloseSubpath(path);

self.physicsBody = [SKPhysicsBody bodyWithPolygonFromPath:path];

CGPathRelease(path);

Everything is going on inside SKSpriteNode method. Instruments tells me about several memory leaks after creating such bodies:

Leaked object: 
  Malloc 32 Bytes
Size:
  32 Bytes
Responsible Library: 
  PhysicsKit
Responsible Frame:
  std::__1::__split_buffer<PKPoint, std::__1::allocator<PKPoint>&>::__split_buffer(unsigned long, unsigned long, std::__1::allocator<PKPoint>&)

The CGPathRelease(path); line is necessary - without it I'm getting more memory leaks about CGPath which is understandable. When I'm using this implementation instead (for testing purposes):

CGFloat radius = MAX(self.frame.size.width, self.frame.size.height) * 0.5f;
self.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:radius];

...everything is working well, without memory leaks. I wonder if this is Sprite Kit bug or I'm doing something wrong.

回答1:

This is a bug in sprite kit. You will have to wait for a fix.



回答2:

The only restriction on the path parameter is:

A convex polygonal path with counterclockwise winding and no self intersections. The points are specified relative to the owning node’s origin.

I do not know the values of offsetX and offsetY so I do not know whether the path is correct or not, but assuming that they are both 0, it seems to me like this path is clockwise and not counterclockwise. I would create the path using constants and no variables, just to make sure that it is correct and if it is still leaking I would say it's a bug in PhysicsKit.



回答3:

As commented in several places, this looks like a bug in the implementation of SKPhysicsBody, which persists at least until iOS 7.1. The reason for this is:

SKPhysicsBody holds an instance variable, '_path', which holds a copy of the initial CGPathRef passed when calling 'bodyWithEdgeChainFromPath' or similiar constructors. This instance variable never gets released, so all paths will stay in memory.

However, you may implement a workaround for this by

(1) subclassing the SKShapeNode, that should hold the SKPhysicsBody,

(2) after creating and assigning the SKPhysicsBody for this node, retrieve the instance variable referring to the CGPathRef of the SKPhysicsBody,

(3) when the shape node is deallocated, check the retain count of the path. If it's > 0, release it and the memory leaks are gone.

There's little code overhead (beside subclassing all shape nodes, that use physics bodies relying on CGPath's). Just do this:

Add an instance variable to your subclass:

{
    CGPathRef myPath;
}

Implement the method to retrieve the value for this CGPath in any subclassed SKShapeNode implementation. You might also consider to add this as a general category on SKNode:

- (CGPathRef) getPhysicsBodyPath
{
    CGPathRef path = nil;
    if ( self.physicsBody ){
        object_getInstanceVariable(self.physicsBody, "_path", (void**) &path);
    }
    return(path);
}

This code will return the CGPathRef instance used by the node's physics body. Note, however, that this must be done immediately after assigning the physics body to the node. At a later time (i.e. in dealloc(), this might return a null value. So, after creating the body, store this value in the instance variable 'myPath'. To make this code work even after Apple might fix the bug, we'll add an additonal retain, which will make sure we can access this object once our SKNode is deallocated (see below):

    /*
     *  we need to keep a copy of the final path reference, that has been created for the physics body.
     *  retrieving this value during deallocation won't work any more...
     */
    myPath = CFRetain([self getPhysicsBodyPath]);

Finally, overwrite the 'dealloc' method and release the path, after your SKNode is released:

- (void) dealloc
{
    self.physicsBody = nil;
    [super dealloc];
    if ( myPath != nil ) {
        /* this will work, because we've retained the path for this instance */
        CFIndex rc = CFGetRetainCount (myPath);
        /* this is our own release ... */
        CGPathRelease(myPath);
        /* in case Apple has fixed the release, this is OK, otherwise we'll need
         * to release the path twice to avoid memory leaks
         */
        if ( rc > 1 ) {
            CGPathRelease(myPath);
        }
    }
}

This will finally release this path and fix the memory leaks. This code works for all iOS version up to 7.1 and it also should work on further versions, once Apple finally fixes this bug and SKPhysicsBoy actually releases the path (as they are supposed to do).



回答4:

I've also experienced similar memory leak, but it was fixed after I removed one line from my viewDidLoad function
skView.showsPhysics = YES;



回答5:

Does anything change if you do the following?

CGPathRef pathCopy = CGPathCreateCopy(path);
CGPathRelease(path);
self.physicsBody = [SKPhysicsBody bodyWithPolygonFromPath:pathCopy];
CGPathRelease(pathCopy);