Richard Coutts wrote:
>> What's the best way to detect and handle low or out of memory conditions?


(The following is applicable to CodeWarrior. It may also apply to the other
dev tools, but I don't have personal experience with them.)  Turn on the C++
exception handling option, then use the C++ exception syntax.

Your code:

    MemTest* test = new MemTest;
    if (test==NULL)
        ErrThrow( 0 );

"new" throws an exception when it fails.  It does not return NULL.  So, this
block will never be executed:

    if (test==NULL)
        ErrThrow( 0 );

(There is a way to make new return NULL rather than throw an exception.  Ben
Combee has an article about this at http://www.palmoswerks.com/.)

Here's a block that I've used to test my exception handling, and verifying
that exceptions are or are not thrown by new.  Using the standard new
operator, the DEBUGMSG("exception caught"); line is executed.

        try
                {       
                // Do something that will eventually cause an
                // exception to be thrown.  If not caught, 
                // this exception will cause the app to exit.
                
                UInt32 size = 0;
                Char * s = NULL;
                
                do
                        {
                        size += 32768;
                        
                        if (s != NULL)
                                {
                                delete [] s;
                                }
                                
                        s = new Char[size];
                        }
                while (s != NULL);

                // If this line below is executed, then "new" did not throw
                // an exception.        
                
                DEBUGMSG("new failed");
                }
        catch(bad_alloc)
                {
                DEBUGMSG("exception caught");
                }

HTH,

Jeff
 

-----Original Message-----
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of Richard
Coutts
Sent: Saturday, March 08, 2003 1:44 PM
To: Palm Developer Forum
Subject: Exception handling when out of memory

I'd like my app to detect the condition when a memory allocation fails and
respond by telling the user they're out of memory and then either freeing up
some or exiting gracefully.  In my tests with POSE, it seems that the device
is taking it upon itself to exit when the memory's depleted.  E.g., I have
the test code to fill the memory:

    class MemTest {
    public:
        UInt16 vals[10];
    };

    while(1) {
        MemTest* test = new MemTest;
        if (test==NULL)
            ErrThrow( 0 );
    }

Eventually POSE gives the error:

    Found 7248 memory leaks for *** (1.0).  Information concerning
    the leaks can be found in the log file.

I hit [Continue] and eventually POSE just exits my app.  I put a Breakpoint
at the ErrThrow line, and also within my ErrCatch, but the execution isn't
reaching either.

What's the best way to detect and handle low or out of memory conditions?

Thanks!
Rich


-- 
For information on using the Palm Developer Forums, or to unsubscribe,
please see http://www.palmos.com/dev/support/forums/




--
For information on using the Palm Developer Forums, or to unsubscribe, please see 
http://www.palmos.com/dev/support/forums/

Reply via email to