Q:

[MS VS 2013]C++ fail on delete operator "Debug Assertion Failed!"

I am getting this error when I try to complie my (trivial, attached below) code:

Debug Assertion Failed! 
Program: ......\Project1.exe 
File: f:\dd\vctools\crt\crtw32\misc\dbgdel.cpp 
Line: 52
Expression: _BLOCK_TYPE_IS_VALID(pHead->nBlockUse)

So, here is my code:
class T1
{
private:
    int* foo;
public:
    T1()
    {
        foo = new int[4];
        foo[0] = 1;
        foo[1] = 2;
        foo[2] = 3;
        foo[3] = 4;
    }
    ~T1()
    {
        delete[] foo;
    }
};

int main()
{
    T1 t1;
    t1.~T1();
}

While its kinda ugly and incomplete, its unquestionably correct. I wrote this one after experiencing exact same error with code performing much more sophisticated memory management, just to see what is going on. Still no clue whats wrong, though.
No compiler errors/warnings, no linker errors/warnings. Error occurs when execution reaches delete[] foo; line.
As if that was not insane enough, program executes completely fine when I switch Solution Configuration from Debug to Release - execution reaches the end of main function with no errors. Any clues how to fix this?

A:

You are doing a double delete. First by calling ~T1(), then when main() goes out of scope. As First Jens noted, there is almost never a good reason to call a destructor directly.
The error occurs in the debug mode because the object is checked to see if it has already been deleted when in debug. If you put a breakpoint on the line giving you an error you will see that the first time delete is called is ok and the error occurs on the second delete which is automatically called when the object goes out of scope.

