When starting VS in debug mode, there are many error checking flags (for example, checking boundaries, viewing memory, etc.). Since you create a lot of data recursively on a static map, it scans the memory before it is released to make sure that nothing has been corrupted. When you switch to the Release build, it should be almost instantaneous.
Upon further verification, you basically allocate almost 1 million pairs ( unsigned long , int ) in the static part of memory. This actually means that ~ 8 MB of data must be freed before the application can complete the close (and since map does not need to be contiguous, it must iterate over all 1 million pairs to remove each of them). Other implementations may slightly optimize memory usage by reserving chunks of space (if he reserved enough space for 100 pairs, this would reduce the freeing process by 2 orders of magnitude).
All that was said, asking why a bad code behaves badly, is similar to how you get wet when you jump into the pool.
source share