The recipe is:
"you shall always initialise your pointer" (with
nullptr
if you need to assign it later) then you may delete it or not, as appropriate.
Another approach would be using a smart pointer, this way no delete is needed.
Try, for instance:
#include <iostream>
#include <memory>
using namespace std;
struct Dialog
{
Dialog(){ cout << "Dialog ctor\n"; }
~Dialog(){ cout << "Dialog dtor\n";}
};
int main()
{
{
cout << "test case 1\n";
Dialog * pdlg = new Dialog();
if ( pdlg ) delete pdlg;
}
{
cout << "test case 2\n";
Dialog * pdlg{ nullptr };
if ( pdlg ) delete pdlg;
}
{
cout << "test case 3\n";
auto spdlg = make_unique<Dialog>();
}
{
cout << "test case 4\n";
unique_ptr<Dialog> spdlg{};
}
}