locked
Why does this report a memory leak? RRS feed

  • Question

  • Hi

    I have this constructor:

    CScriptRenderView::CScriptRenderView()
    : m_pmapSSVarsToPrompts(new CMapStringToString() ), 
    	m_pmapSSVarsToValues(new CMapStringToString() ) ... 

    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 ;
    }

    Yes, when my application terminates I see:

    Detected memory leaks!
    Dumping objects ->
    {462274} normal block at 0x000000B4A88601D0, 30 bytes long.
     Data: <HHÕ ÷           > 48 48 D5 81 F7 7F 00 00 02 00 00 00 02 00 00 00 
    {462273} normal block at 0x000000B499807C50, 48 bytes long.
     Data: <HHÕ ÷           > 48 48 D5 81 F7 7F 00 00 0B 00 00 00 0B 00 00 00 
    f:\dd\vctools\vc7libs\ship\atlmfc\src\mfc\plex.cpp(29) : {462272} normal block at 0x000000B4A7E2EE60, 328 bytes long.
     Data: <        h|  ´   > 00 00 00 00 00 00 00 00 68 7C 80 99 B4 00 00 00 
    f:\dd\vctools\vc7libs\ship\atlmfc\src\mfc\map_ss.cpp(83) : {462271} normal block at 0x000000B4978504A0, 136 bytes long.
     Data: <                > 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
    ScriptRenderView.cpp(209) : {461906} client block at 0x000000B4996D1AB0, subtype c0, 56 bytes long.
    f:\dd\vctools\vc7libs\ship\atlmfc\src\mfc\dumpcont.cpp(23) : atlTraceGeneral - an object at $000000B4996D1AB0, 56 bytes long
    ScriptRenderView.cpp(175) : {461904} client block at 0x000000B4997D0700, subtype c0, 56 bytes long.
    f:\dd\vctools\vc7libs\ship\atlmfc\src\mfc\dumpcont.cpp(23) : atlTraceGeneral - an object at $000000B4997D0700, 56 bytes long
    ScriptRenderView.cpp(209) : {459563} client block at 0x000000B49973FA00, subtype c0, 56 bytes long.
    f:\dd\vctools\vc7libs\ship\atlmfc\src\mfc\dumpcont.cpp(23) : atlTraceGeneral - an object at $000000B49973FA00, 56 bytes long
    Object dump complete.
    

    I don't know about the first 4 bits of detail, but the last 3 are my pointers. Why are they being listed?

    Andrew

    Friday, May 23, 2014 10:15 AM

Answers

  • 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

    Friday, May 23, 2014 10:24 AM