C++ how to call D’tor?

  allocation, c++, class, destructor, new-operator

I wrote:

class image {
public:
    linked_list<int, int> UnLabeledList;
    int *SegArray = nullptr;
    int Total_Segments = 0;

    explicit image(int Segments) : SegArray(new int[Segments]) {
        Total_Segments = Segments;
        for (int i = 0; i < Segments; ++i) {
            if (!UnLabeledList.push_back(i, NOT_INIT)) { // allocation failed for one node (Doesn't throw any exception)
                ~UnLabeledList();
                delete[] SegArray;
                throw;
            }
            SegArray[i] = NOT_INIT;
        }
    };
};

In case one allocation failed I want to destroy the object (since it has previously allocated nodes) for example when allocation fails at i==5. How can I call the d’tor of UnLabeledListto prevent memory leak?

Source: Windows Questions C++

LEAVE A COMMENT