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. free memory when constructor throw exception

free memory when constructor throw exception

Scheduled Pinned Locked Moved C / C++ / MFC
performance
4 Posts 4 Posters 1 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.
  • J Offline
    J Offline
    john5632
    wrote on last edited by
    #1

    If constructor is throwing any execpetion after allocating some memory (using new) on heap then how can we free that allocated memory.

    R _ A 3 Replies Last reply
    0
    • J john5632

      If constructor is throwing any execpetion after allocating some memory (using new) on heap then how can we free that allocated memory.

      R Offline
      R Offline
      rxantos
      wrote on last edited by
      #2

      Ideally you avoid any activity that would throw an exception in the constructor. Instead of using new on the constructor. Mark the pointers as nullptr and then have a separate init function. But you might try

      new (std::nothrow)

      which will eat the exception and instead return nullptr. or you can do something like.

      try {
      ptr = new char[100];
      } catch(std::bad_alloc& e) {
      // Decide what to do.
      }

      But overall, is a bad idea to throw on a constructor.

      1 Reply Last reply
      0
      • J john5632

        If constructor is throwing any execpetion after allocating some memory (using new) on heap then how can we free that allocated memory.

        _ Offline
        _ Offline
        _Superman_
        wrote on last edited by
        #3

        Move the memory allocation to a class of its own so that the new happens in its constructor and delete happens in its destructor. That should be the only responsibility of the allocating class. In the class whos constructor throws, create an object of the allocating class on the stack. Since destructors are only called for objects that are fully constructed, the allocating class destructor is always guaranteed to be called.

        «_Superman_»  _I love work. It gives me something to do between weekends.

        _Microsoft MVP (Visual C++) (October 2009 - September 2013)

        Polymorphism in C

        1 Reply Last reply
        0
        • J john5632

          If constructor is throwing any execpetion after allocating some memory (using new) on heap then how can we free that allocated memory.

          A Offline
          A Offline
          Albert Holguin
          wrote on last edited by
          #4

          Decently well though out description of the problem and solutions: C++ Constructors and Memory Leaks - J@ArangoDB[^]

          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