From 5ac26f43bdb6a8b42aaf67748a9997cafcf0a041 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Fri, 21 Feb 2014 00:51:58 +0000 Subject: [PATCH] Fix memory leak in wxRibbonToolBar in case of error. Use wxScopedPtr to ensure the tool is not leaked, even if we fail to insert it. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@75959 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- src/ribbon/toolbar.cpp | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/ribbon/toolbar.cpp b/src/ribbon/toolbar.cpp index fe337405f4..86b094ac41 100644 --- a/src/ribbon/toolbar.cpp +++ b/src/ribbon/toolbar.cpp @@ -20,6 +20,7 @@ #include "wx/ribbon/art.h" #include "wx/ribbon/bar.h" #include "wx/dcbuffer.h" +#include "wx/scopedptr.h" #ifndef WX_PRECOMP #endif @@ -244,7 +245,7 @@ wxRibbonToolBarToolBase* wxRibbonToolBar::InsertTool( wxASSERT(bitmap.IsOk()); // Create the wxRibbonToolBarToolBase with parameters - wxRibbonToolBarToolBase* tool = new wxRibbonToolBarToolBase; + wxScopedPtr tool(new wxRibbonToolBarToolBase); tool->id = tool_id; tool->bitmap = bitmap; if(bitmap_disabled.IsOk()) @@ -270,8 +271,10 @@ wxRibbonToolBarToolBase* wxRibbonToolBar::InsertTool( size_t tool_count = group->tools.GetCount(); if(pos <= tool_count) { - group->tools.Insert(tool, pos); - return tool; + // Give the ownership of the tool to group->tools + wxRibbonToolBarToolBase* const p = tool.release(); + group->tools.Insert(p, pos); + return p; } pos -= tool_count + 1; }