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 47057
In Process

The Archive Base Latest Questions

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

class someclass {}; class base { int a; int *pint; someclass objsomeclass; someclass* psomeclass;

  • 0
class someclass {};  class base {     int a;     int *pint;     someclass objsomeclass;     someclass* psomeclass; public:     base()     {         objsomeclass = someclass();         psomeclass = new someclass();         pint = new int();          throw 'constructor failed';         a = 43;     } }  int main() {     base temp(); } 

In the above code, the constructor throws. Which objects will be leaked, and how can the memory leaks be avoided?

int main() {     base *temp = new base(); } 

How about in the above code? How can the memory leaks be avoided after the constructor throws?

  • 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:03:47+00:00Added an answer on May 10, 2026 at 4:03 pm

    Yes it will leak memory. When the constructor throws, no destructor will be called (in this case you don’t show a destructor that frees the dynamically allocated objects, but lets assume you had one).

    This is a major reason to use smart pointers – since the smart poitners are full fledged objects, they will get destructors called during the exception’s stack unwind and have the opportunity to free the memory.

    If you use something like Boost’s scoped_ptr<> template, your class could look more like:

    class base{     int a;     scoped_ptr<int> pint;     someclass objsomeclass;     scoped_ptr<someclass> psomeclass;     base() :         pint( new int),        objsomeclass( someclass()),        psomeclass( new someclass())      {         throw 'constructor failed';         a = 43;     } } 

    And you would have no memory leaks (and the default dtor would also clean up the dynamic memory allocations).


    To sum up (and hopefully this also answers the question about the

    base* temp = new base(); 

    statement):

    When an exception is thrown inside a constructor there are several things that you should take note of in terms of properly handling resource allocations that may have occured in the aborted construction of the object:

    1. the destructor for the object being constructed will not be called.
    2. destructors for member objects contained in that object’s class will be called
    3. the memory for the object that was being constructed will be freed.

    This means that if your object owns resources, you have 2 methods available to clean up those resources that might have already been acquired when the constructor throws:

    1. catch the exception, release the resources, then rethrow. This can be difficult to get correct and can become a maintenance problem.
    2. use objects to manage the resource lifetimes (RAII) and use those objects as the members. When the constructor for your object throws an exception, the member objects will have desctructors called and will have an opportunity to free the resource whose lifetimes they are responsible for.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 64k
  • Answers 64k
  • 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
  • added an answer Looking at the stack trace indicates that you're using a… May 11, 2026 at 10:40 am
  • added an answer Getting a pointer again instead of a value: One usually… May 11, 2026 at 10:40 am
  • added an answer Better way, write a custom column. The code follows for… May 11, 2026 at 10:40 am

Related Questions

class someclass {}; class base { int a; int *pint; someclass objsomeclass; someclass* psomeclass;
I have a class that looks like this public class SomeClass { public SomeChildClass[]
Please consider this example class: [Serializable] public class SomeClass { private DateTime _SomeDateTime; public
I have a class SomeClass, which can populate itself from a datarow in it's
Let's say I have some Java code: public class SomeClass { static { private
I have seen some C++ classes with a destructor defined as follows: class someClass
I declared a private variable vector<SomeClass> theVector; someplace inside my SomeClass class. Why can't
Say I've got a class like this: class Test { int x; SomeClass s;
Suppose I have the following code: class some_class{}; some_class some_function() { return some_class(); }
Consider the following code: abstract class SomeClassX<T> { // blah } class SomeClassY: SomeClassX<int>

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.