System.AccessViolationException reading some NTF files
Expected behavior and actual behavior.
We are using the sample C# code provided in GDALRead.cs and GDALReadDirect.cs here https://github.com/OSGeo/gdal/tree/master/gdal/swig/csharp/apps. We expect to be able to read in a NTF image for further processing and thumbnail generation. It works for some NTF images, but others throw an exception on read.
The exception always occurs when reading the third band of an RGB image. Even if we change the order of the colors.
redBand.ReadRaster(0, 0, width, height, r, width, height, 0, 0);
greenBand.ReadRaster(0, 0, width, height, g, width, height, 0, 0);
blueBand.ReadRaster(0, 0, width, height, b, width, height, 0, 0);
The exception thrown is:
Unhandled Exception: System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt. at OSGeo.GDAL.GdalPINVOKE.Band_ReadRaster__SWIG_0(HandleRef jarg1, Int32 jarg2, Int32 jarg3, Int32 jarg4, Int32 jarg5, IntPtr jarg6, Int32 jarg7, Int32 jarg8, Int32 jarg9, Int32 jarg10, Int32 jarg11) at OSGeo.GDAL.Band.ReadRaster(Int32 xOff, Int32 yOff, Int32 xSize, Int32 ySize, IntPtr buffer, Int32 buf_xSize, Int32 buf_ySize, DataType buf_type, Int32 pixelSpace, Int32 lineSpace) at GDALReadDirect.SaveBitmapDirect(Dataset ds, String filename, Int32 iOverview) in C:...\GDALReadDirect.cs:line 195 at GDALReadDirect.Main(String[] args) in C:...\GDALReadDirect.cs:line 131 at GDAL.Program.Main(String[] args) in C:...\Program.cs:line 87
Steps to reproduce the problem.
Use either the GdalRead.cs or GdalReadDirect.cs sample code to read an RGB image. I am not currently able to share the images that cause the exception. If I can get permission to share I will update the issue.
Operating system
Windows 10
GDAL version and provenance
2.4.4 (.NET Framework 4.8)
I can see there are newer releases on Github, but 2.4.4 is the latest that is available on Nuget.
Check with the gdal_translate utility if the crash can be reproduced independntly of the C# bindings. And provide a link to / attach a file that causes the crash
Thanks @rouault Using gdal_translate within Anaconda to extract a JPG from the NTF worked without error. Is it possible the C# sample code is just outdated? (I'm still working on getting permission to share a sample file)
I should note that the latest GDAL versions are different between Anaconda (3.0.2) and Nuget (2.4.4). Version 2.4.4 doesn't appear to be available via Anaconda. So it's possible the error is due to a bug that's been fixed between releases. Please let me know if there's any additional testing you'd like me to try.
It is possible that you might allocate a too small buffer depending on the dataset characteristics
We are using the sample code provided here: https://github.com/OSGeo/gdal/blob/master/gdal/swig/csharp/apps/GDALReadDirect.cs (Error occurs on line 195) We have tried experimenting by adjusting different values. So far without luck. I've included the dataset properties that are displayed for one of the images here. I'm a little confused on why it always fails on the third band when the first two succeed. Even though they all appear to be the same dimension. I tried removing the "+ 1" and "+ 2" on lines 194 and 195 thinking that might be what was causing it to go out of bounds. It didn't help. Any suggestions on what we might be able to adjust to make sure the buffer is allocated properly?
Using driver National Imagery Transmission Format Band 1 : DataType: GDT_Byte Size (16384,9264) PaletteInterp: GCI_RedBand OverView 0 : DataType: GDT_Byte Size (8192,4632) PaletteInterp: GCI_Undefined OverView 1 : DataType: GDT_Byte Size (4096,2316) PaletteInterp: GCI_Undefined OverView 2 : DataType: GDT_Byte Size (2048,1158) PaletteInterp: GCI_Undefined OverView 3 : DataType: GDT_Byte Size (1024,579) PaletteInterp: GCI_Undefined OverView 4 : DataType: GDT_Byte Size (512,289) PaletteInterp: GCI_Undefined OverView 5 : DataType: GDT_Byte Size (256,144) PaletteInterp: GCI_Undefined Band 2 : DataType: GDT_Byte Size (16384,9264) PaletteInterp: GCI_GreenBand OverView 0 : DataType: GDT_Byte Size (8192,4632) PaletteInterp: GCI_Undefined OverView 1 : DataType: GDT_Byte Size (4096,2316) PaletteInterp: GCI_Undefined OverView 2 : DataType: GDT_Byte Size (2048,1158) PaletteInterp: GCI_Undefined OverView 3 : DataType: GDT_Byte Size (1024,579) PaletteInterp: GCI_Undefined OverView 4 : DataType: GDT_Byte Size (512,289) PaletteInterp: GCI_Undefined OverView 5 : DataType: GDT_Byte Size (256,144) PaletteInterp: GCI_Undefined Band 3 : DataType: GDT_Byte Size (16384,9264) PaletteInterp: GCI_BlueBand OverView 0 : DataType: GDT_Byte Size (8192,4632) PaletteInterp: GCI_Undefined OverView 1 : DataType: GDT_Byte Size (4096,2316) PaletteInterp: GCI_Undefined OverView 2 : DataType: GDT_Byte Size (2048,1158) PaletteInterp: GCI_Undefined OverView 3 : DataType: GDT_Byte Size (1024,579) PaletteInterp: GCI_Undefined OverView 4 : DataType: GDT_Byte Size (512,289) PaletteInterp: GCI_Undefined OverView 5 : DataType: GDT_Byte Size (256,144) PaletteInterp: GCI_Undefined
Perhaps provide a fully self contained minimal code snippet. It might be an error in your code. Maybe @szekerest can help
I was able to get one of the problem images working against the GDALDatasetRasterIO.cs and GDDatasetRasterIOEx.cs examples. I will continue digging and report back if I'm able to figure out what the difference is. I just wanted to update you so you don't spend any more time looking into this while I work. Thanks for the help so far @rouault
The GDALDatasetRasterIOEx.cs example appears to have fixed our issue from what we can tell from our testing so far. This example has more robust checking to determine the proper parameters (alpha, pixel format, data type, pixel space) instead of just using fixed values like the other examples. I figured might have been what fixed the issue, but that wasn't the case. In the end it appears to be calling ReadRaster() on the Dataset instead of calling ReadRaster() on each of the individual Band objects that does the trick. It seems there might be some bug with Band.ReadRaster() or at least in the way it is used within the GDALRead.cs example. As a bonus this approach is also significantly faster. Our issue is solved for now. Would you like me to leave this issue open to address the potential Band.ReadRaster() bug? Or shall I close? Would you like me to add some commenting to the GDALRead.cs and GDALReadDirect.cs files so others don't run into the same issue in the future?
From https://github.com/OSGeo/gdal/pull/3300#issuecomment-748198862 , the issue was a 32-bit overflow . @czielin Was the overflow in the allocation of the arrays on your side, or in the GDAL bindings ?
@rouault Based on the stack trace in the initial post my best guess is it's coming from the underlying GDAL library. Though I admittedly am not familiar enough with the code to say so definitively. I would like to reiterate that there isn't any custom code involved in reproducing this. I am simply plugging in a large NTF file to the existing C# examples in this repo.
That all being said, this is only an issue if there is a desire to maintain 32-bit support for processing of larger images. After the latest pull request I submitted #3314 , I have verified that all four read examples work when executed from a 64-bit context. Though with varying levels of performance.