If I have a class or struct like this:
bool localScopeFunc()
{
bool result;
IDXGIFactory* pFactory;
IDXGIAdapter* pAdapter;
result = //Do something here with the objects
if (!result) return false;
result = //Do something here with the objects
if (!result) return false;
result = //Do something here with the objects
if (!result) return false;
// And so on......
//___Do cleanup here___//
pFactory->Release(); pFactory = nullptr;
pAdapter->Release(); pAdapter = nullptr;
return true; // If all passes
}
If at any point during this function something fails and it returns false, it doesn't do the cleanup at the end, thus not calling ->Release() on any objects. Will this mean a memory leak?
If so, I can't figure out a feasible way to do it, as sometimes I'll have a list of function calls, at each stage initialising something new, and if I had to clean everything up in reverse it would look like this:
int main()
{
if (!initTime()) {return -1;}
if (!initD3D()) {shutDownTime(); return -2;}
if (!initCamera()) {shutDownD3D(); shutDownTime(); return -3;}
if (!initSound()) {shutDownCamera(); shutDownD3D(); shutDownTime(); return -3;}
if (!initPhysics()) {shutDownSound(); shutDownD3D(); shutDownTime(); return -4;}
// And so on.
return 0;
}