Skip to content

EXC_BAD_ACCESS eval_iter.c:171 if (flecs_query_run_until(... #1690

@ChadHartman

Description

@ChadHartman

Describe the bug

EXC_BAD_ACCESS eval_iter.c:171 if (flecs_query_run_until(...

To Reproduce
Steps to reproduce the behavior:

  • Flecs v4.0.5
  • Mac OSX 15.5 (24F74)
  • Apple clang version 17.0.0 (clang-1700.0.13.3)
  • Target: arm64-apple-darwin24.5.0
  • C standard c11

Reproducible example:

#include <stdlib.h>
#include <flecs.h>

/* === Components === */

typedef struct Rect {
  float x, y, w, h;
} Rect;

typedef struct BackgroundColor {
  uint8_t r, g, b, a;
} BackgroundColor;

ECS_COMPONENT_DECLARE(Rect);
ECS_COMPONENT_DECLARE(BackgroundColor);

/* === Systems === */

static void App_onStart(ecs_iter_t *it) {

  ecs_entity_t viewPrefab = ecs_entity(
      it->world,
      {.name = "ViewPreFab",
       .add = ecs_ids(EcsPrefab)});
  ecs_set(it->world, viewPrefab, Rect, {0});

  // Root
  ecs_entity_t rootView = ecs_new_w_pair(it->world, EcsIsA, viewPrefab);
  ecs_set(it->world, rootView, BackgroundColor, {0});

  // Button Collection
  ecs_entity_t buttonCollectionView = ecs_new_w_pair(it->world, EcsIsA, viewPrefab);
  ecs_add_pair(it->world, buttonCollectionView, EcsChildOf, rootView);

  // Title
  ecs_entity_t titleView = ecs_new_w_pair(it->world, EcsIsA, viewPrefab);
  ecs_add_pair(it->world, titleView, EcsChildOf, buttonCollectionView);

  // NewGame Button
  ecs_entity_t newGameButtonView = ecs_new_w_pair(it->world, EcsIsA, viewPrefab);
  ecs_add_pair(it->world, newGameButtonView, EcsChildOf, buttonCollectionView);

  // LoadGame Button
  ecs_entity_t loadGameButtonView = ecs_new_w_pair(it->world, EcsIsA, viewPrefab);
  ecs_add_pair(it->world, loadGameButtonView, EcsChildOf, buttonCollectionView);

  // Exit Button
  ecs_entity_t exitButtonView = ecs_new_w_pair(it->world, EcsIsA, viewPrefab);
  ecs_add_pair(it->world, exitButtonView, EcsChildOf, buttonCollectionView);
}

static void App_onStore(ecs_iter_t *it) {

  // Loop through entities
  while (ecs_query_next(it)) {
    const Rect *rects = ecs_field(it, Rect, 0);
    if (ecs_field_is_set(it, 1)) {
      const BackgroundColor *colors = ecs_field(it, BackgroundColor, 1);
      for (int32_t i = 0; i < it->count; ++i) {
        const Rect *rect = &rects[i];
        const BackgroundColor *color = &colors[i];

        printf(
            "Render Rect{x=%.0f, y=%.0f, w=%.0f, h=%.0f}, Color={r=%02x, g=%02x, b=%02x, a=%02x}",
            rect->x,
            rect->y,
            rect->w,
            rect->h,
            color->r,
            color->g,
            color->b,
            color->a);
      }
    } else {
      for (int32_t i = 0; i < it->count; ++i) {
        const Rect *rect = &rects[i];
        printf(
            "Render Rect{x=%.0f, y=%.0f, w=%.0f, h=%.0f}",
            rect->x,
            rect->y,
            rect->w,
            rect->h);
      }
    }
  }

  // Evaluate exit condition
  static uint8_t counter = 0;
  if (++counter == 5) {
    ecs_quit(it->world);
  }
}

/* === Main === */

int main() {

  ecs_world_t *world = ecs_init();
  ECS_COMPONENT_DEFINE(world, Rect);
  ECS_COMPONENT_DEFINE(world, BackgroundColor);
  ECS_SYSTEM(world, App_onStart, EcsOnStart);
  ECS_SYSTEM(world, App_onStore, EcsOnStore, [in] Rect, [in] ?BackgroundColor);

  int res = ecs_app_run(world, &(ecs_app_desc_t){0});
  ecs_fini(world);
  return res;
}

Expected behavior

In my game I run a lua script which creates several view entities during the EcsOnStart phase. I expect that to run once and then render the view entities during the EcsOnStore phase.

Additional context

I apologize if this is not a bug but API abuse. I reread the documentation several times and wasn't sure about several things.

  1. Is it appropriate to create entities in systems? If not and using ecs_app_run when can I create entities?
  2. Is it ok to create Prefabs in systems?
  3. Am I using the optional correctly?
  4. Thank you so much!

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions