1

I have an application that creates scene kit textures using the following:

        let texture = SKTexture(image: image)

The problem is that after several calls, the application crashes due to low memory.

I really only need the most recently created texture so when creating a new one, the preview ones' memory can be released, however, Scene Kit seems to be holding on to these textures even when they aren't being used anymore (the nodes are no longer part of the scene).

How can I manually tell Scene Kit to release the memory and cache of the texture so I don't get into the low memory crashing situation?

Also, I know that the SceneKit documentation recommends removing all references to the texture by removing nodes and such, but that should be happening and yet the Texture is not being released. Is there a good way of debugging and seeing what's holding on to the reference?

In re-reviewing the code, I'm creating a scene to hold the image to be rendered in the primary scene (whose node doesn't change). Basically, there is an object in the scene that can display an image or a video and that gets changed out by returning a Scene which is used as the material diffuse contents.

let texture = SKTexture(image: image)
let sprite = SKSpriteNode(texture: texture)
let scene = SKScene(size: sprite.size)
sprite.position = CGPointMake(CGRectGetMidX(scene.frame), CGRectGetMidY(scene.frame));
scene.addChild(sprite)

And then being used here:

primaryObjectMaterial.diffuse.contents = scene

I would expect once the scene's node material contents are changed, this scene (and the texture and image) would be released as there would no longer be any references to it but perhaps the caching system is hanging onto something or I'm doing something wrong?

Kudit
  • 4,212
  • 2
  • 26
  • 32
  • http://stackoverflow.com/questions/21025295/how-do-i-get-rid-of-sktextures – Oleksii Nezhyborets Aug 03 '16 at 22:38
  • I see where you put that the nodes are no longer a part of the scene but are you actually removing the nodes? – Timmy Sorensen Aug 03 '16 at 23:34
  • @Oleksii: that link recommends not using imageNamed which is what I'm doing. – Kudit Aug 04 '16 at 13:19
  • 1
    @Timmy: it's quite possible they're being held onto somewhere. How can I tell? I'm reusing the same nose but replacing the texture which I would think would release the previous textures via ARC since there should be no more references to them, but perhaps SceneKit is holding onto them somewhere? Perhaps I should hold a reference and somehow manually destroy the texture? – Kudit Aug 04 '16 at 13:20

1 Answers1

0

It's hard to give you an exact answer with out seeing your code or knowing how the app works but you need to take the node that the texture is on and remove it

nameOfNode.removeFromParentNode

This will remove it from memory and you need to add a new node with the new texture.

Timmy Sorensen
  • 570
  • 6
  • 16
  • The thing is the node isn't being removed. I'm just changing the contents of the material. The material content can be an image or a video which is why the material content is its own scene. Would it be better to hold on to a reference to that scene and somehow manually destroy it before creating the replacement scene? – Kudit Aug 04 '16 at 16:24