473,322 Members | 1,501 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,322 software developers and data experts.

Do I need a delete in the constructor too?

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. Anyway, after I've allocated the
memory that I need in my constructor I proceed to call a function in a
third-party library. If that function fails, I need to throw an exception
because the object relies on it to succeed. Now my question is: If I throw
an exception in my constructor, do I have to deallocate my memory there too
or is the destructor enough? I'm guessing the destructor won't be called if
I throw in my constructor (because then the object isn't created and cannot,
naturally, be destroyed), thus I have to take care to deallocate any
dynamically allocated memory in my constructor if it's going to throw an
exception. Correct?

/ WP
Jul 22 '05 #1
3 1309
"William Payne" <mi**************@student.liu.se> wrote in
news:ch**********@news.island.liu.se:
succeed. Now my question is: If I throw an exception in my
constructor, do I have to deallocate my memory there too or is the
destructor enough? I'm guessing the destructor won't be called if I
throw in my constructor (because then the object isn't created and
cannot, naturally, be destroyed), thus I have to take care to
deallocate any dynamically allocated memory in my constructor if it's
going to throw an exception. Correct?


The destructor only runs when a fully constructed object is destroyed. If
you throw an exception within the constructor, the destructor will not be
called. You are correct that you will have to clean up before throwing the
exception. You may be able to consolidate the cleanup in a private function
that you can call from both the destructor and the constructor. You can
simplify the logic if you remember that deleting a null pointer is valid
and does not do anything.

Gregg
Jul 22 '05 #2
"William Payne" <mi**************@student.liu.se> wrote:
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. Anyway, after I've allocated the
memory that I need in my constructor I proceed to call a function in a
third-party library. If that function fails, I need to throw an exception
because the object relies on it to succeed. Now my question is: If I throw
an exception in my constructor, do I have to deallocate my memory there too
or is the destructor enough? I'm guessing the destructor won't be called if
I throw in my constructor (because then the object isn't created and cannot,
naturally, be destroyed), thus I have to take care to deallocate any
dynamically allocated memory in my constructor if it's going to throw an
exception. Correct?


You need to delete the memory yourself in this case. I suggest you use a
vector (if the memory is an array) or wrap the memory in an auto_ptr so
that it will be deleted automatically if there is a throw in the c_tor.
Jul 22 '05 #3

"William Payne" <mi**************@student.liu.se> wrote in message
news:ch**********@news.island.liu.se...
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. Anyway, after I've allocated
the memory that I need in my constructor I proceed to call a function in a
third-party library. If that function fails, I need to throw an exception
because the object relies on it to succeed. Now my question is: If I throw
an exception in my constructor, do I have to deallocate my memory there
too or is the destructor enough? I'm guessing the destructor won't be
called if I throw in my constructor (because then the object isn't created
and cannot, naturally, be destroyed), thus I have to take care to
deallocate any dynamically allocated memory in my constructor if it's
going to throw an exception. Correct?

/ WP


Correct, the rule is that only fully constructed objects have their
destructors called.

This is another reason not to use pointers. Consider

class P
{
public:
P()
{
ptr = new int[100];
try
{
third_party(ptr); // might throw
}
catch (...)
{
delete[] ptr;
throw;
}
}
~P()
{
delete[] ptr;
}
// plus copy ctor and assignment op
private:
int* ptr;
};

class V
{
public:
V() : vec(100)
{
third_party(&vec[0]); // might throw
}
private:
vector<int> vec;
}

See how much simpler the constructor is in class V. In this class if
third_party throws an exception then V::vec will be destroyed because it is
a fully constructed object (even though V itself is not).

john
Jul 22 '05 #4

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

Similar topics

2
by: Radde | last post by:
Hi all, #include<iostream> using namespace std; class A{ private: int* iArray; int size; public:
10
by: n2xssvv g02gfr12930 | last post by:
In a job interview I was asked about the statement below: delete this; Naturally I was horrified, yet they claimed they had used it. Personally I'm pretty damn sure I could never justify this....
16
by: Martin Vorbrodt | last post by:
some butthole asked me an interview question: can you delete "this" pointer in a member function of a class, like this: class C { public: void foo() { delete this; } }; my answer was......
1
by: satan | last post by:
I need a help with the program to test the insertion, deletion, search, copyList, and the copy constructor operations for an object in the class UnorderedLinkedList. These are my classes: ...
7
by: satan | last post by:
I need a help with the program to test the insertion, deletion, search, copyList, and the copy constructor operations for an object in the class UnorderedLinkedList. These are my classes: ...
3
by: yancheng.cheok | last post by:
Hello all, I try to figure out what is the sequence when we create and delete an object. After experiment on my VC++ 2003, I found that here is the sequence new-constructor-destructor-delete ...
5
by: Richard Gromstein | last post by:
Hello, I have an exercise that I need to finish very soon and I really need help understanding what to do and how exactly to do it. I am working on reading the chapter right now and working on it...
7
helpwithcode
by: helpwithcode | last post by:
Hi people, I am just learning java.I have been creating a project which involves JDBC Connectivity.I find that the statements, String string_dob=text_dob.getText(); //Converting string to...
12
by: Premal | last post by:
Hi, I tried to make delete operator private for my class. Strangely it is giving me error if I compile that code in VC++.NET. But it compiles successfully on VC++6.o. Can anybody give me inputs...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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...

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.