Created a simple class to test deleting Objects and this function will be called from another language but I ran into some weird issues I can't explain.
I have class with functions that can destroy instance of it and set the pointer to null
.
1.Destructor is called when delete
is used in the main function which is expected:
using namespace std;
//Forward declaration:
class Motor;
void DestroyMotor(Motor** motor)
{
if (motor != nullptr)
{
//(*motor)->~Motor();
delete *motor;
*motor = nullptr;
}
}
void DestroyMotor(Motor*& motor)
{
delete motor;
motor = nullptr;
}
class Motor
{
public:
Motor();
~Motor();
private:
};
Motor::Motor()
{
cout << "Motor Created" << endl;
}
Motor::~Motor()
{
cout << "Motor Destroyed" << endl;
}
int main()
{
{
Motor *myMotor = new Motor();
if (myMotor == nullptr)
cout << "IS null" << endl;
else
cout << "NOT null" << endl;
delete myMotor;
myMotor = nullptr;
if (myMotor == nullptr)
cout << "IS null" << endl;
else
cout << "NOT null" << endl;
}
cin.get();
return 0;
}
Output:
- Motor Created
- NOT null
- Motor Destroyed
- IS null
2.Destructor is NOT called when delete
is used in one of the DestroyMotor
functions (Bug?):
int main()
{
{
Motor *myMotor = new Motor();
if (myMotor == nullptr)
cout << "IS null" << endl;
else
cout << "NOT null" << endl;
//Free myMotor;
//DestroyMotor(myMotor);
DestroyMotor(&myMotor);
if (myMotor == nullptr)
cout << "IS null" << endl;
else
cout << "NOT null" << endl;
}
cin.get();
return 0;
}
Output:
- Motor Created
- NOT null
- IS null
3.The problem in #2 is fixed if I move the class to the top of those two DestroyMotor
functions and the destructor is now being called.
using namespace std;
class Motor
{
public:
Motor();
~Motor();
private:
};
Motor::Motor()
{
cout << "Motor Created" << endl;
}
Motor::~Motor()
{
cout << "Motor Destroyed" << endl;
}
void DestroyMotor(Motor** motor)
{
if (motor != nullptr)
{
//(*motor)->~Motor();
delete *motor;
*motor = nullptr;
}
}
void DestroyMotor(Motor*& motor)
{
delete motor;
motor = nullptr;
}
int main()
{
{
Motor *myMotor = new Motor();
if (myMotor == nullptr)
cout << "IS null" << endl;
else
cout << "NOT null" << endl;
//delete myMotor;
//myMotor = nullptr;
//Free myMotor;
//DestroyMotor(myMotor);
DestroyMotor(&myMotor);
if (myMotor == nullptr)
cout << "IS null" << endl;
else
cout << "NOT null" << endl;
}
cin.get();
return 0;
}
Output:
- Motor Created
- NOT null
- Motor Destroyed
- IS null
Can anyone explain what exactly is going on with #2 and why moving the class to the top fixed the issue? I mean, I used forward declaration but that never solved the issue.