How to avoid console program crash when terminated too early

This is a discussion on How to avoid console program crash when terminated too early within the A Brief History of Cprogramming.com forums, part of the Community Boards category; Hi ho, I've just started working with C++. But I do know C# already, so it shouldn't be too much ...

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    3

    How to avoid console program crash when terminated too early

    Hi ho,

    I've just started working with C++. But I do know C# already, so it shouldn't be too much of a problem. Anyway, when you create a console program, and it's terminated too early because the user closed the window, it returns an error. My Borland/CodeGear C++ Builder even crashes, which is extremely weird (when I used to work with C#, whatever happened with the program, Visual Studio 2005 itself would never crash).

    Can anyone help me with this?

    Thanks in advance,
    Xargo

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    I've moved this thread to General Discussion because it's an IDE issue and has nothing to do with the language itself.

    First, about the C++ Builder behaviour, that's a clear bug in the IDE and you should send a bug report to CodeGear.

    As for what a program can do if its console is forcibly closed, the answer is, nothing. A pure standard C++ program isn't even aware of having a console - it has standard input and output, and they could be anything.
    A Win32-specific program might detect its console being closed, but what would it do about it? Creating a new console and attaching itself to it might be possible, but it would also be extremely user-unfriendly. The user closed the console for a reason, and might not be too happy to see it come back to life.
    As for intercepting the close and preventing it (or asking the user for confirmation), I don't think that's possible either, because the console doesn't really belong to the process that runs in it.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    3
    Are you sure the crashing of the program itself, even in standalone mode (without debugging or whatever), when the user closes the console, is because of the CodeGear/Borland IDE? I've actually seen many console applications do this. Especially small C++ programs, mess it all up when the user terminates the console manually (had it many times with several C++ console applications made by several programmers).

    If they'd all do this, it's ok, then it's just cuz it is like that. But for example C# doesn't have that problem. Of course C# is built on the .NET Framework, but does that matter? If C# is capable of doing it, why not C++?

  4. #4
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,525
    I think this is just a misunderstanding of Windows' behavior when it comes to the console. If you manually close the console while it is running an executable, then in order to do what you expect the console has to quit everything it's doing in a hurry. Often this gets interpreted as an error with the console because that's pretty much all Windows knows about the actual executable you tried to run, and it doesn't really know what else to do about the exit status that was returned from *your* C++ program.

    C++ is not part of the .NET framework because the language predates it. I admittedly know nothing about the .NET framework, but it really has nothing to do with anything in this case.

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    All I'm saying is that if Borland crashes if you close the console your program is running it, that's a Borland bug. There can be absolutely no doubt about it, and it has really nothing to do with the C++ program.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    Registered User
    Join Date
    Sep 2007
    Posts
    3
    Quote Originally Posted by citizen View Post
    I think this is just a misunderstanding of Windows' behavior when it comes to the console. If you manually close the console while it is running an executable, then in order to do what you expect the console has to quit everything it's doing in a hurry. Often this gets interpreted as an error with the console because that's pretty much all Windows knows about the actual executable you tried to run, and it doesn't really know what else to do about the exit status that was returned from *your* C++ program.
    Owkay, thanks for the information.

    Quote Originally Posted by citizen View Post
    C++ is not part of the .NET framework because the language predates it. I admittedly know nothing about the .NET framework, but it really has nothing to do with anything in this case.
    If the C++ I'm doing right now would be .NET I won't even waste my time on it. The reason I am doing C++ now, is because I want to get rid of the dependance of the .NET framework, which makes your app exclusively for Windows XP and higher. (A pity though, .NET is actually quite nice. If Microsoft would only be working a little more open-source-ish, more cross-platform-ish, and less-monopoly-ish; I would use .NET for almost everything. Although it still has some disadvantages of course (like performance).)

    By the way, you do know that C++.NET also exists?

    Quote Originally Posted by CornedBee View Post
    All I'm saying is that if Borland crashes if you close the console your program is running it, that's a Borland bug. There can be absolutely no doubt about it, and it has really nothing to do with the C++ program.
    Ok.

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    Quote Originally Posted by Xargo View Post
    By the way, you do know that C++.NET also exists?
    Not really. There are the Managed Extensions for C++ and there's C++/CLI. Both are sometimes referred to as C++.NET, but such a thing doesn't really exist.
    Then there's Visual C++.Net, but that's the IDE.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. MSVC++2008 console program portability
    By CodeMonkey in forum Windows Programming
    Replies: 1
    Last Post: 11-18-2008, 02:13 AM
  2. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 10:17 PM
  3. DOS program versus DOS console program
    By scromer in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 01-10-2002, 12:42 PM
  4. semaphores using C++ in console program
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 12-20-2001, 11:46 AM
  5. Console Program Problem
    By Breach23 in forum C++ Programming
    Replies: 3
    Last Post: 10-19-2001, 12:35 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21