GdalRasterLayer threading issues

Topics: SharpMap v0.9 / v1.x
Developer
Jul 17, 2014 at 3:03 PM
Edited Jul 18, 2014 at 7:17 AM
With the changeset https://sharpmap.codeplex.com/SourceControl/changeset/106315 I've introduced a new minimap control into the game, with its use I've noticed some random MemoryAccessExceptions at line 1070 of GdalRasterLayer.cs:
band.ReadRaster(imageRect.Left, imageRect.Top,
         imageRect.Width, imageRect.Height,
         tempBuffer, displayImageSize.Width, rowsToRead, 0, 0);
I thought that these exceptions were originating because the control clones the map referenced, since the GdalRasterLayer is not ICloneable then the same layer can be executed by multiple threads and this could not be supported.

My first attempt was to implement ICloneable inside of GdalRasterLayer, so a clone of the map would give two separated instances that could be executed by different thread, this solution anyway did not appear to solve the problem, I still had random exceptions inside the unmanaged code, like if there is some global state underneath.

The only workaround that I've found is to put the rendering of this layer in a queue through a static lock.

I don't know yet if this happens with any driver, I've only tested this behavior with ECW files, if you confirm this issue then we should add a static lock inside the rendering of this layer, ugly thing.