Re: throwing out of memory exception in c++ doesnt work

From: Stephan (stephas_at_frogking.stephas.homelinux.org)
Date: 03/01/04


Date: Mon, 01 Mar 2004 05:12:56 GMT

On Mon, 01 Mar 2004 04:30:48 +0000, Leor Zolman wrote:

>
>
> The subject of your post is "throwing out of memory exception in c++
> doesn't work", but you've done precisely what it takes to /prevent/ such an
> exception from ever being thrown: you've created and installed a
> new_handler that doesn't follow the rules of what a new_handler should do,
> which is one of the following ( I'm plagiarizing this from Dinkumware's C++
> Reference):
>
> make more storage available for allocation and then return
> call either abort() or exit(int)
> throw an object of type bad_alloc
>
> If you've got a reasonably healthy quantity of virtual memory available,
> though, it may take quite a while to thrash itself to exhaustion before it
> reaches the point of calling your new_handler, so let's speed things up a
> bit and add some exception handling:
>
> #include <iostream>
> #include <exception>
> #include <stdexcept>
>
> using namespace std;
>
> void out_of_mem() {
> cerr << "in out_of_mem: problem\n";
> throw bad_alloc();
> }
>
> int main() {
>
> set_new_handler(out_of_mem);
>
> try {
> while(1) {
> cout << "Top of loop" << endl;
> int * tst = new int[800000000]; // make this too much!
> if (!tst)
> cout << "end of loop: problem\n";
> }
> }
> catch (const exception &e)
> {
> cerr << "Caught: " << e.what() << endl;
> exit(1);
> }
>
> return 0; // not likely to get here!
> }
>
>
> Output (MSVC 7.1):
>
> Top of loop
> in out_of_mem: problem
> Caught: bad allocation
>
>
> So after you "handle" the exceptions and make sure your new_handler does
> one of the right things, you'll still have the virtual memory thrashing
> problem to contend with. I can't help you there, but you definitely want to
> do a better job of avoiding memory leaks than your test program did (yeah
> yeah, just kidding), and perhaps just "know" how much memory you can
> allocate before the thrashing commences and try to keep track of what
> you've allocated.
>
> Good luck,
> -leor
>
> Leor Zolman
> BD Software
> leor@bdsoft.com
> www.bdsoft.com -- On-Site Training in C/C++, Java, Perl & Unix
> C++ users: Download BD Software's free STL Error Message
> Decryptor at www.bdsoft.com/tools/stlfilt.html

thanks for the quick post!!
and your code seems to make a lot more sense now, and the exception gets
thrown, when allocating the array of ints, but I am trying to allocate a
couple small int arrays, and than it will just say Killed and I have no
ide why it didnt go into the handler.
thanks for the good try block/catch block example (although I have no idea
where the exception data type comes from, and I guess I want to avoid it?)
thx
Stephan



Relevant Pages

  • StackOverflowException
    ... The exception is thrown when I attempt to allocate an unmanaged char array ...
    (microsoft.public.dotnet.languages.vc)
  • Re: memory leak
    ... MSFT have already said that they will fix this problem, ... What is your definition of "video RAM"? ... You could try to allocate and have it throw if you're out of virtual memory. ...
    (microsoft.public.dotnet.framework.compactframework)
  • Windows array allocation problem
    ... I'm getting an "insufficient virtual memory" error when trying to allocate an array to a size which is about 385 MB less than the amount of available virtual memory. ...
    (comp.lang.fortran)
  • Re: Windows array allocation problem
    ... allocate an array to a size which is about 385 MB less than the amount of available virtual memory. ... In both cases a call to Windows API function GlobalMemoryStatus or GlobalMemoryStatusEx immediately before the ALLOCATE statement returns a value of available virtual memory of 2,118,221,824 bytes. ... So I'm getting an "insufficient virtual memory" failure when trying to allocate the array to a size which is 384.665 MB less than the amount of reported available virtual memory. ...
    (comp.lang.fortran)
  • Re: Windows array allocation problem
    ... allocate an array to a size which is about 385 MB less than the amount of available virtual memory. ... In both cases a call to Windows API function GlobalMemoryStatus or GlobalMemoryStatusEx immediately before the ALLOCATE statement returns a value of available virtual memory of 2,118,221,824 bytes. ... So I'm getting an "insufficient virtual memory" failure when trying to allocate the array to a size which is 384.665 MB less than the amount of reported available virtual memory. ...
    (comp.lang.fortran)