is it possible to call class in destructor ? if not then why ?
Solved
C++ Gurus
-
My code is shown below :
WaybillCloseFlow::~WaybillCloseFlow() { // TODO Auto-generated destructor stub WaybillCollectionReportFlow *pWaybillCollectionReportFlow = new WaybillCollectionReportFlow; pWaybillCollectionReportFlow->execute(); }
-
@Qt-embedded-developer
And what is your issue? You create a new instance of an unrelated class and call a method on it. (And then fail to delete it, so it should leak.) May or may not be a good thing to do, but certainly allowable. -
@JonB now i am adding below part of code
WaybillCloseFlow::~WaybillCloseFlow() { // TODO Auto-generated destructor stub WaybillCollectionReportFlow *pWaybillCollectionReportFlow = new WaybillCollectionReportFlow; pWaybillCollectionReportFlow->execute(); if(pWaybillCollectionReportFlow) { delete pWaybillCollectionReportFlow; pWaybillCollectionReportFlow = NULL; } }
Then is it possible without memory leak or not ?
-
@Qt-embedded-developer
Yes this will of course prevent the leak. However there's no need to do this if you allocate on stack instead of heap:WaybillCollectionReportFlow waybillCollectionReportFlow; waybillCollectionReportFlow.execute();
That's it, no
new
ordelete
or leak.