Developer forums (C::B DEVELOPMENT STRICTLY!) > Development

app.cpp

(1/3) > >>

sethjackson:
Hi got a tiny question about app.cpp.

Why this?


--- Code: (cpp) ---void CodeBlocksApp::HideSplashScreen()
{
    if (m_pSplash)
        delete m_pSplash;
    m_pSplash = 0;
}

--- End code ---

I belive it should be this....


--- Code: (cpp) ---void CodeBlocksApp::HideSplashScreen()
{
    if (m_pSplash)
    {
        delete m_pSplash;
        m_pSplash = 0;
    }
}

--- End code ---

It seems to me that m_pSplash is getting set to 0 unnessecarily.....

Patch below fixes the problem (I think).....  :lol:

[attachment deleted by admin]

thomas:
If you don't zero the pointer after deleting, you may delete it twice (as HideSplashScreen can be called more often than once).

Deleting the same object twice is a MCA.


EDIT:
Hmm... after reading your post more carefully, you do zero the pointer, sorry ;)
Yes, you are maybe right, it might be zeroed more often than needed, but it really does not matter here.

Urxae:
Actually, it's perfectly OK to delete a null pointer. So maybe it should be

--- Code: (cpp) ---void CodeBlocksApp::HideSplashScreen()
{
    delete m_pSplash;
    if (m_pSplash)
        m_pSplash = 0;
}

--- End code ---
?

However, checking the if probably takes more time than the assignment would take, so

--- Code: (cpp) ---void CodeBlocksApp::HideSplashScreen()
{
    delete m_pSplash;
    m_pSplash = 0;
}

--- End code ---
is probably even better.


--- Quote from: thomas on January 24, 2006, 09:29:41 pm ---If you don't zero the pointer after deleting, you may delete it twice (as HideSplashScreen can be called more often than once).

--- End quote ---

All these versions set the pointer to null after a delete (except the first one above, but only if it was already null).


--- Quote ---Deleting the same object twice is a MCA.

--- End quote ---

MCA?

thomas:
Since HideSplashScreen is usually called exactly once, and in the exceptional case twice, I believe it really does not pay to spend time even thinking about one more pointer assignment :lol:


--- Quote from: Urxae on January 24, 2006, 09:33:16 pm ---MCA?
--- End quote ---
MCA = GAU

killerbot:

--- Quote ---void CodeBlocksApp::HideSplashScreen()
{
    delete m_pSplash;
    m_pSplash = 0;
}
--- End quote ---
That's it : deleting NULL pointers is safe in C++, see one of the Herb Sutter books.

Navigation

[0] Message Index

[#] Next page

Go to full version