Patch - Adds component shutdown functionality - V13.6.1

Implements a shutdown mechanism for components and entities,
ensuring proper resource release and preventing memory leaks.

This change introduces a virtual `Shutdown` method to the
`Component` class, allowing derived components to release
specific resources when they are removed from an entity or
when the entity is destroyed. The `Entity` class now calls
the `Shutdown` method on all its components during its own
release process. The `EntityManager` now calls the
`release()` method when destroying an entity.

This enhancement ensures that resources, such as FMOD sound
objects and channels in the `AudioComponent`, are properly
released, preventing potential resource leaks and improving
the stability of the engine.
This commit is contained in:
2025-09-17 16:47:01 +02:00
parent 7fc4b08808
commit 76fdd3c76e
5 changed files with 62 additions and 19 deletions

View File

@@ -32,6 +32,11 @@ public:
*/
virtual void Initialize() {}
/**
*Virtual function to shutdown the component.
*/
virtual void Shutdown() {}
/**
* Virtual function to update the component.
* @param deltaTime Time since the last update.

View File

@@ -30,6 +30,27 @@ public:
// Ne pas lib<69>rer m_system ici car il peut <20>tre partag<61>
}
/**
* Shutdown the audio component by releasing the sound and channel.
* This method is called when the component is removed from an entity or when the entity is destroyed.
*/
void Shutdown() override {
if (m_channel) {
bool isPlaying = false;
m_channel->isPlaying(&isPlaying);
if (isPlaying) {
m_channel->stop();
}
m_channel = nullptr;
}
if (m_sound) {
m_sound->release();
m_sound = nullptr;
}
m_system = nullptr; // Ne pas lib<69>rer m_system ici car il peut <20>tre partag<61>
m_camera = nullptr;
}
/**
* Initialize the audio component by setting up the FMOD system and camera from the parent entity.
* This method is called when the component is added to an entity.

View File

@@ -163,6 +163,22 @@ public:
*/
FMOD::System* GetSoundSystem() const { return m_soundSystem; }
/**
* Release all data associated with the entity.
* This includes clearing all components and resetting the camera and sound system pointers.
*/
void release()
{
// call shutdown on all components if needed
for (auto& [typeID, component] : m_Components) {
// If components had a shutdown method, we would call it here
component->Shutdown();
}
m_Components.clear();
m_camera = nullptr;
m_soundSystem = nullptr;
}
void SetDevice (ID3D11Device* dev) { device = dev; }
void SetContext(ID3D11DeviceContext* ctx) { context = ctx; }
ID3D11Device* GetDevice() const { return device; }

View File

@@ -48,6 +48,7 @@ public:
void DestroyEntity(EntityID id) {
auto it = m_Entities.find(id);
if (it != m_Entities.end()) {
it->second->release();
m_Entities.erase(it);
m_FreeIDs.push(id); // Recycler l'ID
}