473,378 Members | 1,354 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

ctor exception question

I have a class foo whose ctor might throw an exception. When I create a foo
object using "foo* myfoo = new foo;", and the ctor throws an exception, do
I need to delete myfoo to free its memory?

Apr 15 '06 #1
7 1905
Thomas wrote:
I have a class foo whose ctor might throw an exception. When I create a foo
object using "foo* myfoo = new foo;", and the ctor throws an exception, do
I need to delete myfoo to free its memory?


The compiler will generate code that will deallocate the memory using
operator delete() if the constructor throws.

Apr 15 '06 #2

Thomas wrote:
I have a class foo whose ctor might throw an exception. When I create a foo
object using "foo* myfoo = new foo;", and the ctor throws an exception, do
I need to delete myfoo to free its memory?


No, see:
http://www.parashift.com/c++-faq-lit...html#faq-16.10

Abdo Haji-Ali
Programmer
In|Framez

Apr 15 '06 #3
bb
However, in the following code... how to ensure "classA* ca" memory is
not leaked?

#include <iostream>

class classA {
public:
classA() { std::cout << "classA ctor." << std::endl; }
~classA() { std::cout << "classA dtor." << std::endl; }
private:
};

class classB {
public:
classB() {
throw "oops";
std::cout << "classB ctor." << std::endl;
}
~classB() { std::cout << "classB dtor." << std::endl; }
private:
};

class classD {
public:
classD() : ca(new classA), cb(new classB) {
std::cout << "classD ctor." << std::endl;
}
~classD() {
std::cout << "classD dtor." << std::endl;
delete cb;
delete ca;
}
private:
classA* ca;
classB* cb;
};

int main(int argc, char** argv) {

classD cd;

// code continues...
// how to release "classA* ca" in classD object (in cd above)?
}

Apr 15 '06 #4

bb wrote:
However, in the following code... how to ensure "classA* ca" memory is
not leaked?


Use std::auto_ptr<> or one of the boost smart pointers rather than
plain pointers. When any part of construction throws, the destructors
of so far constracted members and base classes are called.

Apr 15 '06 #5
bb
Thanks Maxim. Thats a v.good idea and works.

Apr 16 '06 #6

bb wrote:
Thanks Maxim. Thats a v.good idea and works.


With a caveat: when using std::auto_ptr<> as a member don't forget to
disable copying by declaring the copy constructor and the assignment
operator non public or implement deep copy. Just declaring
std::auto_ptr<> member const prohibts copying as well.

Apr 16 '06 #7
bb
Thanks again Maxim. I do appreciate those important points.

Apr 16 '06 #8

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

Similar topics

4
by: SteveK | last post by:
**Newbie with managed C++** I'm trying to wrap an unmanaged C++ class in a managed C++ class library. When I instantiate the wrapper class it's ctor calls the ctor on the unmanaged class and at...
6
by: Klaus Ahrens | last post by:
hi all, acoording to the c++ standard "15.3. - -10- Referring to any non-static member or base class of an object in the handler for a function-try-block of a constructor or destructor for...
5
by: PasalicZaharije | last post by:
Hallo, few days ago I see ctor like this: Ctor() try : v1(0) { // some code } catch(...) { // some code }
5
by: Grahamo | last post by:
Hi, I have a basic question regarding some legacy code I'm working with; Basically the code looks something like this. I'd like to know if there are any reasons why a particular approach is...
6
by: puzzlecracker | last post by:
When ctor throws an exception, dtor is not called for that object (correct me if I am wrong) - then how would already allocated memebers de-allocated? Thanks.
4
by: ksukhonosenko | last post by:
This message was originally posted to comp.lang.c++.moderated ---------------------------------------------------------------------------------------------- Hi! I face a problem in my...
8
by: Grizlyk | last post by:
Good morning. Look here: http://groups.google.com/group/fido7.ru.cpp.chainik/browse_frm/thread/7341aba5238c0f79 and here:...
1
by: njuneardave | last post by:
Hey, I'm having an emergency problem with my software and am under a very very tight deadline, so I thought I could post here as a last resort... I randomly get an exception pop up that...
0
by: subramanian100in | last post by:
Consider the following program: #include <cstdlib> #include <iostream> using namespace std; class Test { public:
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.