W
William Payne
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
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