Disposing a Map object triggers the disposing of associated Provider objects

Topics: SharpMap v0.9 / v1.x
Mar 16, 2011 at 5:41 PM


I am using SharpMap v0.9 in a desktop application. The application displays several thematic maps using the same providers object (implementing the IProvider interface). It means that a given IProvider is used as a data source by more than one map. In order to keep the memory footprint as low as possible, I call the Dispose method on Map objects as soon as I do not need them. My problem is that calling the Dispose method triggers the disposing of the Provider objects used by the map, which results in error during the rendering of the other Map objects which should continue to use the provider. Is it an expected behavior? I thought the whole point of separating the data provider objects from the data consumer objects was to loosely couple them and being able to handle them independently from one another. However if the Map disposes its providers it means that a provider should only be used by one map, or that maps sharing the same providers should only be disposed all at once.

Anyway my question is not really about the expected behavior, it is more like: "how to dispose a map object without disposing the associated provider objects?". I first thought of removing all references to the providers objects before disposing the Map, but it seems a bit dirty. Is there another "cleaner" way to achieve that?


Thanks in advance!

Mar 22, 2011 at 11:16 AM

Have you tried removing all layers from Map.Layers prior to calling the Map.Dispose() function?

Hth FObermaier

Mar 22, 2011 at 11:21 AM

I set the DataSource property of my layers to null before disposing them in order to prevent provider disposing. It works but I still think this behavior is weird: if a provider is dependent on a layer disposed state what is the point of being able to have several layers sharing the same provider?

Mar 22, 2011 at 11:30 AM

I see your point. It does not bother me much since I usually stick to the one map object that the MapBox/-Image control creates.

If you can provide a patch, that -optionally- does not dispose the providers, I'll submit that.

Hth FObermaier

Mar 22, 2011 at 11:31 AM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.