如何在没有 OOM 杀手的情况下发出 C++ 终止捕获
How to issue a c++ termination catch without a OOM killer
我正在从一本书中学习异常,try/catch 并且 OS 应该终止以下程序。
书上说,消息 terminate called after throwing an instance of 'std::bad_alloc'
应该出现。但是没有。
我正在使用 Arch Linux 并且程序没有停止。它运行,填充 RAM 有点线性直到它不(大约 90%),处理器工作很多但没有冻结也没有终止。
这是 Windows 的唯一用例,还是我如何在 Linux/maybe Unix 系统上重现错误?
#include <iostream>
#include <exception> //c++ exception
int main()
{
int *feld;
int loop = 1;
for(;;) //infinite loop
{
std::cout << "Loop number: " << loop << '\n';
try
{
feld = new int[10000];
loop++;
if (durchlauf == 100000) //since c++11
std::terminate();
}
catch(...)
{
std::cout << "Error, Program done.\n";
break;
}
}
return 0;
}
编辑:我发现我的 OOM 杀手无法正常使用 swap enabled/at all。但是c++有自己的终止进程调用
https://en.cppreference.com/w/cpp/error/terminate
它只是不会发出异常来打印出捕获线。
有没有人提示发出捕获终止?
我found以下代码供您编写一些终止程序:
希望对您有所帮助。
#include <iostream>
#include <stdexcept>
struct A {
int n;
A(int n = 0): n(n) { std::cout << "A(" << n << ") constructed successfully\n"; }
~A() { std::cout << "A(" << n << ") destroyed\n"; }
};
int foo()
{
throw std::runtime_error("error");
}
struct B {
A a1, a2, a3;
B() try : a1(1), a2(foo()), a3(3) {
std::cout << "B constructed successfully\n";
} catch(...) {
std::cout << "B::B() exiting with exception\n";
}
~B() { std::cout << "B destroyed\n"; }
};
struct C : A, B {
C() try {
std::cout << "C::C() completed successfully\n";
} catch(...) {
std::cout << "C::C() exiting with exception\n";
}
~C() { std::cout << "C destroyed\n"; }
};
int main () try
{
// creates the A base subobject
// creates the a1 member of B
// fails to create the a2 member of B
// unwinding destroys the a1 member of B
// unwinding destroys the A base subobject
C c;
} catch (const std::exception& e) {
std::cout << "main() failed to create C with: " << e.what();
}
只是为了在有人遇到相同问题时提供帮助
100000 次循环后编码抛出的异常:
#include <iostream>
#include <exception> //c++ exception
int main()
{
int *feld;
int loop = 1;
for(;;) //infinite loop
{
std::cout << "Loop number: " << loop << '\n';
try
{
feld = new int[10000];
loop++;
if (loop == 1e5)
throw std::bad_alloc(); //has to be inside the try(){} scope
}
catch(...)
{
std::cout << "Error, Program done.\n";
break;
}
}
return 0;
}
我正在从一本书中学习异常,try/catch 并且 OS 应该终止以下程序。
书上说,消息 terminate called after throwing an instance of 'std::bad_alloc'
应该出现。但是没有。
我正在使用 Arch Linux 并且程序没有停止。它运行,填充 RAM 有点线性直到它不(大约 90%),处理器工作很多但没有冻结也没有终止。 这是 Windows 的唯一用例,还是我如何在 Linux/maybe Unix 系统上重现错误?
#include <iostream>
#include <exception> //c++ exception
int main()
{
int *feld;
int loop = 1;
for(;;) //infinite loop
{
std::cout << "Loop number: " << loop << '\n';
try
{
feld = new int[10000];
loop++;
if (durchlauf == 100000) //since c++11
std::terminate();
}
catch(...)
{
std::cout << "Error, Program done.\n";
break;
}
}
return 0;
}
编辑:我发现我的 OOM 杀手无法正常使用 swap enabled/at all。但是c++有自己的终止进程调用 https://en.cppreference.com/w/cpp/error/terminate 它只是不会发出异常来打印出捕获线。 有没有人提示发出捕获终止?
我found以下代码供您编写一些终止程序: 希望对您有所帮助。
#include <iostream>
#include <stdexcept>
struct A {
int n;
A(int n = 0): n(n) { std::cout << "A(" << n << ") constructed successfully\n"; }
~A() { std::cout << "A(" << n << ") destroyed\n"; }
};
int foo()
{
throw std::runtime_error("error");
}
struct B {
A a1, a2, a3;
B() try : a1(1), a2(foo()), a3(3) {
std::cout << "B constructed successfully\n";
} catch(...) {
std::cout << "B::B() exiting with exception\n";
}
~B() { std::cout << "B destroyed\n"; }
};
struct C : A, B {
C() try {
std::cout << "C::C() completed successfully\n";
} catch(...) {
std::cout << "C::C() exiting with exception\n";
}
~C() { std::cout << "C destroyed\n"; }
};
int main () try
{
// creates the A base subobject
// creates the a1 member of B
// fails to create the a2 member of B
// unwinding destroys the a1 member of B
// unwinding destroys the A base subobject
C c;
} catch (const std::exception& e) {
std::cout << "main() failed to create C with: " << e.what();
}
只是为了在有人遇到相同问题时提供帮助 100000 次循环后编码抛出的异常:
#include <iostream>
#include <exception> //c++ exception
int main()
{
int *feld;
int loop = 1;
for(;;) //infinite loop
{
std::cout << "Loop number: " << loop << '\n';
try
{
feld = new int[10000];
loop++;
if (loop == 1e5)
throw std::bad_alloc(); //has to be inside the try(){} scope
}
catch(...)
{
std::cout << "Error, Program done.\n";
break;
}
}
return 0;
}