J
John paul coder
Guest
Hello All,
From the below function, when executing waitParam.SetValue(true); it is throwing an exception from ~TypeValue() destructor.
To catch the exception, I have used try/catch but it is not handling the exception. Which part of the code do I need to place in the catch block to handle the exception?
I have fixed the issue by changing
if (mStringVal)
with
if (mKind == pkString && mStringVal != nullptr)
in ~TypeValue() destructor.
But wanted to know which part of the code do I need to place in the catch block to handle the exception?
~TypeValue()
{
if (mKind == pkString && mStringVal != nullptr)
{
delete mStringVal;
mStringVal = nullptr;
}
};
Below is the complete code:
int PCPEAutomation::Execute()
{
std::wstring strFilePath = L"C:\PCPEAutomationDev\src\AutomationFiles";
DWORD dwResult = ERROR_SUCCESS;
try
{
PCPECLIAutomation pcpeCLIAutomation;
// Set working dir param
Parameter workingDirParam(PARAMETER_WORKING_DIR);
workingDirParam.SetValue(strFilePath);
pcpeCLIAutomation.SetParameter(workingDirParam);
// Set wait flag param
Parameter waitParam(PARAMETER_WAIT_FOR_COMPLETE);
waitParam.SetValue(true);
pcpeCLIAutomation.SetParameter(waitParam);
//Execute the CLI operation
m_nStatus = pcpeCLIAutomation.Execute();
}
catch (...)
{
dwResult = ERROR_UNHANDLED_EXCEPTION;
}
return m_nStatus;
}
Parameter.h
#include "TypeValue.h"
class Parameter
{
private:
std::wstring mID;
TypeValue mValue;
public:
Parameter()
{
}
Parameter(std::wstring id) :
mID(id)
{
}
~Parameter()
{
}
bool SetValue(const PropertyValue& value)
{
mValue = value;
return true;
}
bool GetValue(PropertyValue& value) const
{
value = mValue;
return true;
}
std::wstring GetID() const
{
return mID;
}
};
TypeValue.h
enum TypeValueKind
{
pkEmpty = 0,
pkBool = 1,
pkInt = 2,
pkString = 3
};
class TypeValue
{
private:
union
{
bool mBoolVal;
int mIntVal;
std::wstring *mStringVal;
};
TypeValueKind mKind;
public:
typedef std::vector<TypeValue*> ArrayVal;
TypeValue() : mKind(pkEmpty)
{
mBoolVal = false;
mIntVal = 0;
mStringVal = nullptr;
};
~TypeValue()
{
if (mStringVal)
{
delete mStringVal;
mStringVal = nullptr;
}
};
TypeValue(bool val) : mKind(pkBool)
{
mBoolVal = val;
}
TypeValue(int val) : mKind(pkInt)
{
mIntVal = val;
}
TypeValue(std::wstring &val) : mKind(pkString)
{
mStringVal = new std::wstring();
*mStringVal = val;
}
};
Could anyone please help me on this?
Continue reading...
From the below function, when executing waitParam.SetValue(true); it is throwing an exception from ~TypeValue() destructor.
To catch the exception, I have used try/catch but it is not handling the exception. Which part of the code do I need to place in the catch block to handle the exception?
I have fixed the issue by changing
if (mStringVal)
with
if (mKind == pkString && mStringVal != nullptr)
in ~TypeValue() destructor.
But wanted to know which part of the code do I need to place in the catch block to handle the exception?
~TypeValue()
{
if (mKind == pkString && mStringVal != nullptr)
{
delete mStringVal;
mStringVal = nullptr;
}
};
Below is the complete code:
int PCPEAutomation::Execute()
{
std::wstring strFilePath = L"C:\PCPEAutomationDev\src\AutomationFiles";
DWORD dwResult = ERROR_SUCCESS;
try
{
PCPECLIAutomation pcpeCLIAutomation;
// Set working dir param
Parameter workingDirParam(PARAMETER_WORKING_DIR);
workingDirParam.SetValue(strFilePath);
pcpeCLIAutomation.SetParameter(workingDirParam);
// Set wait flag param
Parameter waitParam(PARAMETER_WAIT_FOR_COMPLETE);
waitParam.SetValue(true);
pcpeCLIAutomation.SetParameter(waitParam);
//Execute the CLI operation
m_nStatus = pcpeCLIAutomation.Execute();
}
catch (...)
{
dwResult = ERROR_UNHANDLED_EXCEPTION;
}
return m_nStatus;
}
Parameter.h
#include "TypeValue.h"
class Parameter
{
private:
std::wstring mID;
TypeValue mValue;
public:
Parameter()
{
}
Parameter(std::wstring id) :
mID(id)
{
}
~Parameter()
{
}
bool SetValue(const PropertyValue& value)
{
mValue = value;
return true;
}
bool GetValue(PropertyValue& value) const
{
value = mValue;
return true;
}
std::wstring GetID() const
{
return mID;
}
};
TypeValue.h
enum TypeValueKind
{
pkEmpty = 0,
pkBool = 1,
pkInt = 2,
pkString = 3
};
class TypeValue
{
private:
union
{
bool mBoolVal;
int mIntVal;
std::wstring *mStringVal;
};
TypeValueKind mKind;
public:
typedef std::vector<TypeValue*> ArrayVal;
TypeValue() : mKind(pkEmpty)
{
mBoolVal = false;
mIntVal = 0;
mStringVal = nullptr;
};
~TypeValue()
{
if (mStringVal)
{
delete mStringVal;
mStringVal = nullptr;
}
};
TypeValue(bool val) : mKind(pkBool)
{
mBoolVal = val;
}
TypeValue(int val) : mKind(pkInt)
{
mIntVal = val;
}
TypeValue(std::wstring &val) : mKind(pkString)
{
mStringVal = new std::wstring();
*mStringVal = val;
}
};
Could anyone please help me on this?
Continue reading...