0

I use ENTT and add the following component to every entity.

template<typename T, typename... Args>
T& AddComponent(Args&&... args)
{
    HZ_CORE_ASSERT(!HasComponent<T>(), "Entity already has component!");
    T& component = m_Scene->m_Registry.emplace<T>(m_EntityHandle, std::forward<Args>(args)...);
    m_Scene->OnComponentAdded<T>(*this, component);
    return component;
}

struct TagComponent
{
    std::string Tag;

    TagComponent() = default;
    TagComponent(const TagComponent& other) : Tag(other.Tag) {};
    TagComponent(const std::string tag) : Tag(tag) {}
};

Sometimes it will throw "access violation exception" when _Container_base12::_Orphan_all() is called during reallocation. I find that the address for the two pointers in std::Container_proxy is weird. _Mycont should have one byte more for the address to make sense. _Myfirstiter should not have the byte 0xfd at the front, it should be a nullptr.

enter image description here

What's the possible reason?

Edit: Failed to solve this bug. It's really weired that if I put a breakpoint and run step by step when I load the node[0]-1 of the mesh, the bug never occur. Otherwise it came up with a possibility of about 20%.

zyr
  • 21
  • 3
  • Essentially all you can tell from this is that something somewhere in your program has broken something badly before this point. The nature of undefined behaviour means that it's very difficult to guess what or where. – molbdnilo Sep 13 '22 at 08:31
  • It looks like a weird coincidence that the string that the debugger can't read has an address that is the mirror image of the string it can read; `5d305b7365646f6e` is "]0[sedon", which is "nodes[0]" in reverse. That might provide a clue about where you went wrong, or it might not. – molbdnilo Sep 13 '22 at 08:38
  • @molbdnilo What a keen observation! – zyr Sep 13 '22 at 11:54
  • @molbdnilo I read the source code for xstring, _Buf and _Ptr are both defined in a union called _Bxty, so it is not coincidence. But thank you anyway. – zyr Sep 13 '22 at 13:07
  • Ah, the "short string optimization". I should have thought of that. Still, I got distracted from this miserable world for a few minutes, which was good. – molbdnilo Sep 13 '22 at 13:11

1 Answers1

1

0xfd ? I bet there are 4 bytes with that value, not one: What are the debug memory fill patterns in Visual Studio C++ and Windows?

You're probabably looking at the remnants of a destroyed object.

MSalters
  • 173,980
  • 10
  • 155
  • 350