Linked list entity system breaks unexpectedly

I am building an entity system for a video game using doubly linked lists. The nodes (entity) of the linked list is allocated in a special manner; when allocating, a free-list is first searched to see if there exists previously deallocated entities. If an entity exists, it is popped off from the free list and then pushed back into the active entity list with original order maintained. If no such entity exists on the free list, a new entity is allocated from the arena.

Here is the code for reference:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>void EntitySpawn(GameState *game)
{
Entity *entity = EntityAllocate(game);
EntityManager *entity_manager = &game->entity_manager;
if(entity->prev)
{
Entity *next = entity->prev->next;
entity->prev->next = entity;
entity->next = next;
}
else
{
Entity *active_entities = entity_manager->active_entities;
if(active_entities)
{
active_entities->prev = entity;
}
entity->next = active_entities;
entity_manager->active_entities = entity;
}
printf("Allocated Entityn");
entity_manager->num_entities++;
}
void EntityDestroy(GameState *game, Entity *entity)
{
EntityManager *entity_manager = &game->entity_manager;
Entity *active_entities = entity_manager->active_entities;
if(!active_entities || !entity) return;
if(active_entities == entity)
{
entity_manager->active_entities = entity->next;
}
if(entity->next)
{
entity->next->prev = entity->prev;
}
if(entity->prev)
{
entity->prev->next = entity->next;
}
EntityDeallocate(game, entity);
entity_manager->num_entities--;
printf("Destroyed Entityn");
}
Entity *EntityAllocate(GameState *game)
{
EntityManager *entity_manager = &game->entity_manager;
Entity *entity = entity_manager->entity_free_list;
if(!entity)
{
entity = ArenaAllocStruct(game->perma_alloc, Entity);
memset(entity, 0, sizeof(Entity));
entity->id = entity_manager->num_entities;
}
else
{
entity_manager->entity_free_list = entity_manager->entity_free_list->next;
}
return entity;
}
void EntityDeallocate(GameState *game, Entity *entity)
{
EntityManager *entity_manager = &game->entity_manager;
entity->next = entity_manager->entity_free_list;
entity_manager->entity_free_list = entity;
}
</code>
<code>void EntitySpawn(GameState *game) { Entity *entity = EntityAllocate(game); EntityManager *entity_manager = &game->entity_manager; if(entity->prev) { Entity *next = entity->prev->next; entity->prev->next = entity; entity->next = next; } else { Entity *active_entities = entity_manager->active_entities; if(active_entities) { active_entities->prev = entity; } entity->next = active_entities; entity_manager->active_entities = entity; } printf("Allocated Entityn"); entity_manager->num_entities++; } void EntityDestroy(GameState *game, Entity *entity) { EntityManager *entity_manager = &game->entity_manager; Entity *active_entities = entity_manager->active_entities; if(!active_entities || !entity) return; if(active_entities == entity) { entity_manager->active_entities = entity->next; } if(entity->next) { entity->next->prev = entity->prev; } if(entity->prev) { entity->prev->next = entity->next; } EntityDeallocate(game, entity); entity_manager->num_entities--; printf("Destroyed Entityn"); } Entity *EntityAllocate(GameState *game) { EntityManager *entity_manager = &game->entity_manager; Entity *entity = entity_manager->entity_free_list; if(!entity) { entity = ArenaAllocStruct(game->perma_alloc, Entity); memset(entity, 0, sizeof(Entity)); entity->id = entity_manager->num_entities; } else { entity_manager->entity_free_list = entity_manager->entity_free_list->next; } return entity; } void EntityDeallocate(GameState *game, Entity *entity) { EntityManager *entity_manager = &game->entity_manager; entity->next = entity_manager->entity_free_list; entity_manager->entity_free_list = entity; } </code>
void EntitySpawn(GameState *game)
{
    Entity *entity = EntityAllocate(game);
    EntityManager *entity_manager = &game->entity_manager;
    if(entity->prev)
    {
        Entity *next = entity->prev->next;
        entity->prev->next = entity;
        entity->next = next;
    }

    else
    {
        Entity *active_entities = entity_manager->active_entities;
        if(active_entities)
        {
            active_entities->prev = entity;
        }
        entity->next = active_entities;
        entity_manager->active_entities = entity;
    }

    printf("Allocated Entityn");
    entity_manager->num_entities++;
}

