473,468 Members | 1,352 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

delete resouce of partial constructed object

Hi, I have a question regarding the partially constructed object. If an
exception occurs while executing object's constructor, how can I delete
the resource that might have been allocated? For example:

class Foo {};

class Bar {
public:
Bar() {
f = new Foo();
throw Exception();
}
private:
Foo *f;
};

int main() {
try {
Bar *b = new Bar();
} catch (exception e) {

// What happened to *f ? How can I delete it?
}
}
Thanks,
Metaosp
Feb 6 '06 #1
2 1471
Metaosp wrote:
Hi, I have a question regarding the partially constructed object. If an
exception occurs while executing object's constructor, how can I delete
the resource that might have been allocated?
se the RAII principle.
For example:

class Foo {};

class Bar {
public:
Bar() {
f = new Foo();
throw Exception();
}
private:
Foo *f;
};

int main() {
try {
Bar *b = new Bar();
} catch (exception e) {

// What happened to *f ? How can I delete it?
You can't.
}
}


One way of solving it is by using std::auto_ptr, like:

Bar() {
std::auto_ptr<Foo> ptr(new Foo);
throw Exception();
f = ptr->release();
}

Do you actually need to allocate the member dynamically?

Feb 6 '06 #2
Rolf Magnus wrote:
Metaosp wrote:
Hi, I have a question regarding the partially constructed object. If an
exception occurs while executing object's constructor, how can I delete
the resource that might have been allocated?


se the RAII principle.
For example:

class Foo {};

class Bar {
public:
Bar() {
f = new Foo();
throw Exception();
}
private:
Foo *f;
};

int main() {
try {
Bar *b = new Bar();
} catch (exception e) {

// What happened to *f ? How can I delete it?


You can't.
}
}


One way of solving it is by using std::auto_ptr, like:

Bar() {
std::auto_ptr<Foo> ptr(new Foo);
throw Exception();
f = ptr->release();
}


Or better, just make Bar::f (don't try to pronounce it) a smart pointer
instead of a raw pointer. Also prefer initialization lists
(http://www.parashift.com/c++-faq-lit...html#faq-10.6), and prefer
to catch your exception by reference rather than value
(http://www.parashift.com/c++-faq-lit...tml#faq-17.7):

class Bar
{
public:
Bar()
: f( new Foo )
{
throw Exception();
}
private:
std::auto_ptr<Foo> f;
};

int main()
{
try
{
Bar *b = new Bar();
}
catch( const Exception& e )
{
// ...
}
return 0;
}

Cheers! --M

Feb 6 '06 #3

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

Similar topics

1
by: Nimmi Srivastav | last post by:
There's a rather nondescript book called "Using Borland C++" by Lee and Mark Atkinson (Que Corporation) which presents an excellent discussion of overloaded new and delete operators. In fact there...
3
by: Nimmi Srivastav | last post by:
There's a rather nondescript book called "Using Borland C++" by Lee and Mark Atkinson (Que Corporation) which presents an excellent discussion of overloaded new and delete operators. I am...
3
by: William Payne | last post by:
Hi, I have a class that allocates some memory dynamically in its (only) constructor. In my destructor I have a call to delete which corresponds to the new in the constructor. So far so good....
6
by: Jay Nabonne | last post by:
Hi, This might sound odd, but we want to replace the allocation scheme used by new and delete without changing operator new and operator delete. (The global operators are shared and we can't...
6
by: Sowen | last post by:
hi, I have the following code object obj_1; object obj_2; object obj_3; object *objs = { &obj_1, &obj_2, &obj_3 };
10
by: ct | last post by:
Hi, Here is my problem: I need to be able to call a virtual method when an object is deleted. class A { public: virtual ~A(); void virtual DestroyRespond();
10
by: Neelesh Bodas | last post by:
Hi all, suppose the ctor of the class throws. Thus, at this point in time, some memory has been allocated but the object couldnot be constructed. Also, the memory is not yet freed. Does the c++...
27
by: fermisoft | last post by:
I have a strange problem in my project. I have a class like this...... class CMsg { public: void *m_body; ~CMsg()
19
by: Daniel Pitts | last post by:
I have std::vector<Base *bases; I'd like to do something like: std::for_each(bases.begin(), bases.end(), operator delete); Is it possible without writing an adapter? Is there a better way? Is...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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,...
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...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.