c++ - 如何在 C++ 中终止程序

标签 c++ memory-management

当我退出我的 C++ 程序时,它崩溃并出现如下错误:

EAccessViolation with mesage 'Access violation at address 0...

and

Abnormal Program Termination

它可能是由某些析构函数引起的,因为它只在应用程序退出时发生。我使用了一些外部库,但找不到导致它的代码。是否有强制程序立即退出的功能(类似于 Linux 中的 kill),以便操作系统必须释放内存?我可以在应用程序退出事件中使用此功能。

我知道这将是一个糟糕的解决方案,因为它只会隐藏问题。

我只是出于好奇而问,所以请不要给我 -1 :)

我尝试从 stdlib exit(0) 但它没有帮助。

编辑:

感谢您的无数回复:) 我使用 Builder C++ 6(我知道它已经过时但出于某些原因我不得不使用它)。我的应用程序使用库到神经网络 (FANN)。使用调试器我发现程序崩溃:

~neural_net()
{
    destroy();
}

destroy() 多次调用另一个函数 fann_safe_free(ptr),即:

#define fann_safe_free(x) {if(x) { free(x); x = NULL; }}

库运行良好,只有在清理时才会出现问题。这就是为什么我问如此残酷的解决方案。我的应用程序是多线程的,但其他线程对不同的数据进行操作。

我将第 n 次分析我的代码(错误一定在某处),感谢您的所有提示:)

最佳答案

你应该解决这个问题。

  • 第一步:在检查中找到您使用 atexit() 注册的所有函数(我希望数量不多)
  • 第二步:找到所有全局变量并检查它们的析构函数。
  • 第三步:查找所有静态函数变量并检查它们的析构函数。

但否则你可以中止。
注意:abort 用于异常程序终止。

abort()

区别:(注意让应用程序离开main函数等同于exit())

  • 退出()

    1. Call the functions registered with the atexit(3) function, in the reverse order of their registration. This includes the destruction of all global (static storage duration) variables.
    2. Flush all open output streams.
    3. Close all open streams.
    4. Unlink all files created with the tmpfile(3) function.
  • 中止()

    1. Flush all open output streams.
    2. Close all open streams.

关于c++ - 如何在 C++ 中终止程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3630530/

相关文章:

c++ - 突出显示 sql 中的搜索阶段

c++ - "operator = must be a non-static member"是什么意思?

c++ - BGL : how can I get from a bundled object to a vertex descriptor?

c++ - 按升序排列的 vector 元素

c++ - (Qt) QNetworkAccessManager 减慢其他应用程序

visual-studio-2008 - 在 Release模式下 Internet Explorer 8 崩溃后,我需要调试我的 BrowserHelperObject (BHO)(在 C++ 中使用 Visual Studio 2008)

java - OutOfMemoryException - 使用 Glide 的最佳方式

swift - Swift 中奇怪的内存行为

c++ - CPP 中字符串的内存管理问题

c++ - 基类构造函数的段错误