简体   繁体   English

C ++程序退出,代码为0错误

[英]C++ program exited with code 0 error

I'm doing c++ at my job for the first time in years and am trying to track down a problem. 几年来,我第一次从事C ++工作,并试图找出问题所在。 I wrote code that goes out and enumerates the processes running on a machine and returns performance metrics. 我编写了代码,列举了在计算机上运行的进程并返回性能指标。 My problem is that some sort of unhandled error occurs and in the debug window I get a message saying the program has exited with code 0. Here is the code in the main function 我的问题是发生某种未处理的错误,在调试窗口中,我收到一条消息,指出程序已退出,代码为0。这是main函数中的代码

    int _tmain(int argc, _TCHAR* argv[])
    {
        while(nRun == 1)
        {
            try
            {
                WriteHeartBeat();
                DoProcessLoop(dwTotalRAM, nCheckPause, oPMeter, cFileName, oProcess, oCPUUsage, nProcCount, ddsCaps2, lpDD);
                CopyPerfFileToDest(cFileName);
                nRun = 1;
                tEnd = time(NULL);
            }catch(...){
                AddToLog("Error in Main Function");
            }
        }

    AddToLog("App Stopped");
    return 0;
    }

The program runs for a long time but after a while it just comes back saying it exited with code 0 but that "App Stopped" line is never printed into the log. 该程序运行了很长时间,但过了一会儿又回来了,说它以代码0退出了,但是“ App Stopped”这一行从不打印到日志中。 Does anyone know what kind of error I could have or what issue could be occuring? 有谁知道我可能会发生哪种错误或会发生什么问题? Is that try catch block sufficient enough to catch any error that could occur or is there something else I could do. try catch块是否足以捕获可能发生的任何错误,或者还有其他我可以做的事情。 Any help you could offer would be really appreciated. 您能提供的任何帮助将不胜感激。

EDIT: The log file should get 3 entries from here if it exits correctly. 编辑:如果正确退出,日志文件应从此处获取3个条目。 They are "Doing Process Loop" for the the "DoProcessLoop" Function, "Copying File" for the "CopyPerfFileToDest" function and the "App Stopped" if it stops correctly. 它们是“ DoProcessLoop”函数的“正在执行过程循环”,“ CopyPerfFileToDest”函数的“正在复制文件”,如果正确停止,则是“应用程序已停止”。 When I make it stop correctly myself I get all 3 lines, when it is stopping incorrectly I only get "Doing Process Loop" in the log and then it exits with code 0. The error must be in there. 当我自己使它正确停止时,我得到了所有3行,当它错误地停止时,我仅在日志中得到“正在执行过程循环”,然后退出并显示代码0。该错误必须在其中。 I was curious if there is a generic error trap I can do to catch any all errors. 我很好奇是否有通用错误陷阱可以捕获所有错误。

This can happen if one of functions called from _tmain called exit(0): 如果从_tmain调用的函数之一称为exit(0),则会发生这种情况:

http://www.cplusplus.com/reference/clibrary/cstdlib/exit/ http://www.cplusplus.com/reference/clibrary/cstdlib/exit/

http://msdn.microsoft.com/en-us/library/6wdz5232.aspx http://msdn.microsoft.com/zh-CN/library/6wdz5232.aspx

Sometimes files are not flushed right so if the AddToLog function defers writing to a file right before exit then it might not write out the value. 有时文件没有被正确刷新,因此,如果AddToLog函数推迟在退出前立即写入文件,则它可能不会写出该值。 You can debug the program to see if something strange is happening, or add a variable like status and set it in your catch function then return it at the end, so you know based on the value if there was an error. 您可以调试程序以查看是否发生了奇怪的情况,或者添加诸如status之类的变量并将其设置在catch函数中,然后在最后将其返回,这样您就可以根据该值知道是否有错误。

Try changing the return of addtolog with a boolean and surrounding add to log with: 尝试使用布尔值更改addtolog的返回值,并使用以下命令将周围的add更改为日志:

boolean logged=false;
while(!logged){
  logged = AddToLog("App Stopped");
}

This should prevent the program from exiting until "App Stopped" is written, which may be the problem. 这样可以防止程序在编写“ App Stopped”之前退出,这可能是问题所在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM