Link to home
Start Free TrialLog in
Avatar of sacka
sacka

asked on

set_new_handler not working?

I'm trying to use a custom new_handler, in case the new operator cannot get enough memory for my object.
When I run the following code, instead of getting the error message I specify when I run out of memory, the program terminates with the output "Failed".

The code is:

#include <iostream>
#include <cstdlib>
#include <new>
using namespace std;

int counter = 0;

void out_of_memory() {
  cerr << "memory exhausted after " << counter
    << " allocations!" << endl;
  exit(1);
}

int main() {
  set_new_handler(&out_of_memory);
  while(1) {
    counter++;
    int* ia = new int[1000]; // Exhausts memory
  }
}

This code is from "Thinking in C++ 2nd Ed, Vol 1" by Bruce Eckel, available from www.BruceEckel.com (with slight modifications)

I'm running linux mandrake 9.0, and g++ version 3.2  (3.2-1mdk).

Can anyone explain why my program terminates without my new_handler being called?

thanks,

sacka
Avatar of Salte
Salte

I wouldn't use iostream in a memory handler the way you do.

It will typically try to allocate memory and since it is already inside memory allocation (out_of_memory is called before the memory allocation is done) it will just give up and write 'Failed' instead of doing the output to cerr.

Try to use very basic I/O, if you're on unix stick to write(2, msg,strlen(msg)); to do your output inside out_of_memory.

In Win32 you can also use the same write(2,msg,strlen(msg)) since it won't do any new.

You can also try a simple printf(stderr, ..); it might allocate memory but as it is a C function it will use malloc and not new.

Since you are out of memory it indicates that the run time is in trouble. You then have 2 options, either use non-C++ functions to do your I/O OR you can in the early stage of your program allocate a huge block of memory:

char * safety = new char[8192];

Then when you're out of memory you first do a delete [] safety and then you do your cerr etc....

so:

char * safety = 0;

int main()
{
   safety = new char[8192];
   ....
}

void out_of_memory()
{
   delete [] safety;
   cerr .....;
   exit(1);
}

Alf
Avatar of sacka

ASKER

Salte,

Thanks for your comment.

I tried using the safety method, with no success.
I also tried using printf, same thing.

Which header files do I need to include to call write(,,)?  I couldn't find documentation that mentioned that function.

Is there something weird about my system?  What results do you get running the code I posted initially?  Or the code with your suggestions?

thanks,

sacka
ASKER CERTIFIED SOLUTION
Avatar of Salte
Salte

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
works perfectly under mingw/gcc 3.2 and under redhat 7.2 and gcc 3.2.2.  what you have might be a compiler and/or a lib bug.  try to upgrade to 3.2.2

-b
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Answered: Points to Salte

Please leave any comments here within the next seven days. Experts: Silence
means you don't care.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

-bcl (bcladd)
EE Cleanup Volunteer