0

I have a small game made in visual studio 2013, written in c++. It creates a window in which it draws several things and pops up the console I used to debug. The problem is: if I click on the "exit" button from the menu I introduced, all things good, the process is terminated as it should be; if I press the 'X' (close) button on top of the window, it closes the window, but the console still remains. As it should be excepted, not having the console enabled doesn't fix anything: there are no windows associated with the game, but the sound still plays and the process still exists in task manager. I've searched a lot about this and I couldn't find a solution for this issue.

I've tried with atexit, but the program doesn't go to the function written for atexit. Maybe I'm placing it wrong, I can't tell. This is what I've tried.

void close_win() {
    if (engine)
        engine->drop();
    /*if (SC_CLOSE) {
        cout << "sc_close";
        _exit(0);
    }*/
    cout << "Ceva ";
    exit(0);
}

int main(int argc, char** argv) {
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
    glutInitWindowSize(800, 600);
    glutInitWindowPosition(100, 100);
    glutCreateWindow("Aarghhh! O ramaaa !");

    createMenu();     
    glClearColor(0.0, 0.0, 0.0, 0.0);
    init();
    glutReshapeFunc(reshape);
    glutDisplayFunc(dreptunghi);
    glutSpecialFunc(player);
    glutMainLoop();
    atexit(close_win);

    return 0;
}

or

    BOOL WINAPI ConsoleHandler(DWORD CEvent)
{
    if (CEvent == CTRL_CLOSE_EVENT)
        {
            cout << "You are closing my program.";
        }
        return TRUE;
    }   if (SetConsoleCtrlHandler((PHANDLER_ROUTINE)ConsoleHandler, TRUE) == FALSE)
        {
            cout << "The handler is not going to work.";
        }

or

if (glutGetWindow == 0)
    exit(0);

The game was made without classes, using OpenGL and GLUT libraries, if it matters for anything.

Also, when I set a breakpoint to atexit and use step over, after 2 steps the debug stops with "sehprolg4.asm not found". Does any of this has to do with the fact that the function doesn't call close_win()?

  • If you do not process the `X` button click it will only close the window, it will not close/terminate any other process, including the process that created the window. You have to process the `X` clicks the same way you process the `exit` button. – SunKnight0 Oct 30 '15 at 19:43
  • I realised that, but I don't know how. What's the associated variable for the 'X' button? And if I have, let's say CloseButton, how do I process it, like a normal input from the keyboard (GLUT_KEY_DOWN for exemple)? – Alina Voicu Oct 30 '15 at 19:58
  • I don't know. I use standard Win32 for Windows C++, I don't have knowledge of OpenGL or GLUT. In a standard program one would process the WM_CLOSE message. – SunKnight0 Oct 30 '15 at 20:12
  • Can you give me an example of how you would process it? – Alina Voicu Oct 30 '15 at 20:23
  • Try moving the `atexit(close_win)` call one line up, before `glutMainLoop()`. – rodrigo Oct 30 '15 at 20:26
  • @Alina Voicu: An example of how I would do it will not help you. Win32 message processing code it very different from what you are using. – SunKnight0 Oct 30 '15 at 20:34
  • @rodrigo I tried, it's still the same. SunKnight0 I see. Well, thanks for the answers anyway! – Alina Voicu Oct 30 '15 at 20:37
  • `atexit()` registers a function to be called when the program exits. So you must call it before entering the main loop (before `glutMainLoop()`). Also, you must not call `exit()` from your `close_win()` or you may get an infinite loop. And maybe your `engine->drop();` is hanging, that `sehprolg4.asm` thing sound a lot like SEH (Structured Exception Handling) and that is because something is going wrong. – rodrigo Oct 30 '15 at 20:53
  • @rodrigo If I don't call 'exit()', what method should I use to kill the process? I moved the message to be the first line in the 'close_win()' function, but it's still not displayed, so it's not because of the 'engine->drop()' line; the function just isn't called by 'atexit'. I figured something is wrong, but I don't know what so I can fix it. – Alina Voicu Oct 30 '15 at 21:03
  • Closing the glut window should call `exit(0)` automatically, that is handled by the glut internals, so in theory you need nothing here. The `cout` part not showing may be because you need an `endl`: `cout << "close_win" << endl;`. And maybe your calling manually `exit(0)` works but the glut one does not because your `drop()` function accesses the main window or GL resources or something like that. – rodrigo Oct 30 '15 at 21:06
  • @rodrigo I've commented the drop line and now that i've placed std::cout << "Ceva "< – Alina Voicu Oct 30 '15 at 21:12
  • @AlinaVoicu: Look at [this answer](http://stackoverflow.com/questions/127386/in-visual-studio-c-what-are-the-memory-allocation-representations). `0xFEEEFEEE` is the pattern used by the MS debug library to fill freed memory. And `0xC0000005` is the memory access violation exception. So you are most likely using a pointer stored in a freed structure. Conclusion: you have a bug elsewhere in the code **or** you are using glut/gl from `drop()`. – rodrigo Oct 30 '15 at 21:19

0 Answers0