Docs, debugDisposed for ImmutableBuffer (flutter/engine#26591)

This commit is contained in:
Dan Field
2021-06-04 16:14:01 -07:00
committed by GitHub
parent de064ab4b3
commit c32ac6533a
3 changed files with 58 additions and 4 deletions

View File

@@ -51,11 +51,13 @@ Future<void> createSingleFrameCodec() async {
);
final Codec codec = await descriptor.instantiateCodec();
_validateCodec(codec);
await codec.getNextFrame();
final FrameInfo info = await codec.getNextFrame();
info.image.dispose();
_validateCodec(codec);
codec.dispose();
descriptor.dispose();
buffer.dispose();
assert(buffer.debugDisposed);
_finish();
}
void _validateCodec(Codec codec) native 'ValidateCodec';

View File

@@ -2054,6 +2054,7 @@ Future<Codec> instantiateImageCodec(
targetHeight = descriptor.height;
}
}
buffer.dispose();
return descriptor.instantiateCodec(
targetWidth: targetWidth,
targetHeight: targetHeight,
@@ -2141,8 +2142,17 @@ void decodeImageFromPixels(
targetWidth: targetWidth,
targetHeight: targetHeight,
)
.then((Codec codec) => codec.getNextFrame())
.then((FrameInfo frameInfo) => callback(frameInfo.image));
.then((Codec codec) {
final Future<FrameInfo> frameInfo = codec.getNextFrame();
codec.dispose();
return frameInfo;
})
.then((FrameInfo frameInfo) {
buffer.dispose();
descriptor.dispose();
return callback(frameInfo.image);
});
});
}
@@ -5178,6 +5188,9 @@ class Shadow {
}
/// A handle to a read-only byte buffer that is managed by the engine.
///
/// The creator of this object is responsible for calling [dispose] when it is
/// no longer needed.
class ImmutableBuffer extends NativeFieldWrapperClass2 {
ImmutableBuffer._(this.length);
@@ -5194,9 +5207,39 @@ class ImmutableBuffer extends NativeFieldWrapperClass2 {
/// The length, in bytes, of the underlying data.
final int length;
bool _debugDisposed = false;
/// Whether [dispose] has been called.
///
/// This must only be used when asserts are enabled. Otherwise, it will throw.
bool get debugDisposed {
late bool disposed;
assert(() {
disposed = _debugDisposed;
return true;
}());
return disposed;
}
/// Release the resources used by this object. The object is no longer usable
/// after this method is called.
void dispose() native 'ImmutableBuffer_dispose';
///
/// The underlying memory allocated by this object will be retained beyond
/// this call if it is still needed by another object that has not been
/// disposed. For example, an [ImageDescriptor] that has not been disposed
/// may still retain a reference to the memory from this buffer even if it
/// has been disposed. Freeing that memory requires disposing all resources
/// that may still hold it.
void dispose() {
assert(() {
assert(!_debugDisposed);
_debugDisposed = true;
return true;
}());
_dispose();
}
void _dispose() native 'ImmutableBuffer_dispose';
}
/// A descriptor of data that can be turned into an [Image] via a [Codec].

View File

@@ -712,6 +712,15 @@ class ImmutableBuffer {
Uint8List? _list;
final int length;
bool get debugDisposed {
late bool disposed;
assert(() {
disposed = _list == null;
return true;
}());
return disposed;
}
void dispose() => _list = null;
}