How to debug this bug caused by type traits?

157 views Asked by At

I encountered a really strange bug in my application. It is a video game that uses Bullet Physics among other libraries.

After implementing a SQLite interface, collision detections got messed up. The players falls through the terrain, or collides with invisible objects. To me it looks like a wrong memory access. The database functionality works fine though.

I could already narrow down the issue. The SQLite interface uses type traits which are provided by the different storeable types. When commenting out all trait specializations, collisions work fine.

How can I find the reason of this bug? Please let me know if any other information could be useful for solving the problem, so that I can include that into the question.

Update: This is the code that I need to comment for proper collisions. The storable type is Person.

type/person/type.h

#pragma once
struct Person
{
    float Height, Mass;
    // ...
};

type/person/trait.h

#pragma once
#include "type.h"
#include "manager/data/manager.h"
using namespace std;

template <>
struct ManagerDataTrait<Person>
{
    static const unordered_map<string, string> Fields;
    static void Serialize(Person *Instance, ManagerData::Serialization *Data)
    {
        Data->FLOAT("height", Instance->Height);
        Data->FLOAT("mass",   Instance->Mass);
    }
    static void Deserialize(Person *Instance, ManagerData::Deserialization *Data)
    {
        Instance->Height = Data->FLOAT("height");
        Instance->Mass   = Data->FLOAT("mass");
    }
};

type/person/general.cpp

#include "type.h"
#include "trait.h"
using namespace std;

const unordered_map<string, string> ManagerDataTrait<Person>::Fields = []{
    unordered_map<string, string> fields;
    fields.insert(std::make_pair("height", "FLOAT"));
    fields.insert(std::make_pair("mass", "FLOAT"));
    return fields;
}();
1

There are 1 answers

0
danijar On BEST ANSWER

The user Yakk answered my question in the comments.

The specialization should be visible adjacent to the type. Undefined behavior occurs if one unit sees it and another does not.