Skip to content

Commit

Permalink
Clean up internal resources on Destroy (#886)
Browse files Browse the repository at this point in the history
* Clean up resources

---------

Signed-off-by: Ian Chen <[email protected]>
  • Loading branch information
iche033 authored Aug 28, 2023
1 parent 2286452 commit 09022e4
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 1 deletion.
15 changes: 15 additions & 0 deletions ogre/src/OgreDistortionPass.cc
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,21 @@ void OgreDistortionPass::Destroy()
this->dataPtr->distortionInstance = nullptr;
this->dataPtr->distortionCompositorListener.reset();
}

if (!this->dataPtr->distortionMaterial.isNull())
{
Ogre::MaterialManager &matManager = Ogre::MaterialManager::getSingleton();
matManager.remove(this->dataPtr->distortionMaterial->getName());
this->dataPtr->distortionMaterial.setNull();
}

if (!this->dataPtr->distortionTexture.isNull())
{
auto &textureManager = Ogre::TextureManager::getSingleton();
textureManager.unload(this->dataPtr->distortionTexture->getName());
textureManager.remove(this->dataPtr->distortionTexture->getName());
this->dataPtr->distortionTexture.setNull();
}
}

//////////////////////////////////////////////////
Expand Down
3 changes: 3 additions & 0 deletions ogre2/include/gz/rendering/ogre2/Ogre2LensFlarePass.hh
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ namespace gz
public: void WorkspaceRemoved(
Ogre::CompositorWorkspace *_workspace) override;

// Documentation inherited
public: virtual void Destroy() override;

/// \brief Check to see if the lens flare is occluded and return a scaling
/// factor that is proportional to the lens flare's visibility
/// \remark Ogre2LensFlarePass::PreRender must have been called first.
Expand Down
8 changes: 8 additions & 0 deletions ogre2/src/Ogre2LensFlarePass.cc
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,14 @@ Ogre2LensFlarePass::Ogre2LensFlarePass() :
//////////////////////////////////////////////////
Ogre2LensFlarePass::~Ogre2LensFlarePass()
{
this->Destroy();
}

//////////////////////////////////////////////////
void Ogre2LensFlarePass::Destroy()
{
this->dataPtr->rayQuery.reset();
this->dataPtr->currentCamera.reset();
}

//////////////////////////////////////////////////
Expand Down
5 changes: 5 additions & 0 deletions ogre2/src/Ogre2Material.cc
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,12 @@ Ogre2Material::~Ogre2Material()
void Ogre2Material::Destroy()
{
if (!this->Scene()->IsInitialized())
{
// just reset the ogre pointers and return.
this->dataPtr->ogreSolidColorMat.reset();
this->dataPtr->ogreSolidColorShader.reset();
return;
}

if (!this->ogreDatablock)
return;
Expand Down
3 changes: 3 additions & 0 deletions ogre2/src/Ogre2RayQuery.cc
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ Ogre2RayQuery::Ogre2RayQuery()
//////////////////////////////////////////////////
Ogre2RayQuery::~Ogre2RayQuery()
{
if (!this->Scene()->IsInitialized())
return;

if (this->dataPtr->rayQuery)
{
Ogre2ScenePtr ogreScene =
Expand Down
12 changes: 11 additions & 1 deletion ogre2/src/Ogre2SelectionBuffer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,9 @@ Ogre2SelectionBuffer::Ogre2SelectionBuffer(const std::string &_cameraName,
/////////////////////////////////////////////////
Ogre2SelectionBuffer::~Ogre2SelectionBuffer()
{
if (!this->dataPtr->scene || !this->dataPtr->scene->IsInitialized())
return;

this->DeleteRTTBuffer();

// remove selectionMaterial in destructor
Expand All @@ -157,7 +160,14 @@ Ogre2SelectionBuffer::~Ogre2SelectionBuffer()
}

// remove selection buffer camera
this->dataPtr->sceneMgr->destroyCamera(this->dataPtr->selectionCamera);
if (this->dataPtr->selectionCamera)
{
this->dataPtr->selectionCamera->removeListener(
this->dataPtr->materialSwitcher.get());
this->dataPtr->sceneMgr->destroyCamera(this->dataPtr->selectionCamera);
this->dataPtr->selectionCamera = nullptr;
this->dataPtr->materialSwitcher.reset();
}
}

/////////////////////////////////////////////////
Expand Down

0 comments on commit 09022e4

Please sign in to comment.