Use an objpool for bosses

The primary reason is to avoid a use-after-free when something tries to
unbox a dead boss reference.
This commit is contained in:
Andrei Alexeyev 2021-03-22 17:40:23 +02:00
parent b0a2c5cde9
commit 41dc16993b
No known key found for this signature in database
GPG key ID: 72D26128040B9690
3 changed files with 7 additions and 2 deletions

View file

@ -17,6 +17,7 @@
#include "util/glm.h"
#include "portrait.h"
#include "stages/stage5/stage5.h" // for unlockable bonus BGM
#include "stageobjects.h"
static void ent_draw_boss(EntityInterface *ent);
static DamageResult ent_damage_boss(EntityInterface *ent, const DamageInfo *dmg);
@ -36,7 +37,7 @@ static void calc_spell_bonus(Attack *a, SpellBonus *bonus);
DECLARE_TASK(boss_particles, { BoxedBoss boss; });
Boss *create_boss(char *name, char *ani, cmplx pos) {
Boss *boss = calloc(1, sizeof(Boss));
Boss *boss = objpool_acquire(stage_object_pools.bosses);
boss->name = strdup(name);
boss->pos = pos;
@ -1332,7 +1333,7 @@ void free_boss(Boss *boss) {
boss_set_portrait(boss, NULL, NULL, NULL);
aniplayer_free(&boss->ani);
free(boss->name);
free(boss);
objpool_release(stage_object_pools.bosses, boss);
}
void boss_start_attack(Boss *b, Attack *a) {

View file

@ -14,6 +14,7 @@
#include "enemy.h"
#include "laser.h"
#include "stagetext.h"
#include "boss.h"
#include "aniplayer.h"
#define MAX_projectiles 2048
@ -21,6 +22,7 @@
#define MAX_enemies 64
#define MAX_lasers 64
#define MAX_stagetext 1024
#define MAX_bosses 1
#define OBJECT_POOLS \
OBJECT_POOL(Projectile, projectiles) \
@ -28,6 +30,7 @@
OBJECT_POOL(Enemy, enemies) \
OBJECT_POOL(Laser, lasers) \
OBJECT_POOL(StageText, stagetext) \
OBJECT_POOL(Boss, bosses) \
StageObjectPools stage_object_pools;

View file

@ -21,6 +21,7 @@ typedef struct StageObjectPools {
ObjectPool *enemies;
ObjectPool *lasers;
ObjectPool *stagetext;
ObjectPool *bosses;
};
ObjectPool *first;