Skip to content

Commit

Permalink
Use std::shared_ptr for editor resources, fix crashes with brush
Browse files Browse the repository at this point in the history
Simplify memory management of groups, layers, images, sounds and envelopes by using `std::shared_ptr` for all of them.

Clear brush when creating a new map to prevent crash due to unloaded textures being used in tile layer. Closes ddnet#6935.

Also fix crash when restoring a saved brush after removing an image that is being used by it.
  • Loading branch information
Robyt3 committed Jul 28, 2023
1 parent 1f1cc80 commit 6f03aab
Show file tree
Hide file tree
Showing 8 changed files with 455 additions and 542 deletions.
464 changes: 207 additions & 257 deletions src/game/editor/editor.cpp

Large diffs are not rendered by default.

143 changes: 69 additions & 74 deletions src/game/editor/editor.h

Large diffs are not rendered by default.

106 changes: 49 additions & 57 deletions src/game/editor/io.cpp

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/game/editor/layer_game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ void CLayerGame::SetTile(int x, int y, CTile Tile)
{
if(!m_pEditor->m_Map.m_pFrontLayer)
{
CLayer *pLayerFront = new CLayerFront(m_Width, m_Height);
std::shared_ptr<CLayer> pLayerFront = std::make_shared<CLayerFront>(m_Width, m_Height);
m_pEditor->m_Map.MakeFrontLayer(pLayerFront);
m_pEditor->m_Map.m_pGameGroup->AddLayer(pLayerFront);
}
Expand Down
12 changes: 6 additions & 6 deletions src/game/editor/layer_quads.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,10 @@ void CLayerQuads::BrushSelecting(CUIRect Rect)
Graphics()->LinesEnd();
}

int CLayerQuads::BrushGrab(CLayerGroup *pBrush, CUIRect Rect)
int CLayerQuads::BrushGrab(std::shared_ptr<CLayerGroup> pBrush, CUIRect Rect)
{
// create new layers
CLayerQuads *pGrabbed = new CLayerQuads();
std::shared_ptr<CLayerQuads> pGrabbed = std::make_shared<CLayerQuads>();
pGrabbed->m_pEditor = m_pEditor;
pGrabbed->m_Image = m_Image;
pBrush->AddLayer(pGrabbed);
Expand Down Expand Up @@ -138,9 +138,9 @@ int CLayerQuads::BrushGrab(CLayerGroup *pBrush, CUIRect Rect)
return pGrabbed->m_vQuads.empty() ? 0 : 1;
}

void CLayerQuads::BrushPlace(CLayer *pBrush, float wx, float wy)
void CLayerQuads::BrushPlace(std::shared_ptr<CLayer> pBrush, float wx, float wy)
{
CLayerQuads *pQuadLayer = (CLayerQuads *)pBrush;
std::shared_ptr<CLayerQuads> pQuadLayer = std::static_pointer_cast<CLayerQuads>(pBrush);
for(const auto &Quad : pQuadLayer->m_vQuads)
{
CQuad n = Quad;
Expand Down Expand Up @@ -264,9 +264,9 @@ void CLayerQuads::ModifyEnvelopeIndex(FIndexModifyFunction Func)
}
}

CLayer *CLayerQuads::Duplicate() const
std::shared_ptr<CLayer> CLayerQuads::Duplicate() const
{
return new CLayerQuads(*this);
return std::make_shared<CLayerQuads>(*this);
}

int CLayerQuads::SwapQuads(int Index0, int Index1)
Expand Down
12 changes: 6 additions & 6 deletions src/game/editor/layer_sounds.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,10 @@ void CLayerSounds::BrushSelecting(CUIRect Rect)
Graphics()->LinesEnd();
}

int CLayerSounds::BrushGrab(CLayerGroup *pBrush, CUIRect Rect)
int CLayerSounds::BrushGrab(std::shared_ptr<CLayerGroup> pBrush, CUIRect Rect)
{
// create new layer
CLayerSounds *pGrabbed = new CLayerSounds();
std::shared_ptr<CLayerSounds> pGrabbed = std::make_shared<CLayerSounds>();
pGrabbed->m_pEditor = m_pEditor;
pGrabbed->m_Sound = m_Sound;
pBrush->AddLayer(pGrabbed);
Expand All @@ -167,9 +167,9 @@ int CLayerSounds::BrushGrab(CLayerGroup *pBrush, CUIRect Rect)
return pGrabbed->m_vSources.empty() ? 0 : 1;
}

void CLayerSounds::BrushPlace(CLayer *pBrush, float wx, float wy)
void CLayerSounds::BrushPlace(std::shared_ptr<CLayer> pBrush, float wx, float wy)
{
CLayerSounds *pSoundLayer = (CLayerSounds *)pBrush;
std::shared_ptr<CLayerSounds> pSoundLayer = std::static_pointer_cast<CLayerSounds>(pBrush);
for(const auto &Source : pSoundLayer->m_vSources)
{
CSoundSource n = Source;
Expand Down Expand Up @@ -228,7 +228,7 @@ void CLayerSounds::ModifyEnvelopeIndex(FIndexModifyFunction Func)
}
}

CLayer *CLayerSounds::Duplicate() const
std::shared_ptr<CLayer> CLayerSounds::Duplicate() const
{
return new CLayerSounds(*this);
return std::make_shared<CLayerSounds>(*this);
}
Loading

0 comments on commit 6f03aab

Please sign in to comment.