Meaning of exit codes in my C++ MFC program

Posted on 2014-11-05
Last Modified: 2014-11-20
I am developing a dialog-based application in MFC using C++.   I am using Visual Studio 2013.  During debugging activities, I am used to seeing messages like "The thread 0x1648 has exited with code 20 (0x14)".  Messages like this appear in the "Output" window of Visual Studio, after the program has terminated.  I have poked around and come to believe that this exit code (20) is OK for a dialog based application.  But today I see this message: "The thread 0x568 has exited with code 32812 (0x802c)."  So I thought something went wrong.  

But three things puzzle me.  1)  There is no other indication that anything was wrong (no failures, exceptions, leaks, etc., reported).  2) I went back to a version of my program from a couple of weeks ago and it did the same thing.  In fact I re-ran about a dozen versions stretching back a month and they all had the same exit code.  This is really odd because they did not have that exit code at the time they were being developed and tested.  2) When I research exit codes, I started to wonder if it means anything at all.  Apparently my application sets this code in some manner (I'm not doing it explicitly).  

So, can you tell me if this means anything is wrong?  Is there a way to determine what exit code 32812 means?  Why does this sort of appear to be dependent not upon my application, but on my machine (i.e., the same program had an exit code of 20 a week ago).

Thanks very much for unraveling this mystery for me.
Question by:debrunson
  • 3
  • 2
  • 2
LVL 86

Expert Comment

ID: 40424839
>>  Is there a way to determine what exit code 32812 means?

If you have the source code - yes ;o)

Seriously, that can be any arbitrary, application-specific value that is passed to 'ExitThread()' ( or returned from the Thread function, which is documented as "The function should return a value that indicates its success or failure.". AFAIK there is no standardized list of codes, if you are lucky, values from 'winerror.h' are used.

Author Comment

ID: 40425018
Unforunately I do not know where or when my program sends information to ExitThread().  I do not have this instruction anywhere in my solution.

However there is new information to further expose my ignorance.  When I click the "X" in the upper right corner, I get an exit code of 20, which is what I have come to assume is normal.  But when I click my File/Exit menu item, I get the exit code 32812.  Perhaps you can explain something to me...

Long ago I overrode the OnClose() handler, which fires when the "X" is clicked.  This handler calls a cleanup method, then ends by calling the base class handler CDialog::OnClose();

However when File/Exit is clicked, I call the same cleanup method, but I then end with EndDialog(IDOK).  

I do this because I was under the impression that I should not send Windows system messages in my code, i.e. just as I would not send an "OnPaint()" message myself, I would not send an "OnClose()" message.  I similarly believe that I should not call the OnClose() handler myself.  So, I seem to be stuck with closing the app using CDialog::OnClose() on one hand, or EndDialog(IDOK) on the other.  Maybe neither is wrong and the exit code is just a reflection of the two different methods.

Your thoughts?
LVL 86

Accepted Solution

jkr earned 250 total points
ID: 40425081
It *could* well be like that, yes that's nothing I would rely on. MFC does not create a separate thread to handle dialogs and/or windows, it adheres strictly to a 'one single GUI thread' philosphy.
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

LVL 32

Assisted Solution

sarabande earned 250 total points
ID: 40426616
most developers would return 0 on success of a thread and error code on failure.

for handler functions they either are void or return 1 if they handled the messages or 0 if they should passed to the next handler (if any).


Author Comment

ID: 40426984
With your input I tried returning TRUE from my main app's InitInstance() routine.  This then returns an exit code of 0x0 regardless of how I close the dialog.  Possibly I am starting to understand.  So if an error bubbles up from the dialog and returns an error code to the main app, I can return from the main app with that error code to use in my diagnostics.  Does that sound right?

However someplace I read that I was supposed to return FALSE from the main app "so that we exit the  application, rather than start the application's message pump."  Does that make any sense?  Or is it OK to return TRUE from the main application?

Thank you.
LVL 32

Expert Comment

ID: 40455358
So if an error bubbles up from the dialog and returns an error code to the main app, I can return from the main app with that error code to use in my diagnostics.  Does that sound right?
yes. if you started the application in a batch file or from command window, any exit code different from 0 would be assigned to ERRORLEVEL what could be checked by command like 'if errorlevel 1 ...'. similar any launcher application can programmatically check the exit code of a launched and terminated program. on success the program should return 0 to keep it simple. so it rarely makes sense to return a dialog return code (for example IDCANCEL == 2) as exit code of the application as this is not an error. on the other hand it would make much sense to return a non-zero if the database could not be opened or the license has expired.


Author Comment

ID: 40455564
Thanks very much Sara.  I think I understand.

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now