473,467 Members | 1,929 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

constructor error with an inherited class

Hi, i have the following problem:
I have an unmanaged class that is derived from 2 base classes

class A : public B, C
{
...
}

the constructor is something like

A::A() : B(), C()
{
...
}

i was wondering what would happen if the contructor of B or C throws an
exception.
in that case i will never get the pointer to A, so i cannot delete it, but
what if the other constructor
(for example B()) succeeded? will B automatically be deleted (will its
constructor be invoked)? or will there be a memory leak?

kind regards,
Bruno.
Nov 16 '05 #1
1 908
Bruno van Dooren wrote:
Hi, i have the following problem:
I have an unmanaged class that is derived from 2 base classes

class A : public B, C
{
...
}

the constructor is something like

A::A() : B(), C()
{
...
}

i was wondering what would happen if the contructor of B or C throws an
exception.
in that case i will never get the pointer to A, so i cannot delete it, but
what if the other constructor
(for example B()) succeeded? will B automatically be deleted (will its
constructor be invoked)? or will there be a memory leak?


Fully constructed base class and member subobjects will be destroyed.
However, if your ctor does something like this:

// p1 and p2 are X* and Y*
Z()
: p1(new X),
p2(new Y)
{
}

Then if "new Y" throws an exception, you have a problem, because the Z
object is not fully constructed, so its dtor won't be called, and since p1
is a native pointer, its destruction is a no-op. However, if p1 was an
object of a suitable smart pointer type, then p1 would be destroyed and its
dtor called, deleting the object it points to, and everything would be fine.

To finish answering your question, exceptions thrown during the execution of
a new-expression do not normally leak the memory allocated by the new
operator. Instead, an operator delete function which matches the operator
new used by the new-expression is called, and it this operator delete that
is responsible for "undoing" the memory allocation.

--
Doug Harrison
Microsoft MVP - Visual C++
Nov 16 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Dominique | last post by:
Suppose I have: class A { A(int x, int y); }; and class B: public A {
45
by: Ben Blank | last post by:
I'm writing a family of classes which all inherit most of their methods and code (including constructors) from a single base class. When attempting to instance one of the derived classes using...
7
by: ad | last post by:
When a class (say class1) inherited form another class (Class0). The constructor in the class0 will be executing before the constructor of class1. Can we over the constructor?
19
by: Andrew J. Marshall | last post by:
I want to create a class that must receive a parameter when instantiated. In other words, I do not want it to have a "Public Sub New()". 1) Does VB.NET create a default public constructor if I do...
8
by: Mike - EMAIL IGNORED | last post by:
I have a class that may or may not be inherited. Its constructor calls a function that should be called only if the class is not inherited. Is there a way to tell, other than simply passing a...
6
by: Taran | last post by:
Hi All, I tried something with the C++ I know and some things just seem strange. consider: #include <iostream> using namespace std;
7
by: AndreasW | last post by:
Hi, internal static class Program { public class foo { private int _id; public foo() {
6
by: =?Utf-8?B?T2xkQnV0U3RpbGxMZWFybmluZw==?= | last post by:
I forget the right syntax.... If I Have multiple signatures for a constructor, but I want each to call a "main" version of the constructor...I can't remember how to do this in C#.... public...
2
by: tshad | last post by:
Using VB.Net VS 2003 I am getting the following message when I call my 2nd constructor. Too many arguments to 'Public Sub New()'. I have 2 constructors: **********************************...
4
by: l.s.rockfan | last post by:
Hello, how do i have to call an inherited, templated class constructor from the initializer list of the inheriting, non-templated class constructor? example code: template<typename T>...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.