3

I have a Sprite Kit game that I created in Xcode 5 and when profiling it for leaks using Instruments I see that there are indeed some leaks:

enter image description here

The problem is that I can't tell where in my application this is coming from as the "Responsible Frame" column doesn't point me to anywhere in my application.

How would one go about debugging/tracking the origins of this issue?

Update #1

There is only one file in which I'm interacting w/ CGPath but I am calling CGPathRelease

...
CGMutablePathRef path = CGPathCreateMutable();

CGPathMoveToPoint(path, NULL, 0, 0);
CGPathAddLineToPoint(path, NULL, size.width, 0);
CGPathAddLineToPoint(path, NULL, size.width, (upperCount * size.width));
CGPathAddLineToPoint(path, NULL, 0, (upperCount * size.width));

CGPathCloseSubpath(path);

upper.physicsBody = [SKPhysicsBody bodyWithPolygonFromPath:path];

CGPathRelease(path);
...        

Update #2

After toggling open the right panel in Instruments I was able to see the offending lines (although I'm still not sure is wrong here):

The first set of leaks... enter image description here

The second set of leaks... enter image description here

Kyle Decot
  • 20,715
  • 39
  • 142
  • 263
  • If I remember correctly, the CGPaths & Co are "old" and new CGRelease. – Larme Mar 11 '14 at 10:43
  • Use the static analyser to find them – Fogmeister Mar 11 '14 at 10:45
  • @Fogmeister I ran the Static Analyzer in Xcode but it returned w/ No Issues – Kyle Decot Mar 11 '14 at 10:51
  • @Larme I'm not sure what you mean. I updated my question w/ the place that I'm using a path but I *thought* I was releasing it correctly. Is this not what I'm looking for or am I doing it incorrectly? – Kyle Decot Mar 11 '14 at 10:54
  • With the CGPathRelease, it seems correct. I just assumed that with all the ARC-thing, you may have have forget to do this release manually. Did you use Memory Leak Instrument to know from what line it leaks? Show the panel at the right and double click on the leak. – Larme Mar 11 '14 at 11:10
  • Click the disclosure arrow to show a specific path instance that was leaked, the click the arrow to look at its retain/release history (use the record retain counts option if it's not set). Find the retain/create that's not paired... – Thomas Deniau Mar 19 '14 at 10:00
  • I have published a workaround to this problem here: http://stackoverflow.com/questions/20292318/why-does-creating-and-removing-skshapenode-and-sknode-repeatedly-cause-a-memory – Tommy356 Jul 27 '14 at 07:05

2 Answers2

2

Have you seen this? SKPhysicsBody bodyWithPolygonFromPath memory leaks

Looks like its a spritekit bug in SKPhysicsBody (I would guess it is being retained within bodyWithPolygonFromPath but not released).

Community
  • 1
  • 1
Edwin Iskandar
  • 4,119
  • 1
  • 23
  • 24
0

Hmm... I'm really not sure what is going on here. However, would it be possible to try it using UIBezierPath to create the path instead.

Just to test it...

CGMutablePathRef path = CGPathCreateMutable();

CGPathMoveToPoint(path, NULL, 0, 0);
CGPathAddLineToPoint(path, NULL, size.width, 0);
CGPathAddLineToPoint(path, NULL, size.width, (upperCount * size.width));
CGPathAddLineToPoint(path, NULL, 0, (upperCount * size.width));

CGPathCloseSubpath(path);

upper.physicsBody = [SKPhysicsBody bodyWithPolygonFromPath:path];

CGPathRelease(path);

Would become...

UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(0, 0)];
[path addLineToPoint:CGPointMake(size.width, 0)];
[path addLineToPoint:CGPointMake(size.width, (upperCount * size.width))];
[path addLineToPoint:CGPointMake(0, (upperCount * size.width))];
[path closePath];

upper.physicsBody = [SKPhysicsBody bodyWithPolygonFromPath:path.CGPath];

Just to see if that stops the leaks. If so... keep it :D

If you have to use the path multiple times then you can keep a reference to it so you can lazily load it and then only have to load it once.

Fogmeister
  • 76,236
  • 42
  • 207
  • 306
  • That didn't solve it (Although that looks a lot cleaner). The leaks still happen when moving to the initial point and when assigning the path to the physics body. – Kyle Decot Mar 11 '14 at 12:05
  • It sounds like it isn't the CGPath that is leaking. Not here anyway. – Fogmeister Mar 11 '14 at 13:01