diff --git a/src/ObjectStore.c b/src/ObjectStore.c index e546feb..cf17cdb 100644 --- a/src/ObjectStore.c +++ b/src/ObjectStore.c @@ -93,7 +93,13 @@ void remove_object_from_store(ObjectStore *os, ObjectID *id_ptr) { os->reusable_ids[(os->deleted_count)++] = id; - os->count -= 1; + if (id == (os->count - 1)) { + // Only decrement the count if the last object is deleted. Otherwise, the + // count shouldn't be decremented since the deleted object would be in the + // middle, which means that the functions that loop over all objects in the + // store will not actually loop over everything + os->count -= 1; + } } void handle_all_objects_events(ObjectStore *os, const SDL_Event *event) {