Solved

Handling of memory allocation failures

Posted on 2000-04-01
5
300 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
ID: 2676387
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
ID: 2677211
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
ID: 2677439
"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
ID: 2677893
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
ID: 2679259
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
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.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

786 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