dispose method

  1. @mustCallSuper
  2. @protected
  3. @visibleForTesting
void dispose()

Clears any retained resources that this layer holds.

This method must dispose resources such as ui.EngineLayer and ui.Picture objects. The layer is still usable after this call, but any graphics related resources it holds will need to be recreated.

This method only disposes resources for this layer. For example, if it is a ContainerLayer, it does not dispose resources of any children. However, ContainerLayers do remove any children they have when this method is called, and if this layer was the last holder of a removed child handle, the child may recursively clean up its resources.

This method automatically gets called when all outstanding LayerHandles are disposed. LayerHandle objects are typically held by the parent layer of this layer and any RenderObjects that participated in creating it.

After calling this method, the object is unusable.

Implementation

@mustCallSuper
@protected
@visibleForTesting
void dispose() {
  assert(!_debugMutationsLocked);
  assert(
    !_debugDisposed,
    'Layers must only be disposed once. This is typically handled by '
    'LayerHandle and createHandle. Subclasses should not directly call '
    'dispose, except to call super.dispose() in an overridden dispose  '
    'method. Tests must only call dispose once.',
  );
  assert(() {
    assert(
      _refCount == 0,
      'Do not directly call dispose on a $runtimeType. Instead, '
      'use createHandle and LayerHandle.dispose.',
    );
    _debugDisposed = true;
    return true;
  }());
  if (kFlutterMemoryAllocationsEnabled) {
    FlutterMemoryAllocations.instance.dispatchObjectDisposed(object: this);
  }
  _engineLayer?.dispose();
  _engineLayer = null;
}