Access violation at unusual program termination (C ++)

The following minimal code examples work fine when they end normally (by pressing enter):

#include <stdio.h>
#include <iostream>
#include <string>

using namespace std;

class SingletonLogClass
{

private:

    SingletonLogClass() {}

public:

    ~SingletonLogClass()
    {
        LogMessage("~SingletonLogClass()");
    }

    static SingletonLogClass& getInstance(void)
    {
        static SingletonLogClass inst;
        return inst;
    }

    void LogMessage(string msg)
    {
        //printf("%s\n", msg.c_str());
        cout << msg << endl;
    }

};

int main(int argc, char* argv[])
{
    SingletonLogClass::getInstance().LogMessage("This is a message");
    getchar();
    return 0;
}

When I exit the program by closing the console window, it depends on the implementation of the function LogMessage. If it is implemented using printf, everything will be fine. But when it is implemented using cout, I get an access violation.

Can someone explain this? What exactly happens when the program terminates by closing the console window? Why does he work with printf, but not with cout?

I work with VC ++ 2010.

+3
source share
1 answer

cout - . singleton ( getInstance) .

++ , . , , cout SingletonLogClass. SingletonLogClass -, cout ( printf ).

, ( enter) , , main, , , . .

+6

All Articles