On 23/05/2014 12:15, Chuckie72 wrote:
Hi
I have this constructor:
CScriptRenderView::CScriptRenderView()
: m_pmapSSVarsToPrompts(new CMapStringToString() ),
m_pmapSSVarsToValues(new CMapStringToString() ) ...
Are these m_pmapSSVarsToXxxx just raw pointers? Have you considered using smart pointers, with automatic destruction?
(e.g. unique_ptr, or shared_ptr?)
> In the destructor:
CScriptRenderView::~CScriptRenderView()
{
if (m_pmapSSVarsToValues != NULL)
delete m_pmapSSVarsToValues ;
if (m_pmapSSVarsToPrompts != NULL)
delete m_pmapSSVarsToPrompts ;
if (m_pmapSSVarsToOptions != NULL)
delete m_pmapSSVarsToOptions ;
}
Note that if you use "delete", you don't need to check against NULL (or nullptr).
"delete" works just fine (i.e. does nothing) if the input pointer is nullptr.
The memory leaks may have different causes.
For example, an exception may be thrown somewhere, and the destructor of the CScriptRenderView class may not be called for some reason. So the pointer data members are not deleted.
Moreover, for your class you defined a destructor. In general, if you define a destructor, you may consider also defining a copy constructor and operator= (the so called "Rule of Three"). Or at least declare private copy constructor and copy assignment,
if your class is not copyable.
You may also consider following the instructions here:
http://msdn.microsoft.com/en-us/library/e5ewb1h3(v=vs.90).aspx
Giovanni