ESP32 Code Optimization workaround
Posted: Wed Nov 03, 2021 11:47 am
Hi All, I am currently facing one issue. Basically I am trying to call destructor followed by delete on the same pointer.
In this case, I am getting some issue. Please find below code snippet
As you can see in above case, inside destructor when I am freeing up memory followed by assigning same pointer to null.
Basically last line is not executing.
delete[] m_ptr; // This is executing.
m_ptr = nullptr; // This is not executing. Maybe due to optimization.
// And after this call is returning from destructor.
So, can anyone please suggest how can I make it execute?
In this case, I am getting some issue. Please find below code snippet
Code: Select all
class Test
{
public:
uint8_t* m_ptr;
Test();
~Test();
void show_element();
};
Test::Test()
{
m_ptr = new uint8_t[10];
m_ptr[0] = 10;
printf("Inside Test() \n\r");
}
Test::~Test()
{
if(nullptr != m_ptr)
{
delete[] m_ptr;
m_ptr = nullptr;
}
}
void Test::show_element()
{
printf("m_ptr[0] = %d \n\r", m_ptr[0]);
}
void main()
{
Test* tptr = new Test;
tptr->show_element();
tptr->~Test();
delete tptr;
}
Basically last line is not executing.
delete[] m_ptr; // This is executing.
m_ptr = nullptr; // This is not executing. Maybe due to optimization.
// And after this call is returning from destructor.
So, can anyone please suggest how can I make it execute?