SanderMertens / flecs

A fast entity component system (ECS) for C & C++
https://www.flecs.dev
Other
6.46k stars 454 forks source link

ecs_run_post_frame leaking for one-off context #1347

Closed jpeletier closed 1 month ago

jpeletier commented 2 months ago

Describe the bug I need to pass one-off context pointer to ecs_run_post_frame(), so what I am doing is allocating a value and have the callback free the value once called. This is to achieve something like a lambda capturing some values since run_post_frame only allows function pointers.

My problem is that if the world is finished before the call happens, I get a leak:

To Reproduce Steps to reproduce the behavior:

  flecs::world ecs;
  int *a = new int(79);
  ecs.run_post_frame(
      [](flecs::world_t *w, void *ctx) {
        int *a = (int *) ctx;
        std::cout << "a = " << *a << std::endl;
        delete a;
      },
      a);
  // world destroyed here.

The callback is not invoked and int* a does not get a chance to be freed.

Would it be possible to have an additional optional parameter to ecs_run_post_frame that takes a context free callback? If != NULL, this free-callback would be called after each run_post_frame invocation, and on world fini.

My workaround for now is to call ecs.progress() manually before destroying the world, so these callbacks have a chance and can free the context, but that has other side-effects.

Please let me know if this is bug/enhancement and whether this could be changed (I can open a PR). Thanks.

SanderMertens commented 2 months ago

What actually should happen here is that run_post_frame should throw an error when it's called outside of frame_begin/frame_end (which are called by progress).

SanderMertens commented 1 month ago

Done!