void EntityDestroy(GameState *game, Entity *entity)
{
    EntityManager *entity_manager = &game->entity_manager;
    Entity *active_entities = entity_manager->active_entities;
    
    if(!active_entities || !entity) return;
    
    if(active_entities == entity)
    {
        entity_manager->active_entities = entity->next;
    }

    if(entity->next)
    {
        entity->next->prev = entity->prev;
    }

    if(entity->prev)
    {
        entity->prev->next = entity->next;
    }
    
    EntityDeallocate(game, entity);
    entity_manager->num_entities--;
    printf("Destroyed Entityn");
}

Entity *EntityAllocate(GameState *game)
{
    EntityManager *entity_manager = &game->entity_manager;
    Entity *entity = entity_manager->entity_free_list;
    if(!entity)
    {
        entity = ArenaAllocStruct(game->perma_alloc, Entity);
        memset(entity, 0, sizeof(Entity));
        entity->id = entity_manager->num_entities;
    }

    else
    {
        entity_manager->entity_free_list = entity_manager->entity_free_list->next;
    }

    return entity;
}

void EntityDeallocate(GameState *game, Entity *entity)
{
    EntityManager *entity_manager = &game->entity_manager;
    entity->next = entity_manager->entity_free_list;
    entity_manager->entity_free_list = entity;
}

Although this code works about 90% of the time as expected, sometimes the entity destruction causes unexpected behaviour, including deallocated entities breaking free from the free-list (memory leak), or deallocated entities suddenly creating a cycle, resulting in an infinite loop when printing the linked list.

Here’s an example of a memory leak:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>[Before deallocation]
Entity Active List
[16] -> [15] -> [14] -> [13] -> [12] -> [11] -> [10] -> [8] -> [7] -> [6] -> [5] -> [4] -> [3] -> [2] -> [1] -> [0] ->
Entity Free List
[9] ->
</code>
<code>[Before deallocation] Entity Active List [16] -> [15] -> [14] -> [13] -> [12] -> [11] -> [10] -> [8] -> [7] -> [6] -> [5] -> [4] -> [3] -> [2] -> [1] -> [0] -> Entity Free List [9] -> </code>
[Before deallocation]

Entity Active List
[16] -> [15] -> [14] -> [13] -> [12] -> [11] -> [10] -> [8] -> [7] -> [6] -> [5] -> [4] -> [3] -> [2] -> [1] -> [0] ->
Entity Free List
[9] ->
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>[After deallocating '12']
Entity Active List
[16] -> [15] -> [14] -> [11] -> [10] -> [8] -> [7] -> [6] -> [5] -> [4] -> [3] -> [2] -> [1] -> [0] ->
Entity Free List
[12] -> [9] ->
</code>
<code>[After deallocating '12'] Entity Active List [16] -> [15] -> [14] -> [11] -> [10] -> [8] -> [7] -> [6] -> [5] -> [4] -> [3] -> [2] -> [1] -> [0] -> Entity Free List [12] -> [9] -> </code>
[After deallocating '12']

Entity Active List
[16] -> [15] -> [14] -> [11] -> [10] -> [8] -> [7] -> [6] -> [5] -> [4] -> [3] -> [2] -> [1] -> [0] ->
Entity Free List
[12] -> [9] ->

After deallocating the 12th node from the Entity Active List, Entity ’13’ suddenly goes missing. I am trying to find the source of this issue in the code, but I can’t seem to trace the culprit.

2

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật