Skip to content

Commit bfd1306

Browse files
committed
Fix memory leak in main menu (News browser)
The new way is cascading delete of all parent's children, removed function had many issues including dangling parent pointer, double deletion risk, UAF Fixed memory leak: m_pButtonNewsLink
1 parent ea949b2 commit bfd1306

File tree

1 file changed

+8
-23
lines changed

1 file changed

+8
-23
lines changed

Client/core/CNewsBrowser.cpp

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ CNewsBrowser::CNewsBrowser()
2626
m_pWindow = NULL;
2727
m_pTabPanel = NULL;
2828
m_pButtonOK = NULL;
29+
m_pButtonNewsLink = NULL;
2930
}
3031

3132
////////////////////////////////////////////////////
@@ -199,31 +200,15 @@ void CNewsBrowser::CreateGUI()
199200
////////////////////////////////////////////////////
200201
void CNewsBrowser::DestroyGUI()
201202
{
202-
// Destroy
203-
for (uint i = 0; i < m_TabList.size(); i++)
204-
{
205-
CGUITab* pTab = m_TabList[i];
206-
if (pTab)
207-
delete pTab;
208-
}
209-
m_TabList.clear();
210-
for (uint i = 0; i < m_TabContentList.size(); i++)
211-
{
212-
CGUIWindow* pWindow = m_TabContentList[i];
213-
if (pWindow)
214-
{
215-
CGUIElement* m_pScrollPane = pWindow->GetParent();
216-
if (m_pScrollPane)
217-
{
218-
delete m_pScrollPane;
219-
}
220-
delete pWindow;
221-
}
222-
}
223-
m_TabContentList.clear();
224-
SAFE_DELETE(m_pTabPanel);
203+
// Clean up the main UI elements in reverse order of creation
204+
SAFE_DELETE(m_pTabPanel); // This will destroy all tabs and their children
225205
SAFE_DELETE(m_pButtonOK);
206+
SAFE_DELETE(m_pButtonNewsLink);
226207
SAFE_DELETE(m_pWindow);
208+
209+
// Clear the lists after the GUI objects are destroyed
210+
m_TabList.clear();
211+
m_TabContentList.clear();
227212
}
228213

229214
////////////////////////////////////////////////////

0 commit comments

Comments
 (0)