Skip to content

<vector>, <array>: Exception: 'read access violation' in _Orphan_range_unlocked() in Debug-mode #1934

@Spammed

Description

@Spammed

For DEBUG builds in MS Visual Studio 2019, Ver. 16.10.0, Toolset v142, I got with the code below an exception: 'read access violation' in _Orphan_range_unlocked() (possibly very similar to what is discussed here [https://stackoverflow.com/a/62605686/15157143)])

The problem does not occur if arrvec is not static.
It does not fail with a std::vector<int> directly (not in an array).
It does not fail with clang toolset nor in release nor in C++17 or in older VS versions
Because the exception only occurs in these very specific circumstances, I suspect there may be a bug in Visual Studio.
Or is it a programming error after all? I would like to understand this.

#include <vector>
#include <array>

int main()
{
    // Exception: 'read access violation' in _Orphan_range_unlocked()
    // (in C++20 MSVC Debug builds but not in C++17 nor Release)

    static std::array<std::vector<int>, 4> arrvec;
    arrvec[0].reserve(3);
    arrvec[0].emplace_back(1);  // <- Exception occurs here
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingcompilerCompiler work involved

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions