Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6014881
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T02:45:23+00:00 2026-05-23T02:45:23+00:00

The following minimum code examples runs fine when it is terminated normally (by pressing

  • 0

The following minimum code examples runs fine when it is terminated 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 terminate the program by closing the console window it depends on the implementation of the LogMessage function. If it’s implemented using printf everything is fine. But when it’s implemented using cout I get an access violation.

Can someone explain this?
What exactly happens when the program is terminated by closing the console window?
Why does it work with printf but not with cout?

I’m working with VC++ 2010.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T02:45:24+00:00Added an answer on May 23, 2026 at 2:45 am

    cout is a global object. Your singleton instance (defined as static in the getInstance static member function) is also a global object.

    In C++, you cannot have control over the order of construction, neither destruction of global objects. As such, it is possible that the cout object is destructed before your SingletonLogClass is. As the destructor of your SingletonLogClass logs something, it cannot use anymore cout (whereas printf is fine).

    The difference in behaviour when the program is terminated normally (pressing enter) and thus exiting the main function, and when the program is terminated abruptly by closing the shell comes from the order of destruction of globals. You cannot have control over the order of destruction of global objects.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using the following code to ping a website to check for connectivity.
I have the following code: progressBar1.Minimum = 0; progressBar1.Maximum = Results.Count; foreach (MyClass cls
The following piece of code attempts to create a map that shows the minimum
The following code splits a string and selects all of the alphanumeric words containing
I'm moving an HSlider thumb via the accelerometer. The following code works fine. The
Suppose I have the following: A region defined by minimum and maximum latitude and
I have notices following: if I have a project with minimum files in res
I have the following validation in a model: validates_length_of :description, :minimum => 2, :on
Very simple question: I want to optimize the following jQuery code with maximum readability,
Hey there, I have the following code: sVal = analogRead(potPin); // read the value

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.