victorfisac / Physac

2D physics header-only library for videogames developed in C using raylib library.
http://www.victorfisac.com/physac
MIT License
432 stars 28 forks source link

Heap use after free in examples #35

Closed raysan5 closed 1 year ago

raysan5 commented 6 years ago

Related issues: https://github.com/raysan5/raylib/issues/485 and https://github.com/raysan5/raylib/issues/486

As stated by another user by mail, those issues seem related to:

...when you call reset the code is still running in the thread... so while it iterating over the items, suddenly they are removed and of course you will get floating point errors and access violations. Also destroying and item while iterating over the list, will cause these problems too.

...basically in a thread environment, you have to project critical sections with semaphores.... I basically flatten the PhysicsLoop into a routine called PhysicsThread... and then have my own loop that calls PhysicsThread and I wrap this in critical sections (pseudocode:

procedure physics_loop
begin
repeat 
lock.enter;
PhysicsThread;
lock.leave
until done = true;
end;