setNumberOfDesktops is called from the desktopgrid (or pot. other effects) and this code belongs there and not into the config loading
also the function included some (probably evolutionary) redundant blocks
needs backport - i'm however not sure that this really will fix the bug, it's just overflow prone and the bug likely defers from an overflow (if the claimed Qt bug is none)
Testing
created desktops w/ desktopgrid, moved windows to the end, removed desktops, w/ & w/o ending the effect in between