David,

The best way to see if a block of memory can be allocated is to try to actually
allocate it and see if the attempt succeeds or not.

There's no good way for you to preflight the process. For instance, let's assume
that you write a hypothetical function as follows:

Bool OKToAllocate (UInt32 size)
{
    // Determine if we can safely allocate a chunk of size bytes
   ...
}

Then you probably see using it as:

if (OKToAllocate(fooBytes))
    hdl = MemHandleNew (fooBytes);

But this is not a safe approach. It is possible for another task to switch in
between the calls to OKToAllocate and MemHandleNew and allocate some memory of
its own. Thus, the result of OKToAllocate might be invalid, and your call to
MemHandleNew would fail.

But all of this might be a Red Herring anyway. If your application is crashing,
it's not because a call to MemHandleNew failed. MemHandleNew (or whatever) will
return NULL if it can't allocate the space. Before pursuing this avenue any
further, you should really determine why your application is crashing. *Then*
attack the known problem.

-- Keith






David Korus <[EMAIL PROTECTED]> on 10/15/99 12:36:11 PM

Please respond to [EMAIL PROTECTED]

Sent by:  David Korus <[EMAIL PROTECTED]>


To:   "'palm-dev-forum @3com.com'" <[EMAIL PROTECTED]>
cc:    (Keith Rollin/HQ/3Com)
Subject:  RE: how to monitor memory?




Well, unfortunately this would mean re-writing all my code using new memory
operations.  Also, my error may be completely unrelated to memory
whatsoever.  It would be much nicer from my standpoint to be able to call a
function to get the amount of free memory (or even check it through the
debugger).

Someone must have written some program to detect the amount of free memory.
Would anyone be willing to volunteer a solution to the problem of
determining the amount of free memory?  Or at least point me in the right
direction?  I'm not sure I completely understand the Palm memory model.
Thanks!

David Korus

-----Original Message-----
From: Roger Chaplin [mailto:[EMAIL PROTECTED]]
Sent: Thursday, October 14, 1999 7:04 PM
To: [EMAIL PROTECTED]
Subject: Re: how to monitor memory?


Here's an idea. Since the memory allocation routines do return a null
pointer if the allocation fails, can't you just do all of your memory
allocations for a particular operation up front, and bail out if any of
them fail? This may have the disadvantage of requiring more stack space
(for local variables to hold the handles) if you're doing many separate
allocations, but it should be very reliable.

--
Roger Chaplin
<[EMAIL PROTECTED]>






Reply via email to