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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7904669/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 17:41:34  来源:igfitidea点击:

C++ program exited with code 0 error

c++

提问by user1014833

I'm doing c++ at my job for the first time in years and am trying to track down a problem. 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

多年来,我第一次在工作中使用 C++,并试图找出问题所在。我编写了代码,用于枚举机器上运行的进程并返回性能指标。我的问题是发生了某种未处理的错误,在调试窗口中我收到一条消息,说程序已退出,代码为 0。这是主函数中的代码

    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. 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. Any help you could offer would be really appreciated.

该程序运行了很长时间,但过了一会儿,它又回来说它以代码 0 退出,但“App Stopped”行从未打印到日志中。有谁知道我可能遇到什么样的错误或可能发生什么问题?那个 try catch 块是否足以捕获可能发生的任何错误,或者我还能做些什么。您能提供的任何帮助将不胜感激。

EDIT: The log file should get 3 entries from here if it exits correctly. They are "Doing Process Loop" for the the "DoProcessLoop" Function, "Copying File" for the "CopyPerfFileToDest" function and the "App Stopped" if it stops correctly. 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. I was curious if there is a generic error trap I can do to catch any all errors.

编辑:如果日志文件正确退出,它应该从这里获得 3 个条目。它们是“DoProcessLoop”函数的“Doing Process Loop”、“CopyPerfFileToDest”函数的“Copying File”和“App Stopped”(如果它正确停止)。当我自己让它正确停止时,我得到所有 3 行,当它不正确停止时,我只会在日志中得到“Doing Process Loop”,然后它以代码 0 退出。错误必须在那里。我很好奇是否有一个通用的错误陷阱可以捕获所有错误。

回答by Alex F

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/en-us/library/6wdz5232.aspx

回答by Michael Shopsin

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. 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.

有时文件没有正确刷新,因此如果 AddToLog 函数在退出之前推迟写入文件,那么它可能不会写出值。您可以调试程序以查看是否发生了奇怪的事情,或者添加一个像 status 这样的变量并将其设置在您的 catch 函数中,然后在最后返回它,这样您就可以根据该值知道是否有错误。

回答by Sean

Try changing the return of addtolog with a boolean and surrounding add to log with:

尝试使用布尔值和周围的 add 更改 addtolog 的返回以记录:

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”,这可能是问题所在。