(一)初探new_handler
首先,C++的内存管理和异常处理是重点,也是交融的板块。在编写C/C++程序时,经常会malloc/new一块内存,大概就会有如下的代码:
//malloc:
#define SIZE 100
int *p = malloc(sizeof(int) * SIZE);
if( !p ){
//...
}
//new:
int *p = new int[SIZE];
if( !p ){
//...
}
or:
try{
int *p = new int[SIZE];
}
catch(...){
//...
}
在C++的某些旧版编译器会将new 失败的返回值置为null,但是一般是抛出std:bad_alloc异常,如果不捕捉这个异常,程序会abort。此外,new分配内存失败不仅仅是被try{}catch{}捕捉,还可以被new_handler捕捉。
一般来说,可用通过设置全局的new_handler:
#include <iostream>
#include <new>
using namespace std;
void New_Handler_Test(void)
{
cout << "New_Handler_Test" << endl;
}
int main(int argc, char *argv[])
{
std::set_new_handler(New_Handler_Test);//参数必须是 void func(void)型的函数
int *ptr = new int[100000000000L];
return 0;
}
运行如上代码:New_Handler_Test 会被一直打印,因为陷入了死循环。
这个时候可以灵活的在New_Handler_Test中直接throw std::bad_alloc(); 或者卸载New_Handler_Test等。
void New_Handler_Test(void)
{
cout << "New_Handler_Test" << endl;
#if BAD_ALLOC
throw std::bad_alloc();
#elif ABORT
std::abort();
#elif EXIT
std::exit();
#elif UNNEW_HANLER
std::set_new_handler(nullptr);
#endif
}
Note:即使正常退出New_Handler_Test, std::bad_alloc还是会被抛出,所以try{}catch{}和new_handler机制应该结合使用。
(二)定制对象的new_handler
在上面的实验中,一进入main就设置了一个全局的new_handler function,这会导致所有对象的new failed 都进入同一个new_handler function,这一定是不美的。下面进行定制:
#include <iostream>
#include <new>
using namespace std;
void new_handler_test(void)
{
cout << "[global]:I am a new handler!" << endl;
static int cnt = 0;
cnt++;
if( cnt >= 3 ) std::set_new_handler(nullptr);
}
void new_handler_testAPP(void)
{
cout << "[local]:I am a new handler!" << endl;
static int cnt = 0;
cnt++;
if( cnt >= 3 ) std::set_new_handler(nullptr);
}
class New_HandlerSupport{
public:
explicit New_HandlerSupport(std::new_handler phandler):handler(phandler){
cout << "New_HandlerSupport" << endl;
};
~New_HandlerSupport(){
cout << "~New_HandlerSupport" << endl;
set_new_handler(handler);
};
private:
std::new_handler handler;
New_HandlerSupport(const New_HandlerSupport &);
New_HandlerSupport & operator=(const New_HandlerSupport &);
};
class TestAPP{
public:
explicit TestAPP(){
cout << "TestAPP" << endl;
}
~TestAPP(){
cout << "~TestAPP" << endl;
}
static std::new_handler set_new_handler(std::new_handler phandler) throw();
static void * operator new(std::size_t size);
private:
static std::new_handler current_phandler;
int a[100000000000L];
};
std::new_handler TestAPP::current_phandler = nullptr;
std::new_handler TestAPP::set_new_handler(std::new_handler phandler) throw()
{
cout << "TestAPP set_new_handler" << endl;
std::new_handler old_phandler = current_phandler;
current_phandler = phandler;
return old_phandler;
}
void * TestAPP::operator new(std::size_t size)
{
cout << "TestAPP new" << endl;
New_HandlerSupport h(std::set_new_handler(current_phandler));
return ::operator new(size);
}
int main(int argc, char *argv[])
{
//global new_handler
//set_new_handler(new_handler_test);
//int *p = new int[100000000000L];
TestAPP::set_new_handler(new_handler_testAPP);
TestAPP *ptc = nullptr;
//ptc = new TestAPP(); //abtain a std::bad_alloc && abort this program
try{
ptc = new TestAPP();
}
catch(const std::bad_alloc& e){
std::cerr << e.what() << '\n';
}
cout << "delete the object" << endl;
delete ptc; //ptc can be nullptr
return 0;
}
以上为TestAPP定制了一个new_handler_testAPP function。//参考Effective C++:条款49:了解new_handler的行为
(三)复用定制版new_handler
在(二)中我们为一个类定制了new_handler,看起来是比价复杂的,如果每个对象都这么写岂不是很麻烦,那么复用版的就来了:
//...