Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 51827
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T16:48:41+00:00 2026-05-10T16:48:41+00:00

Looking for an answer for C# and C++. (in C#, replace ‘destructor’ with ‘finalizer’)

  • 0

Looking for an answer for C# and C++. (in C#, replace ‘destructor’ with ‘finalizer’)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T16:48:41+00:00Added an answer on May 10, 2026 at 4:48 pm

    Preamble: Herb Sutter has a great article on the subject:

    http://herbsutter.wordpress.com/2008/07/25/constructor-exceptions-in-c-c-and-java/

    C++ : Yes and No

    While an object destructor won’t be called if its constructor throws (the object "never existed"), the destructors of its internal objects could be called.

    As a summary, every internal parts of the object (i.e. member objects) will have their destructors called in the reverse order of their construction. Every thing built inside the constructor won’t have its destructor called unless RAII is used in some way.

    For example:

    struct Class {    Class() ;    ~Class() ;        Thing *    m_pThing ;    Object     m_aObject ;    Gizmo *    m_pGizmo ;    Data       m_aData ; }  Class::Class() {    this->m_pThing = new Thing() ;    this->m_pGizmo = new Gizmo() ; } 

    The order of creation will be:

    1. m_aObject will have its constructor called.
    2. m_aData will have its constructor called.
    3. Class constructor is called
    4. Inside Class constructor, m_pThing will have its new and then constructor called.
    5. Inside Class constructor, m_pGizmo will have its new and then constructor called.

    Let’s say we are using the following code:

    Class pClass = new Class() ; 

    Some possible cases:

    • Should m_aData throw at construction, m_aObject will have its destructor called. Then, the memory allocated by "new Class" is deallocated.

    • Should m_pThing throw at new Thing (out of memory), m_aData, and then m_aObject will have their destructors called. Then, the memory allocated by new Class is deallocated.

    • Should m_pThing throw at construction, the memory allocated by "new Thing" will be deallocated. Then m_aData, and then m_aObject will have their destructors called. Then, the memory allocated by new Class is deallocated.

    • Should m_pGizmo throw at construction, the memory allocated by "new Gizmo" will be deallocated. Then m_aData, and then m_aObject will have their destructors called. Then, the memory allocated by new Class is deallocated. Note that m_pThing leaked

    If you want to offer the Basic Exception Guarantee, you must not leak, even in the constructor. Thus, you’ll have to write this this way (using STL, or even Boost):

    struct Class {    Class() ;    ~Class() ;        std::auto_ptr<Thing>   m_pThing ;    Object                 m_aObject ;    std::auto_ptr<Gizmo>   m_pGizmo ;    Data                   m_aData ; }  Class::Class()    : m_pThing(new Thing())    , m_pGizmo(new Gizmo()) { } 

    Or even:

    Class::Class() {    this->m_pThing.reset(new Thing()) ;    this->m_pGizmo.reset(new Gizmo()) ; } 

    if you want/need to create those objects inside the constructor.

    This way, no matter where the constructor throws, nothing will be leaked.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 119k
  • Answers 119k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer This should do what you want: x = raw_input('How many… May 11, 2026 at 11:44 pm
  • Editorial Team
    Editorial Team added an answer A lot of the examples you can use by replacing… May 11, 2026 at 11:44 pm
  • Editorial Team
    Editorial Team added an answer Even with your PrintRange sub declared as Public you still… May 11, 2026 at 11:44 pm

Related Questions

Looking for an answer for C# and C++. (in C#, replace 'destructor' with 'finalizer')
I'm looking for an Objective-C way of sorting characters in a string, as per
How do you find the phone numbers in 50,000 HTML pages? Jeff Attwood posted
How can I use WIA and Twain in C#? The TWIAIN/C# example found at
Important: This question is getting quite long, if this is the first time you're

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.