
Dear Dominique and Jack,
can you please try the following on your machines? My guess is that you will not see the "bad_alloc caught" output from a.out, in which case the question becomes:
Why isn't setrlimit working on Mac OS X?
All the best,
Roberto
$ cat bug.cc #include <new> #include <cstring> #include <cerrno> #include <sys/types.h> #include <sys/time.h> #include <sys/resource.h> #include <unistd.h> #include <iostream> #include <cstdlib>
#define LIMIT(WHAT) \ do { \ if (getrlimit(WHAT, &t) != 0) { \ std::cerr << "getrlimit failed: " << strerror(errno) << std::endl; \ exit(1); \ } \ t.rlim_cur = bytes; \ if (setrlimit(WHAT, &t) != 0) { \ std::cerr << "setrlimit failed: " << strerror(errno) << std::endl; \ exit(1); \ } \ } while (0)
void limit_memory(unsigned long bytes) { struct rlimit t; // Limit heap size. LIMIT(RLIMIT_DATA); // Limit resident set size. LIMIT(RLIMIT_RSS); // Limit mapped memory (brk + mmap). //LIMIT(RLIMIT_VMEM); // Limit virtual memory. LIMIT(RLIMIT_AS); }
int main() try { limit_memory(10000); (void) new char[20000]; std::cout << "no exception thrown" << std::endl; } catch (std::bad_alloc) { std::cout << "bad_alloc caught" << std::endl; } catch (...) { std::cout << "unknown exception thrown" << std::endl; } $ g++ -W -Wall bug.cc $ a.out bad_alloc caught $