diff --git a/mzLib/Readers/timsTOF/FrameProxy.cs b/mzLib/Readers/timsTOF/FrameProxy.cs index 44ef76bf..c3c6ab1e 100644 --- a/mzLib/Readers/timsTOF/FrameProxy.cs +++ b/mzLib/Readers/timsTOF/FrameProxy.cs @@ -183,44 +183,46 @@ internal double GetOneOverK0(double zeroIndexedScanNumberMedian) /// Note: different threads must not read scans from the same storage handle /// concurrently. /// - internal unsafe static uint[] GetScanRawData(UInt64 fileHandle, long frameId, UInt32 numScans, Object fileLock) + internal static uint[] GetScanRawData(UInt64 fileHandle, long frameId, UInt32 numScans, Object fileLock) { int bufferSize = _defaultBufferSize; // buffer expansion loop while (true) { IntPtr pData = Marshal.AllocHGlobal(bufferSize * Marshal.SizeOf()); - uint outputLength; - - lock ( fileLock ) + try { - outputLength = tims_read_scans_v2( - fileHandle, - frameId, - scan_begin: 0, - scan_end: numScans, - buffer: pData, - length: (uint)(bufferSize * 4)); + uint outputLength; + + lock (fileLock) + { + outputLength = tims_read_scans_v2( + fileHandle, + frameId, + scan_begin: 0, + scan_end: numScans, + buffer: pData, + length: (uint)(bufferSize * 4)); + } + + if (4 * bufferSize > outputLength) + { + var dataArray = new uint[bufferSize]; + CopyToManaged(pData, dataArray, 0, bufferSize); + Marshal.FreeHGlobal(pData); + + return dataArray; + } + + if (outputLength > 16777216) // Arbitrary 16 mb frame limit + { + throw new Exception("Maximum frame size exceeded"); + } + + // Increase buffer size if necessary + bufferSize = ((int)outputLength / 4) + 1; } - - if (4 * bufferSize > outputLength) - { - var dataArray = new uint[bufferSize]; - CopyToManaged(pData, dataArray, 0, bufferSize); - Marshal.FreeHGlobal(pData); - - return dataArray; - } - - if (outputLength > 16777216) // Arbitrary 16 mb frame limit - { - throw new Exception("Maximum frame size exceeded"); - } - - // Increase buffer size if necessary - bufferSize = ((int)outputLength / 4) + 1; - - Marshal.FreeHGlobal(pData); + finally{ Marshal.FreeHGlobal(pData); } } } diff --git a/mzLib/Readers/timsTOF/TimsConversion.cs b/mzLib/Readers/timsTOF/TimsConversion.cs index 26ef65bf..b4b766fc 100644 --- a/mzLib/Readers/timsTOF/TimsConversion.cs +++ b/mzLib/Readers/timsTOF/TimsConversion.cs @@ -49,37 +49,40 @@ internal unsafe double[] DoTransformation(UInt64 fileHandle, long frameId, doubl fixed (double* inputPtr = &input[0]) { IntPtr outPtr = Marshal.AllocHGlobal(input.Length * Marshal.SizeOf()); - lock (_fileLock) + try { - switch (function) + lock (_fileLock) { - case ConversionFunctions.IndexToMz: - tims_index_to_mz(fileHandle, frameId, inputPtr, (double*)outPtr, (UInt32)input.Length); - break; - case ConversionFunctions.MzToIndex: - tims_mz_to_index(fileHandle, frameId, inputPtr, (double*)outPtr, (UInt32)input.Length); - break; - case ConversionFunctions.ScanToOneOverK0: - tims_scannum_to_oneoverk0(fileHandle, frameId, inputPtr, (double*)outPtr, (UInt32)input.Length); - break; - case ConversionFunctions.OneOverK0ToScan: - tims_oneoverk0_to_scannum(fileHandle, frameId, inputPtr, (double*)outPtr, (UInt32)input.Length); - break; - case ConversionFunctions.ScanToVoltage: - tims_scannum_to_voltage(fileHandle, frameId, inputPtr, (double*)outPtr, (UInt32)input.Length); - break; - case ConversionFunctions.VoltageToScan: - tims_voltage_to_scannum(fileHandle, frameId, inputPtr, (double*)outPtr, (UInt32)input.Length); - break; - default: - break; + switch (function) + { + case ConversionFunctions.IndexToMz: + tims_index_to_mz(fileHandle, frameId, inputPtr, (double*)outPtr, (UInt32)input.Length); + break; + case ConversionFunctions.MzToIndex: + tims_mz_to_index(fileHandle, frameId, inputPtr, (double*)outPtr, (UInt32)input.Length); + break; + case ConversionFunctions.ScanToOneOverK0: + tims_scannum_to_oneoverk0(fileHandle, frameId, inputPtr, (double*)outPtr, (UInt32)input.Length); + break; + case ConversionFunctions.OneOverK0ToScan: + tims_oneoverk0_to_scannum(fileHandle, frameId, inputPtr, (double*)outPtr, (UInt32)input.Length); + break; + case ConversionFunctions.ScanToVoltage: + tims_scannum_to_voltage(fileHandle, frameId, inputPtr, (double*)outPtr, (UInt32)input.Length); + break; + case ConversionFunctions.VoltageToScan: + tims_voltage_to_scannum(fileHandle, frameId, inputPtr, (double*)outPtr, (UInt32)input.Length); + break; + default: + break; + } } + + Marshal.Copy(outPtr, transformedValues, 0, input.Length); } - Marshal.Copy(outPtr, transformedValues, 0, input.Length); - Marshal.FreeHGlobal(outPtr); + finally { Marshal.FreeHGlobal(outPtr); } } - return transformedValues; }