This is Unofficial EPICS BASE Doxygen Site
forceBadAllocException.cpp
Go to the documentation of this file.
1 /*************************************************************************\
2 * Copyright (c) 2002 The University of Chicago, as Operator of Argonne
3 * National Laboratory.
4 * Copyright (c) 2002 The Regents of the University of California, as
5 * Operator of Los Alamos National Laboratory.
6 * EPICS BASE Versions 3.13.7
7 * and higher are distributed subject to a Software License Agreement found
8 * in file LICENSE that is included with this distribution.
9 \*************************************************************************/
10 //
11 // On older version of ms visual c++ operator new
12 // does not throw a bad_alloc exception
13 // when it fails. It simply returns a null pointer.
14 // This behavior is not in conformance with the
15 // ANSI / ISO C++.
16 //
17 #if _MSC_VER > 1000 && _MSC_VER < 1400
18 
19 #include <new>
20 #include <new.h>
21 
22 // instruct loader to call this gllobal object
23 // constructor before user global object constructors
24 #pragma warning (disable: 4073)
25 #pragma init_seg(lib)
26 #pragma warning (default: 4073)
27 
28 static int my_new_handler (size_t size)
29 {
30  throw std::bad_alloc();
31  return 0;
32 }
33 
34 class my_new_handler_obj
35 {
36 public:
37  my_new_handler_obj()
38  {
39  //_old_new_mode = _set_new_mode(1); // cause malloc to throw like new
40  _old_new_handler = _set_new_handler(my_new_handler);
41  }
42 
43  ~my_new_handler_obj()
44  {
45  _set_new_handler(_old_new_handler);
46  //_set_new_mode(_old_new_mode);
47  }
48 
49 private:
50  _PNH _old_new_handler;
51  //int _old_new_mode;
52 };
53 
54 static my_new_handler_obj _g_new_handler_obj;
55 
56 #endif