Assertion failure when removing a component while an entity is being destroyed

オープン
#224 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
58/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
停滞
技術スタック
cpp
領域
game-dev

調査の方向性

EntityManager::destroy(Entity::Id entity) から始め、削除ループ中にコンポーネントのデストラクタから Entity::remove() がどのように呼び出されるかを調べます。あるコンポーネントが別のコンポーネントを削除する破棄を再現し、その後 assertion が発生しなくなり、すべてのコンポーネントが一度だけ削除されることを確認します。

索引モデルが issue の本文から書いたものです。

説明

Hi,

I noticed that when you are destroying an entity, if you attempt to remove another component from that entity at the same time (e.g: from a component's destructor), you will get an assertion failure in Entity::remove()

As far as I could investigate, the issue is located in EntityManager::destroy(Entity::Id entity).

Old code:

  void destroy(Entity::Id entity) {
    assert_valid(entity);
    uint32_t index = entity.index();
    auto mask = entity_component_mask_[index];
    for (size_t i = 0; i < component_helpers_.size(); i++) {
      BaseComponentHelper *helper = component_helpers_[i];
      if (helper && mask.test(i))
        helper->remove_component(Entity(this, entity));
    }
    event_manager_.emit<EntityDestroyedEvent>(Entity(this, entity));
    entity_component_mask_[index].reset();
    entity_version_[index]++;
    free_list_.push_back(index);
  }

New code that fixes the issue:

  void destroy(Entity::Id entity) {
    assert_valid(entity);
    uint32_t index = entity.index();
    for (size_t i = 0; i < component_helpers_.size(); i++) {
      BaseComponentHelper *helper = component_helpers_[i];
      if (helper && entity_component_mask_[index].test(i))
        helper->remove_component(Entity(this, entity));
    }
    event_manager_.emit<EntityDestroyedEvent>(Entity(this, entity));
    entity_component_mask_[index].reset();
    entity_version_[index]++;
    free_list_.push_back(index);
  }

The issue is caused by the fact mask is being cached before the for loop. As a result, if there is any attempt to remove a component in an other component's destructor, the so called component will be removed twice, hence causing the assertion failure.

Since I'm not in a situation where I can commit anything and/or submit a PR for a few days, I just wanted to let you know about the issue and the fix ;)

Best regards.

主要言語
C++
スター
2.3k
フォーク
300
PR マージ指標
30日以内にマージされた PR はありません

環境構築

このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

alecthomas/entityx のほかの issue

alecthomas/entityx の issue をすべて見る

似ている issue

C++ の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。