Skip to content

Commit 1f46cd7

Browse files
committed
CGraphics: Fix additional memory leak
1 parent c029a6f commit 1f46cd7

File tree

1 file changed

+83
-25
lines changed

1 file changed

+83
-25
lines changed

Client/core/Graphics/CGraphics.cpp

Lines changed: 83 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
#include "StdInc.h"
1313
#include <game/CSettings.h>
14+
#include <memory>
1415
#include "DXHook/CProxyDirect3DDevice9.h"
1516
#include "CTileBatcher.h"
1617
#include "CLine3DBatcher.h"
@@ -52,37 +53,83 @@ CGraphics::CGraphics(CLocalGUI* pGUI)
5253
m_ActiveBlendMode = EBlendMode::BLEND;
5354
m_CurDrawMode = EDrawMode::NONE;
5455
m_CurBlendMode = EBlendMode::BLEND;
55-
56-
m_pRenderItemManager = new CRenderItemManager();
57-
m_pTileBatcher = new CTileBatcher();
58-
m_pLine3DBatcherPreGUI = new CLine3DBatcher(true);
59-
m_pLine3DBatcherPostFX = new CLine3DBatcher(true);
60-
m_pLine3DBatcherPostGUI = new CLine3DBatcher(false);
61-
m_pMaterialLine3DBatcherPreGUI = new CMaterialLine3DBatcher(true);
62-
m_pMaterialLine3DBatcherPostFX = new CMaterialLine3DBatcher(true);
63-
m_pMaterialLine3DBatcherPostGUI = new CMaterialLine3DBatcher(false);
64-
m_pPrimitive3DBatcherPreGUI = new CPrimitive3DBatcher(true);
65-
m_pPrimitive3DBatcherPostFX = new CPrimitive3DBatcher(true);
66-
m_pPrimitive3DBatcherPostGUI = new CPrimitive3DBatcher(false);
67-
m_pMaterialPrimitive3DBatcherPreGUI = new CMaterialPrimitive3DBatcher(true, this);
68-
m_pMaterialPrimitive3DBatcherPostFX = new CMaterialPrimitive3DBatcher(true, this);
69-
m_pMaterialPrimitive3DBatcherPostGUI = new CMaterialPrimitive3DBatcher(false, this);
70-
m_pPrimitiveBatcher = new CPrimitiveBatcher();
71-
m_pPrimitiveMaterialBatcher = new CPrimitiveMaterialBatcher(this);
72-
73-
m_pScreenGrabber = NewScreenGrabber();
74-
m_pPixelsManager = NewPixelsManager();
75-
m_LastLostDeviceTimer.SetMaxIncrement(250);
76-
m_pAspectRatioConverter = new CAspectRatioConverter();
56+
std::unique_ptr<CRenderItemManager> renderItemManager(new CRenderItemManager());
57+
std::unique_ptr<CTileBatcher> tileBatcher(new CTileBatcher());
58+
std::unique_ptr<CLine3DBatcher> line3DPreGUI(new CLine3DBatcher(true));
59+
std::unique_ptr<CLine3DBatcher> line3DPostFX(new CLine3DBatcher(true));
60+
std::unique_ptr<CLine3DBatcher> line3DPostGUI(new CLine3DBatcher(false));
61+
std::unique_ptr<CMaterialLine3DBatcher> materialLinePreGUI(new CMaterialLine3DBatcher(true));
62+
std::unique_ptr<CMaterialLine3DBatcher> materialLinePostFX(new CMaterialLine3DBatcher(true));
63+
std::unique_ptr<CMaterialLine3DBatcher> materialLinePostGUI(new CMaterialLine3DBatcher(false));
64+
std::unique_ptr<CPrimitive3DBatcher> primitive3DPreGUI(new CPrimitive3DBatcher(true));
65+
std::unique_ptr<CPrimitive3DBatcher> primitive3DPostFX(new CPrimitive3DBatcher(true));
66+
std::unique_ptr<CPrimitive3DBatcher> primitive3DPostGUI(new CPrimitive3DBatcher(false));
67+
std::unique_ptr<CMaterialPrimitive3DBatcher> materialPrimitivePreGUI(new CMaterialPrimitive3DBatcher(true, this));
68+
std::unique_ptr<CMaterialPrimitive3DBatcher> materialPrimitivePostFX(new CMaterialPrimitive3DBatcher(true, this));
69+
std::unique_ptr<CMaterialPrimitive3DBatcher> materialPrimitivePostGUI(new CMaterialPrimitive3DBatcher(false, this));
70+
std::unique_ptr<CPrimitiveBatcher> primitiveBatcher(new CPrimitiveBatcher());
71+
std::unique_ptr<CPrimitiveMaterialBatcher> primitiveMaterialBatcher(new CPrimitiveMaterialBatcher(this));
72+
std::unique_ptr<CScreenGrabberInterface> screenGrabber(NewScreenGrabber());
73+
std::unique_ptr<CPixelsManagerInterface> pixelsManager(NewPixelsManager());
74+
std::unique_ptr<CAspectRatioConverter> aspectRatioConverter(new CAspectRatioConverter());
75+
76+
m_pRenderItemManager = renderItemManager.release();
77+
m_pTileBatcher = tileBatcher.release();
78+
m_pLine3DBatcherPreGUI = line3DPreGUI.release();
79+
m_pLine3DBatcherPostFX = line3DPostFX.release();
80+
m_pLine3DBatcherPostGUI = line3DPostGUI.release();
81+
m_pMaterialLine3DBatcherPreGUI = materialLinePreGUI.release();
82+
m_pMaterialLine3DBatcherPostFX = materialLinePostFX.release();
83+
m_pMaterialLine3DBatcherPostGUI = materialLinePostGUI.release();
84+
m_pPrimitive3DBatcherPreGUI = primitive3DPreGUI.release();
85+
m_pPrimitive3DBatcherPostFX = primitive3DPostFX.release();
86+
m_pPrimitive3DBatcherPostGUI = primitive3DPostGUI.release();
87+
m_pMaterialPrimitive3DBatcherPreGUI = materialPrimitivePreGUI.release();
88+
m_pMaterialPrimitive3DBatcherPostFX = materialPrimitivePostFX.release();
89+
m_pMaterialPrimitive3DBatcherPostGUI = materialPrimitivePostGUI.release();
90+
m_pPrimitiveBatcher = primitiveBatcher.release();
91+
m_pPrimitiveMaterialBatcher = primitiveMaterialBatcher.release();
92+
m_pScreenGrabber = screenGrabber.release();
93+
m_pPixelsManager = pixelsManager.release();
94+
m_pAspectRatioConverter = aspectRatioConverter.release();
7795
}
7896

