Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit fd34a57

Browse files
committed
Hold on to dart-side refs for shader libraries
1 parent 32ccac8 commit fd34a57

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

lib/gpu/lib/src/shader_library.dart

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,26 @@ base class ShaderLibrary extends NativeFieldWrapperClass1 {
1818

1919
ShaderLibrary._();
2020

21+
// Hold a Dart-side reference to shaders in the library as they're wrapped for
22+
// the first time. This prevents the wrapper from getting prematurely
23+
// destroyed.
24+
Map<String, Shader> shaders_ = {};
25+
2126
Shader? operator [](String shaderName) {
2227
// This `flutter_gpu` library isn't always registered as part of the builtin
2328
// DartClassLibrary, and so we can't instantiate the Dart classes on the
2429
// engine side.
2530
// Providing a new wrapper to [_getShader] for wrapping the native
2631
// counterpart (if it hasn't been wrapped already) is a hack to work around
2732
// this.
28-
return _getShader(shaderName, Shader._());
33+
Shader? result = shaders_[shaderName];
34+
if (result == null) {
35+
result = _getShader(shaderName, Shader._());
36+
if (result != null) {
37+
shaders_[shaderName] = result;
38+
}
39+
}
40+
return result;
2941
}
3042

3143
@Native<Handle Function(Handle, Handle)>(

0 commit comments

Comments
 (0)