I have a class DLXMatrix
with constructor delegation. it as a simple constructor:
DLXMatrix::DLXMatrix(int nb_col);
And another more advanced one which is currently:
DLXMatrix::DLXMatrix(int nb_col, const std::vector<std::vector<int>> &rows)
: DLXMatrix(nb_col) {
for (const auto &r : rows) // TODO : cleanup if an exception is raised
add_row_sparse(r);
}
However, depending on the input, the call to add_row_sparse(r)
may raise a std::out_of_range
exception.
I understand that if I simply let the exception pass, I'll be leaking memory in the various attribute (typically one of the attribute is a vector
)...
Note that, at this stage, this
already points to a valid objects since, by delegation, it was constructed by another constructor. Therefore, I'm tempted to call the destructor as in
DLXMatrix::DLXMatrix(int nb_col, const std::vector<std::vector<int>> &rows)
: DLXMatrix(nb_col) {
try {
for (const auto &r : rows)
add_row_sparse(r);
} catch (std::out_of_range e) {
this->~DLXMatrix();
throw std::out_of_range(e);
}
}
Unfortunately this segfaults. Am I totally out of my mind ? Should I call the destructors only on the attributes.