#10487·filament

Destroying a MaterialInstance after setConstant() over-releases its MaterialDefinition (use-after-free on Material destroy)

Author: PierfrancescoSoffrittiCreated Sep 28, 2026Updated Sep 28, 2026

Describe the bug Destroying a MaterialInstance that had setConstant() applied releases its material's MaterialDefinition from the MaterialCache, although the instance never acquired it. With one material and one specialized instance, the definition is freed when the instance is destroyed. Destroying the Material afterwards reads the freed definition and crashes. MaterialInstance::duplicate() of a specialized instance does the same: the copy releases the definition again when it is destroyed.

The cause, on v1.77.1 and on main (9dd8e75a13):

  • Material::Builder::build() is the only caller of MaterialCache::acquireMaterial() (filament/src/details/Material.cpp).
  • LocalProgramCache::terminate() is the only caller of MaterialCache::releaseMaterial() (filament/src/LocalProgramCache.cpp). It runs for the material (FMaterial::terminate()) and also for every instance whose own program cache is initialized (FMaterialInstance::terminate(), when mPrograms.isInitialized()).
  • An instance's cache is initialized by flushSpecializationConstants() → LocalProgramCache::initializeForMaterialInstance(), which doesn't acquire the definition. Neither does LocalProgramCache's copy constructor, which duplicate() uses.

So each specialized instance, and each duplicate of one, adds a release without a matching acquire. This seems to date from per-instance constants (#9774, first released in v1.71.4). Constants set on the material with Material::Builder::constant() are balanced and work.

To Reproduce

  1. A material that declares a constant: constants : [ { name : probe, type : bool, default : false } ].
  2. Run:
cpp
Material* m = Material::Builder().package(data, size).build(*engine);
MaterialInstance* mi = m->createInstance();
mi->setConstant("probe", true);
// render at least one frame with a renderable using mi (this flushes the pending constants)
engine->destroy(mi);  // releases the MaterialDefinition: its count reaches 0 and it is freed
engine->destroy(m);   // FMaterial::terminate() -> releasePrograms() reads the freed definition
  1. The app crashes in engine->destroy(m). Frames before that render correctly.

The same app with the constant set through Material::Builder::constant() instead of setConstant() shuts down cleanly, including five create/duplicate/destroy cycles of a material, its instances and duplicates.

Expected behavior Destroying a specialized instance, or a duplicate of one, releases only its own programs and specialization constants. The definition stays alive until the Material is destroyed.

A possible fix is to make the material the only owner of the definition's reference. This fixes both setConstant() and duplicate():

diff
--- a/filament/src/LocalProgramCache.cpp
+++ b/filament/src/LocalProgramCache.cpp
@@ void LocalProgramCache::terminate(FEngine& engine) {
     mMaterial->getDefinition().releasePrograms(engine, mCachedPrograms.as_slice(),
             mMaterial->getMaterialParser(), mSpecializationConstants.get(),
             mMaterial->isDefaultMaterial());
-    engine.getMaterialCache().releaseMaterial(engine, mMaterial->getDefinition());
 }
--- a/filament/src/details/Material.cpp
+++ b/filament/src/details/Material.cpp
@@ void FMaterial::terminate(FEngine& engine) {
     mPrograms.terminate(engine);
+    engine.getMaterialCache().releaseMaterial(engine, mDefinition);
 }

The alternative is to acquire the definition in initializeForMaterialInstance() and in LocalProgramCache's copy constructor and assignment. I haven't built Filament with this patch; it comes from reading the source, and it matches the crash below.

Screenshots N/A

Logs

Exception Type: EXC_BAD_ACCESS (SIGSEGV)
Exception Subtype: KERN_INVALID_ADDRESS at 0x0000000000000120
0  filament::MaterialParser::getCrc32() const + 12
1  void filament::(anonymous namespace)::releaseProgramsImpl<false>(filament::FEngine&, utils::Slice<filament::backend::Handle<filament::backend::HwProgram>>, filament::MaterialDefinition const&, utils::Slice<std::__1::variant<int, float, bool> const>, bool) + 96
2  <app renderer destructor, calling engine->destroy(material)>

Desktop (please complete the following information):

  • OS: macOS 26.5.1
  • GPU: Apple M5 Pro
  • Backend: Metal (Filament v1.77.1 prebuilt release). The code involved is backend-independent.

Smartphone (please complete the following information):

  • Device: N/A (not reproduced on a phone; the same Filament code ships in our Android and iOS builds)
  • OS: N/A

Additional context We want per-instance constants to compile weather effects out of the shaders while they're off, on low-end Android GPUs. Material-level constants already save about 2 ms per frame on a Galaxy A15 (Mali-G57, Vulkan) there. But switching an effect on the existing instances needs setConstant(), or a second copy of every material and of each instance's parameters.