@@ -9,7 +9,7 @@ import { Maybe } from '@/src/types';
99import { ImageMetadata } from '@/src/types/image' ;
1010import vtkImageData from '@kitware/vtk.js/Common/DataModel/ImageData' ;
1111import { defineStore } from 'pinia' ;
12- import { markRaw , reactive , ref } from 'vue' ;
12+ import { markRaw , nextTick , reactive , ref } from 'vue' ;
1313
1414/**
1515 * An internal cache of progressively loadable images.
@@ -37,7 +37,7 @@ export const useImageCacheStore = defineStore('image-cache', () => {
3737 const data = image . getVtkImageData ( ) ;
3838 // ProgressiveImage initializes with empty vtkImageData before actual data loads.
3939 // VTK.js volume renderer crashes on empty data (null scalar texture).
40- if ( ! data . getPointData ( ) . getScalars ( ) ?. getData ( ) ?. length ) return null ;
40+ if ( ! data ? .getPointData ( ) . getScalars ( ) ?. getData ( ) ?. length ) return null ;
4141 return data ;
4242 }
4343
@@ -116,19 +116,20 @@ export const useImageCacheStore = defineStore('image-cache', () => {
116116
117117 function removeImage ( id : string ) {
118118 if ( ! ( id in imageById ) ) return ;
119+ const image = imageById [ id ] ;
119120 unregisterListeners ( id ) ;
120121
121- // Release vtk data and any per-image caches (e.g. cine compressed frames
122- // and decoded-frame LRU). Without this, removing a dataset leaks all of
123- // its memory until the page reloads.
124- imageById [ id ] . dispose ( ) ;
125-
126122 const idx = imageIds . value . indexOf ( id ) ;
127123 if ( idx > - 1 ) imageIds . value . splice ( idx , 1 ) ;
128124 delete imageById [ id ] ;
129125 delete imageStatus [ id ] ;
130126 delete imageLoading [ id ] ;
131127 delete imageErrors [ id ] ;
128+
129+ // Vue tears down image consumers in its next update flush. Keep the VTK
130+ // object alive until those consumers have detached their actors and event
131+ // handlers, but make it unreachable from the cache immediately.
132+ void nextTick ( ( ) => image . dispose ( ) ) ;
132133 [ ...deletionCallbacks ] . forEach ( ( callback ) => callback ( [ id ] ) ) ;
133134 }
134135
0 commit comments