-1

現在、ゲーム エンジン用に C++ で ECS に取り組んでいます。2 つの仮想関数を持ちinit()update()派生構造体に実装されている基本システム構造体があります。テンプレートを使用する関数addSystem()removeSystem()関数があり、一連のSystem*システムがあります。それらを呼び出そうとすると、セグメンテーション違反が発生します。

システム:

struct System{
public:
    uint32_t id;
    virtual void init(World* world){}
    virtual void update(World* world){}
};

addSystem():

template<typename T>
    void addSystem(){
        T* system = allocate<T>();
        system->id = getID();
        systems.append(system);
        #ifdef DEBUG
                LOG("ECS: System added successfully.");
        #endif // DEBUG
}

removeSystem():

template<typename T>
    void removeSystem(uint32_t id){
        unsigned int index;
        for(int i = 0; i < systems.size; i++){
            if (systems[i]->id == id){
                index = i;
                break;
            }
        }

        systems.remove(index);
}

System* からの仮想関数の呼び出し:

for (int i = 0; i < systems.size; i++){
    systems[i]->init(this); // Here is the segmentation fault.
}

for (int i = 0; i < systems.size; i++){
    systems[i]->update(this); // Here is the segmentation fault.
}

さらに情報が必要な場合はお尋ねください。

編集: sizefor ループでは 1 に等しく、systems[i] は有効なポインターです。私もテストp systems[i]->updateしましたが、有効なアドレスもあります。問題は呼び出し中です。

4

1 に答える 1