PDA

View Full Version : How to avoid console program crash when terminated too early



Xargo
09-30-2007, 01:03 PM
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

CornedBee
09-30-2007, 02:29 PM
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.

Xargo
10-01-2007, 01:31 PM
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++?

whiteflags
10-01-2007, 02:16 PM
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.

CornedBee
10-01-2007, 03:07 PM
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.

Xargo
10-03-2007, 01:37 PM
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.


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? ;)


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.

CornedBee
10-03-2007, 04:43 PM
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.