Subtle LayerHandle iteration bug found in various methods


Three occurrences were found in the code, iterating the layers as follows:

There are two internal collections for layer management. One holds a std::vector of layer references (whose index is the Layer Handle), and the other is a std::deque maintaining the current layer order (Layer Position). This is why you will see references to a LayerHandle as well as a LayerPosition, and methods to return the layer handle from the layer position, and vice versa.

The size of the deque is always the number of layers. The vector of layer references, however, can vary as layers are added or removed, particularly since the vector never shrinks. If a layer is removed, a gap is left in the vector until another layer is added to fill that gap. This technique results in Layer Handles that are fixed (unchanging) for the life of the map instance, but also may not be sequential.

The code segment above equates the index (or Position) of the layer to the layer Handle, which is not guaranteed to be true. This is seldom a problem because in most cases, a user will add layers to the map, and then leave them alone. If layers are only added to the map, the layer Position as well as the layer Handle will each be a value from 0 to NumLayers -1, and the above code will work properly. But as soon as any layers are removed from the map, there will be gaps in the layer handle, and you can no longer iterate sequentially from 0 on up.

Therefore I have changed these code segments to the following, which iterates the layer Position (always 0 to NumLayers - 1) and then calls GetLayerHandle to fetch the Handle at the current Position. This code is always safe:




Jerry Faust
March 22, 2020, 10:00 PM

Code has been submitted to the ‘develop’ branch.



Jerry Faust


Jerry Faust



Fix versions