Solved

Handling of memory allocation failures

Posted on 2000-04-01
5
298 Views
Last Modified: 2013-11-20
I just tried to set my own routine to handle memory allocation failures in
an MFC application. I used _set_new_handler function, but it did not work.
When the application ran out of memory my routine was not entered. Instead a
message "Out of memory" appeared. I guess this is some standard handling
done by MFC, however I was not able to find where it is done and how I can
change it.
0
Comment
Question by:ewajoz
5 Comments
 
LVL 1

Expert Comment

by:kkarunakar
Comment Utility
Hi there ,
This is keshav from India. Plese make ur question clear.How do u use the _set_new_handler();
For giving any comment on this Pls give some code also..
Regards..keshav
0
 
LVL 23

Accepted Solution

by:
chensu earned 100 total points
Comment Utility
The exception-handling function specified with the _set_new_handler function is called only if the new operator (and malloc if the _set_new_mode function specifies so) fails to allocate memory. It won't be called when Win32 API functions fail to allocate memory, such as VirtualAlloc, GlobalAlloc.
0
 
LVL 3

Expert Comment

by:Try
Comment Utility
"chensu", OK, he found out the hard way that "_set_new_handler" does not work the way he applied it, but you forgot to tell him how "to handle memory allocation failures in an MFC application."  You only told him why it didn't work!
0
 
LVL 23

Expert Comment

by:chensu
Comment Utility
It depends on which MFC functions are being used. In some cases, you may use try-catch to catch the CMemoryException.
0
 

Author Comment

by:ewajoz
Comment Utility
I know that _set_new_handler does not cover API functions, I used new op.
Chensu's remark on exception throwing is right - it is my case. It is a pity you cannot find that information in MSDN in "new" description. (accoriding to it NULL is returned on allocation failure).
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now