diff --git a/src/MSIData.jl b/src/MSIData.jl index 1e88fa3..ed8851c 100644 --- a/src/MSIData.jl +++ b/src/MSIData.jl @@ -1532,9 +1532,12 @@ end function _iterate_uncompressed_fast(f::Function, data::MSIData, source::ImzMLSource, indices_to_iterate::Union{AbstractVector{Int}, Nothing}) spectrum_indices = (indices_to_iterate === nothing) ? (1:length(data.spectra_metadata)) : indices_to_iterate - # Intialize local buffers for this thread's sequential iteration - mz_buf = Vector{Float64}() - int_buf = Vector{Float64}() + # Initialize local buffers for this thread's sequential iteration. + # IMPORTANT: the element type must match the on-disk format. Using Float64 + # buffers here made `read!` consume twice as many bytes for Float32 datasets + # and misinterpret them as Float64 garbage (breaking use_mmap=false). + mz_buf = Vector{source.mz_format}() + int_buf = Vector{source.intensity_format}() for i in spectrum_indices meta = data.spectra_metadata[i] diff --git a/src/imzML.jl b/src/imzML.jl index a144893..f8071e2 100644 --- a/src/imzML.jl +++ b/src/imzML.jl @@ -559,8 +559,13 @@ function load_imzml_lazy(file_path::String; cache_size::Int=100, use_mmap::Bool= mz_is_compressed, int_is_compressed, global_mode) @debug "Metadata parsing complete. Saving cache for next time..." - # We create a temporary MSIData just for save_metadata_cache - tmp_source = ImzMLSource(ibd_handles, default_mz_format, default_intensity_format, mmap_data, any_comp) + # We create a temporary MSIData just for save_metadata_cache. + # IMPORTANT: do NOT share the real `ibd_handles` vector here. MSIData + # installs a finalizer that closes every handle in `source.ibd_handles`; + # when this temporary object is garbage-collected it would close the + # handles owned by the real dataset (breaking use_mmap=false reads with + # "Bad file descriptor"). The cache writer only needs the formats. + tmp_source = ImzMLSource(IO[], default_mz_format, default_intensity_format, nothing, any_comp) tmp_msi = MSIData(tmp_source, spectra_metadata, instrument_meta, (width, height), nothing, cache_size) save_metadata_cache(tmp_msi, cache_path) end