How can I catch the memory error...

I'm writing a application which need to resize(or renew) a imge buffer's size by custom. So I must adjust
whether my app could provide the enough space to do this.
But in fact, after using whether SetHandleSize() or NewHandle(), I call the MemError() to get the error number, its value is always no changed,
How can I catch the error, or find another way?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Check the result that NewHandle returns.  If it returns NIL, the system was unable to allocate the requested amount of memory.  You won't find out about that from MemError().

SethandleSize() should return memFullErr (-108) if there is not enough memory.

What makes you think the memory allocation is failing?  How much memory are you (re)allocating?  What is the heap size of your application?  How much memory does your machine have?

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
bhzhaoAuthor Commented:
Thanks for your help. I also want to ask How I could know how many memory my app need at least, i.e how may empty memory should I left for my app. So I can limit the buffer size (re)allocated.

After using SetHandleSize(), it seemed it was success, but the application would crash when exit this procedure. I think the most possibale reason is that this block had covered parts of the stack of this app.
If you are using CodeWarrior, I would recommend investigating ZoneRanger to analyze your memory usage.  One way to estimate your app memory usage without the buffer increase is to profile it without that code, and see how much memory it takes.

I don't think SetHandleSize would have succeeded if your heap collided with the stack.  More likely you are getting the memory you allocate, but overwriting what you asked for.  Make sure, for example, that you aren't allocating N bytes and then writing out N longs.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.