Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Question about NEW throwing in MFC app

Question about NEW throwing in MFC app

Scheduled Pinned Locked Moved C / C++ / MFC
c++question
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    FrankRizzo
    wrote on last edited by
    #1

    We are all aware that by default VC++ implementation of NEW does not throw an exception (as it should based on C++ standards). MSDN has an article related to using set_new_handler() to make new throw std::bad_alloc when it fails instead of returning NULL. My question is how is that handled in an MFC app? For instance lets say you have a method named Foo(), and Foo() might throw std::bad_alloc due to it calling new internally. If you called Foo() from an MFC class method you override, and did not have a try/catch, would MFC catch the bad_alloc? In fact, what impact does it have at all on MFC if you change NEW to throw? I'm also curious how other developers handle NEW... Do you leave it returning NULL by default? If so, won't that cause problems if you use STL and an STL method calls new and it throws?

    M 1 Reply Last reply
    0
    • F FrankRizzo

      We are all aware that by default VC++ implementation of NEW does not throw an exception (as it should based on C++ standards). MSDN has an article related to using set_new_handler() to make new throw std::bad_alloc when it fails instead of returning NULL. My question is how is that handled in an MFC app? For instance lets say you have a method named Foo(), and Foo() might throw std::bad_alloc due to it calling new internally. If you called Foo() from an MFC class method you override, and did not have a try/catch, would MFC catch the bad_alloc? In fact, what impact does it have at all on MFC if you change NEW to throw? I'm also curious how other developers handle NEW... Do you leave it returning NULL by default? If so, won't that cause problems if you use STL and an STL method calls new and it throws?

      M Offline
      M Offline
      Michael Dunn
      wrote on last edited by
      #2

      MFC has its own override of operator new that throws a CMemoryException. --Mike-- I'm bored... Episode I bored. 1ClickPicGrabber - Grab & organize pictures from your favorite web pages, with 1 click! My really out-of-date homepage Sonork-100.19012 Acid_Helm

      F 1 Reply Last reply
      0
      • M Michael Dunn

        MFC has its own override of operator new that throws a CMemoryException. --Mike-- I'm bored... Episode I bored. 1ClickPicGrabber - Grab & organize pictures from your favorite web pages, with 1 click! My really out-of-date homepage Sonork-100.19012 Acid_Helm

        F Offline
        F Offline
        FrankRizzo
        wrote on last edited by
        #3

        Yep, and that is what happens when MFC calls new and it fails. But what happens if you call STL or your own funtions from within an MFC function override and your new handler is called throwing bad_alloc? It appears that MFC wont catch it, you will get an unhandled exception. If you try to recover from it, you might leak MFC resources. PS: Did this change in VC .NET? Or does new still return NULL?

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups