Menu

Why does this give a resourceLeak error?

2015-06-15
2015-06-23
  • Sander Bouwhuis

    Sander Bouwhuis - 2015-06-15

    Why does the following code produce a resourceLeak error for hTileFont?
    I allocate memory with CreateFont() and deallocate with DeleteObject().

    ~~~~~~
    HFONT hTileFont = NULL;

    // ...

    hTileFont = CreateFont(i32TileFontHeight, 0, 0, 0, i32TileFontWeight, false, false, false, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, PROOF_QUALITY, VARIABLE_PITCH | FF_DONTCARE, pwcFontFace);

    // ...

    // Create the tiles
    InitializeTile(&m_btnAccessControlAccessControl, MODULE_ACCESSCONTROL, L"Access_control", pwcIniFile, true, hTileFont, bTileBorder);

    // ...

    // Free the resources
    if(hTileFont)
    DeleteObject(hTileFont);

     
  • Daniel Marjamäki

    sounds like a bug. but I fail to reproduce. I use this command:

    $ ./cppcheck --platform=win32A --enable=style 1.c
    

    Tried with this code:

    void f() {
        HFONT hTileFont = NULL;
        hTileFont = CreateFont(i32TileFontHeight, 0, 0, 0, i32TileFontWeight, false, false, false, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, PROOF_QUALITY, VARIABLE_PITCH | FF_DONTCARE, pwcFontFace);
        InitializeTile(&m_btnAccessControlAccessControl, MODULE_ACCESSCONTROL, L"Access_control", pwcIniFile, true, hTileFont, bTileBorder);
        if(hTileFont)
            DeleteObject(hTileFont);
    }
    
     
  • Sander Bouwhuis

    Sander Bouwhuis - 2015-06-19

    I have it on a lot of places in my source code. Every time it thinks there is a resource leak. I don't understand why.

    I use the GUI on Windows. Maybe that makes a difference?

     

    Last edit: Sander Bouwhuis 2015-06-19
  • Daniel Marjamäki

    It is probably some detail there in your code that makes Cppcheck confused.

    If you copy paste a function with FP to a different file and check that.. does it show the warning still? If it does then it's some detail in the function. Otherwise it seems there is something about a function call or type etc.

    I would like that you reduce your code by repeating these steps:
    1. remove some code
    2. run cppcheck on the file
    3. if the FP is not shown => undo changes

    Start with large chunks of code. I often start by removing #includes. then I remove large blocks of code such as types/functions/etc.. when I do this, I remove stuff until every statement, every identifier and every operation etc is needed to reproduce the FP.

     
  • Daniel Marjamäki

    I use the GUI on Windows. Maybe that makes a difference?

    It should not make a difference.

    Can you try to reproduce a FP with the command line cppcheck also? Try some command such as:

    cppcheck --platform=win64 --library=windows <filename>
    
     
  • Sander Bouwhuis

    Sander Bouwhuis - 2015-06-23

    Ooooooooooooooooh, shoot! I understand now.

    The problem is that there is a return in the function when an error occurs. It should never happen, but if it occurs then I return out of the function. The hTileFont won't be deallocated then (actually, the application shuts down when this function fails so nothing matters then anymore).

    Thanks for the help.

     

    Last edit: Sander Bouwhuis 2015-06-23

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.