2

This is a potential memory leak, correct? arr won't be deallocated if the constructor throws, right? I have read that the destructor won't be called in this case, but any sub objects of badType would be, however I think the array won't here because it's a raw pointer, correct?

class badType
{
private:
    int* arr;
    int myLen, myNum;
public:
    badType(int myLen, int aNum)
    {
        this->myLen = myLen;
        arr = new int[myLen];

        if (aNum < 100) 
            throw exception{ "Da number is too low" };

        myNum = aNum;
    }

    ~badType()
    {
        delete[] arr;
    }
};
Steve M
  • 9,296
  • 11
  • 49
  • 98
  • Good question, I think I found a dup here that answers your question: http://stackoverflow.com/questions/147572/will-the-below-code-cause-memory-leak-in-c – Doug T. Feb 11 '14 at 01:22
  • In this particular case you can test for `aNum < 100` before allocating `arr`. – s.bandara Feb 11 '14 at 01:24
  • why don't you pop a `cout<<"destructor called\n";` into the destructor? – BWG Feb 11 '14 at 01:26
  • Another possible answer [here](http://stackoverflow.com/questions/9043365/destructor-not-called-when-an-exception-is-thrown). – Josh Braun Feb 11 '14 at 01:27
  • @BWG - Maybe it deletes it without calling the destructor, I don't know – Steve M Feb 11 '14 at 01:28
  • 1
    @svenoaks And, you allocate amount `myLen`, which is not `mylen`. Don't name stuff with such a minor difference! – BWG Feb 11 '14 at 01:29
  • "I think the array won't here because it's a raw pointer, correct?". Correct conclusion, error in the reasoning. The array is not a member of the class. That is why it is not destroyed. Also the array is *not* a raw pointer. The array is pointed to by the pointer, but that has no effect on its lifetime. – Steve Jessop Feb 11 '14 at 01:33
  • Good points Steve, I need to slow down with typing and think more :) – Steve M Feb 11 '14 at 01:35

2 Answers2

4

No, it won't deallocate. It will be a memory leak if the constructor throws at aNum < 100.

An object of any storage duration whose initialization or destruction is terminated by an exception will have destructors executed for all of its fully constructed subobjects (excluding the variant members of a union-like class), that is, for subobjects for which the principal constructor (12.6.2) has completed execution and the destructor has not yet begun execution.

The standard generally won't state explicitly that something won't be called. So by only stating destructors of fully constructed subobjects will be called, the standard is equivalently saying the object's own destructor won't be called.

However, the object itself will be deallocated if it's allocated on the free store.

If the object was allocated in a new-expression, the matching deallocation function (3.7.4.2, 5.3.4, 12.5), if any, is called to free the storage occupied by the object.

neverhoodboy
  • 1,040
  • 7
  • 13
  • Do you have standard quote on that? – Steve M Feb 11 '14 at 01:29
  • @svenoaks Dynamic storage doesn't deallocate itself. So if the destructor isn't called, that means it DEFINITELY won't be deallocated. – BWG Feb 11 '14 at 01:31
  • @svenoaks: I edited the answer to add the quote you want. – neverhoodboy Feb 11 '14 at 01:32
  • I don't think stronging 'fully' is right, since it's not an object? But close enough, thanks. – Steve M Feb 11 '14 at 01:34
  • In practice the destructor will not be called. However, I don't think the standard explicitly requires this behavior. Is that correct? – andypea Feb 11 '14 at 01:39
  • @andrew.punnett The standard generally won't state explicitly that something won't be called. So by only stating destructors of fully constructed subobjects will be called, the standard is equivalently saying the object's own destructor won't be called. – neverhoodboy Feb 11 '14 at 01:41
  • @neverhoodboy And assuming it is called (or assuming it isn't) would result in undefined behavior. – andypea Feb 11 '14 at 01:46
1

You are right, the array won't be deallocated.

An object is destroyed only if fully constructed i.e if no exception propagates from its constructor.

Read: RAII

Prince
  • 20,353
  • 6
  • 39
  • 59