Search code examples
c++heap-corruption

Locating memory errors in a C++ GUI


Or at least I think the problem involves some kind of memory error. I'm making a program in SFML and I'm currently working on the menus using a GUI class that I made just for SFML. Internally, the GUI class uses std::shared_ptr to manage all of its internal pointers. The program consistently crashes after main() exits and all global destructors have been called, and gdb says a break point was triggered in ntdll!WaitForAlpCompletion, which leads me to believe that the problem is memory corruption. Whenever I remove the GUI instantiation from the menu function, it exits and closes with no errors. This seems to indicate GUI as the cause of the crash, except that sub-menus which create and destroy their own instances of GUI can be called and exited without any crashes or break points.

Some psuedocode:

SubMenu
{
    Create GUI
        Do Menu
    Destroy GUI
}

Menu
{
    Create GUI
        Do Menu?SubMenu
    Destroy GUI
}

main
{
    Init Stuff
    Menu
    UnInit Stuff
    Destroy GUI
    return 0
}

//after return
Global Dtors
Breakpoint triggered???

I'm at a loss as to what this could be. I plan on using some memory debugger like valgrind sometime today, but I was wondering if anyone else had any ideas on what this could be.


Solution

  • A heap corruption can be caused with this code:

    int main()
    {
        int *A(new(std::nothrow) int(10));
        int *B(A);
    
        delete B;
        delete A;
    }
    

    Does any of your code contain this similar situation?