1

I am writing a wrapper class template around a byte storage and I use a constexpr std::array to store the offsets of the members (some can be missing based on the template). The code runs as expected but as I went to write a natvis file for it, it seems that in the release configuration, the constexpr std::array is undefined. Below is a trimmed down version of the problem:

struct STest
{
    static constexpr std::array<int, 4> TestArray { 1, 2, 3, 4 };
    int a[4];
};

__declspec(noinline) STest* Test()
{
    STest* sTest = new STest();
    sTest->a[STest::TestArray[1]] = 5;
    return sTest;
}

int main()
{
    std::cout << "Hello World!\n";

    auto& b = *Test();

    std::printf("%d\t%d\t%d\t%d\n",
        b.a[STest::TestArray[0]], 
        b.a[STest::TestArray[1]],
        b.a[STest::TestArray[2]],
        b.a[STest::TestArray[2]]);
}

Natvis File:

<?xml version="1.0" encoding="utf-8"?> 
<AutoVisualizer xmlns="http://schemas.microsoft.com/vstudio/debugger/natvis/2010">

    <Type Name="STest">
        <Expand>
            <Item Name="TestArray">TestArray</Item>
        </Expand>
    </Type>

</AutoVisualizer>

Natvis Error: Error: identifier "TestArray" is undefined.

Note that I need to be able to access the offset array in my actual use case from within natvis to fetch the offsets so that I can use them to access objects in the byte storage.

I was expecting the constexpr to exist in release configurations as well. It makes sense that it gets compiled out, but constexpr primitives still remain accessible and usable by natvis. I can store a particular entry in the array and use it like this:

static constexpr size_t Element2 = TestArray[2];

But why is the array undefined when the above works? I also tried storing a static volatile pointer to the constexpr array, using a C-style array, and using a member function that returns a pointer to the array to see if natvis can use that.

EDIT: The compiler I am using is msvc 2022 v143

EDIT: Tried the /Zo compiler option as mentioned by user IInspectable but that does not seem to have an effect on the error

EDIT: Tried replacing <Item... with <ExpandedItem... but got the same error.

13
  • It's reasonable for the TestArray to get optimized away in a Release configuration. Can you try adding the /Zo compiler option to see if that changes things? Also, since NatVis is notoriously debugger-specific, could you add the specific debugger you are using to your question? Commented Jan 7 at 15:49
  • The irony is that I want it to be TestArray to be optimised out for performance. But I still want it to show up in natvis so I can write a debug view for the struct. I don't want to mess with the compiler options as imo that doesn't fix the problem. If I get a dump from a program crash in release configuration, I still want the natvis to work in the dump. EDIT: That's not an optimisation flag, will add it and see Commented Jan 7 at 16:00
  • Correct, the /Zo compiler option doesn't affect code generation. It controls the amount of data dumped into the program database (PDB). The PDB is what drives NatVis evaluation, so that's why I suggested having the compiler produce more complete data. Since that did not work out, here's another idea (that probably won't succeed either): Try using <ExpandedItem ... instead of <Item .... Commented Jan 7 at 19:30
  • Heyo, thanks for the suggestions. I tried <ExpandeItem...> as well and I get the same error: Error: identifier "TestArray" is undefined Commented Jan 8 at 12:31
  • With that I guess that one of two things is happening; 1 static data members do not show up as a child of their enclosing class (you can easily verify this), or 2 the symbol doesn't survive compilation and doesn't show up in the PDB. I used to have a command-line tool that dumps all types from a PDB, but can't seem to find it. If it turns up I'll let you know. Commented Jan 8 at 12:53

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.