Home Fixing or avoiding memory leak in default third party library
Reply: 1

Fixing or avoiding memory leak in default third party library

Xartec Published in 2017-11-14 22:34:39Z

I developed an app that includes the ability to preview the subdivision results of a 3D model on the fly. I have my own catmull clark subdivision functions to permanently modify the geometry, but I use the .subdivisionLevel property of the SCNGeometry to temporarily subdivide the model as a preview. In most cases previewing does not automatically mean the user will go for the permanent option.

.subdivisionLevel uses (just as MDLMesh’s subdivision, which I tried as a workaround) Pixar’s OpenSubdiv to do the actual subdivision and smoothing. It works faster than my own but more importantly it doesn’t permanently modify the vertex data I provide through a SCNGeometry source.

The problem is, I can’t get it to stop leaking memory. I first noticed this a long time ago, figured it was something in my code. I don’t think it’s just one specific IOS version and it happens in both Swift and Objective C. Eventually I set up a small example adding just 1 line to the SceneKit game template in Xcode, setting the ship’s subdivisionLevel to 1. Instruments shows that immediately results in memory leaks:

I submitted a bug report to Apple a week ago but I’m not sure I can expect a reply or a fix anytime soon or at all. The screenshot is from a test with a very small model, but even with small models (hundreds to couple of thousand vertices) it leaks a lot and fast and will lead to the app crashing.

To reproduce, create a new project in Xcode based on the SceneKit game template and add the following lines to handletap:

  if result.node.geometry!.subdivisionLevel == 3 {
     result.node.geometry!.subdivisionLevel = 0
  } else {
     result.node.geometry!.subdivisionLevel = 3

(Remove the ! For objective c) Tap the ship to leak megabytes, tap it some more and it quickly adds up.

OpenSubdiv is used in 3D Studio max as well as others obviously and it appears to be in Apple’s implementation. So my question is: is there a way to fix/avoid this problem without giving up on the subdivision features of SceneKit entirely, or is a response from Apple my only chance?

Xartec Reply to 2017-11-15 17:54:37Z

Going through the WWDC videos to get an idea of how committed Apple is to OpenSubdiv and thus the chance of them fixing the leaks, I found the subdivision can be performed on the GPU by Metal since the latest SceneKit update.

Here are the required two lines (Swift) if you want to use subdivision in SceneKit or Model IO:

let tess = SCNGeometryTessellator()
geometry.tessellator = tess

(from WWDC 2017 What's new in Scenekit, 23:45 into the video)

This will cause the subdivision to be performed on the GPU (thus faster, especially at higher levels), use less memory, and most importantly, releases the memory when setting the subdivision level lower or back to zero.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.324276 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO