EXC_BAD_ACCESS crash with .replace(withDrawables: ) on MaterialParameters.Value.textureResource

I get a crash every time I try to swap this texture for a drawable queue.

I have a DrawableQueue leftQueue created on the main thread, and I invoke this block on the main thread. Scene.usda contains a reference to a model cinema. It crashes on the line with the replace().


if let shaderMaterial = try? await ShaderGraphMaterial(named: "/Root/cinema/_materials/Screen", from: "Scene.usda", in: theaterBundle)  {
  if let imageParam = shaderMaterial.getParameter(name: "image"), case let .textureResource(imageTexture) = imageParam {
    imageTexture.replace(withDrawables: leftQueue)                                                            
  }
}

One weird thing, the imageParam has an invalid value when it crashes.

imageParam RealityFoundation.MaterialParameters.Value <invalid> (0x0)

Here is the stack trace is:

* thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, address=0x9)
    frame #0: 0x0000000191569734 libobjc.A.dylib`objc_release + 8
    frame #1: 0x00000001cb9e5134 CoreRE`re::SharedPtr<re::internal::AssetReference>::reset(re::internal::AssetReference*) + 64
    frame #2: 0x00000001cba77cf0 CoreRE`re::AssetHandle::operator=(re::AssetHandle const&) + 36
    frame #3: 0x00000001ccc84d14 CoreRE`RETextureAssetReplaceDrawableQueue + 228
    frame #4: 0x00000001acf9bbcc RealityFoundation`RealityKit.TextureResource.replace(withDrawables: RealityKit.TextureResource.DrawableQueue) -> () + 164
  * frame #5: 0x00000001006d361c Screenlit`TheaterView.setupMaterial(self=Screenlit.TheaterView @ 0x000000011e2b7a30) at TheaterView.swift:245:74
    frame #6: 0x00000001006e0848 Screenlit`closure #1 in closure #1 in closure #1 in closure #1 in TheaterView.body.getter(self=Screenlit.TheaterView @ 0x000000011e2b7a30) at TheaterView.swift:487
    frame #7: 0x00000001006f1658 Screenlit`partial apply for closure #1 in closure #1 in closure #1 in closure #1 in TheaterView.body.getter at <compiler-generated>:0
    frame #8: 0x00000001004fb7d8 Screenlit`thunk for @escaping @callee_guaranteed @Sendable @async () -> (@out τ_0_0) at <compiler-generated>:0
    frame #9: 0x0000000100500bd4 Screenlit`partial apply for thunk for @escaping @callee_guaranteed @Sendable @async () -> (@out τ_0_0) at <compiler-generated>:0
Answered by jnorris441 in 788087022

This was a case of swapping a texture that had already been swapped for a DrawableQueue. Also has to make sure to retain references to all of the RealityFoundation.MaterialParameters.Value inside my for loop so that they are not destroyed accidentally.

It also seems to be a race condition, because it will sometimes work fine.

Accepted Answer

This was a case of swapping a texture that had already been swapped for a DrawableQueue. Also has to make sure to retain references to all of the RealityFoundation.MaterialParameters.Value inside my for loop so that they are not destroyed accidentally.

EXC_BAD_ACCESS crash with .replace(withDrawables: ) on MaterialParameters.Value.textureResource
 
 
Q