7997
CGraphics::~CGraphics()
8098
{
8199
if (m_pLineInterface)
100+
{
82101
m_pLineInterface->Release();
102+
m_pLineInterface = nullptr;
103+
}
104+
105+
// Ensure queued draw items release their allocations before we tear down batchers
106+
ClearDrawQueue(m_PreGUIQueue);
107+
ClearDrawQueue(m_PostGUIQueue);
108+
109+
if (m_pPrimitiveBatcher)
110+
m_pPrimitiveBatcher->ClearQueue();
111+
if (m_pPrimitiveMaterialBatcher)
112+
m_pPrimitiveMaterialBatcher->ClearQueue();
113+
if (m_pPrimitive3DBatcherPreGUI)
114+
m_pPrimitive3DBatcherPreGUI->ClearQueue();
115+
if (m_pPrimitive3DBatcherPostFX)
116+
m_pPrimitive3DBatcherPostFX->ClearQueue();
117+
if (m_pPrimitive3DBatcherPostGUI)
118+
m_pPrimitive3DBatcherPostGUI->ClearQueue();
119+
if (m_pMaterialPrimitive3DBatcherPreGUI)
120+
m_pMaterialPrimitive3DBatcherPreGUI->ClearQueue();
121+
if (m_pMaterialPrimitive3DBatcherPostFX)
122+
m_pMaterialPrimitive3DBatcherPostFX->ClearQueue();
123+
if (m_pMaterialPrimitive3DBatcherPostGUI)
124+
m_pMaterialPrimitive3DBatcherPostGUI->ClearQueue();
83125

84126
DestroyStandardDXFonts();
85127

128+
SAFE_RELEASE(m_pDXSprite);
129+
SAFE_RELEASE(m_pSavedStateBlock);
130+
SAFE_RELEASE(m_pSavedFrontBufferData);
131+
SAFE_RELEASE(m_pTempBackBufferData);
132+
86133
SAFE_RELEASE(m_ProgressSpinnerTexture);
87134
SAFE_RELEASE(m_RectangleEdgeTexture);
88135
SAFE_DELETE(m_pRenderItemManager);
@@ -1435,21 +1482,32 @@ bool CGraphics::CreateStandardDXFontWithCustomScale(eFontType fontType, float fS
14351482
bool CGraphics::LoadAdditionalDXFont(std::string strFontPath, std::string strFontName, unsigned int uiHeight, bool bBold, DWORD ulQuality,
14361483
ID3DXFont** ppD3DXFont)
14371484
{
1438-
int iLoaded = AddFontResourceEx(strFontPath.c_str(), FR_PRIVATE, 0);
1485+
int iLoaded = AddFontResourceEx(strFontPath.c_str(), FR_PRIVATE, 0);
1486+
bool bFontResourceAdded = (iLoaded > 0);
14391487

14401488
int iWeight = bBold ? FW_BOLD : FW_NORMAL;
14411489
*ppD3DXFont = NULL;
14421490

14431491
bool bSuccess = true;
1444-
// Normal size
14451492
if (!SUCCEEDED(D3DXCreateFont(m_pDevice, uiHeight, 0, iWeight, 1, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, ulQuality, DEFAULT_PITCH | FF_DONTCARE,
14461493
strFontName.c_str(), ppD3DXFont)))
14471494
{
14481495
WriteErrorEvent(SString("Could not create Direct3D font '%s'", strFontName.c_str()));
14491496
bSuccess = false;
14501497
}
14511498

1452-
return bSuccess && (iLoaded == 1);
1499+
if (!bSuccess && bFontResourceAdded)
1500+
{
1501+
RemoveFontResourceEx(strFontPath.c_str(), FR_PRIVATE, 0);
1502+
}
1503+
1504+
if (!bFontResourceAdded)
1505+
{
1506+
SAFE_RELEASE(*ppD3DXFont);
1507+
return false;
1508+
}
1509+
1510+
return bSuccess;
14531511
}
14541512

14551513
bool CGraphics::LoadAdditionalDXFont(std::string strFontPath, std::string strFontName, unsigned int uiHeight, bool bBold, ID3DXFont** ppD3DXFont)

0 commit comments

Comments
 (0)