This is a follow up on my previous question, which can be found here.
I have a container class DSTrajectoryPoint. The class has a member variable m_up_StateValue of type unique_ptr. I also have another container class which has a member variable of type std::vector<unique_ptr<DSTrajectoryPoint>>.
When I initialise DSTrajectoryPoint with the default constructor, I set the m_up_StateValue to nullptr. The default constructor of DSTrajectory creates a single instance of DSTrajectoryPoint. This is done to perform a type check and the instance of DSTrajectoryPoint should cease to exist after the construction of DSTrajectory is complete.
Unfortunately, there seems to be a problem with memory deallocation. Sometimes, I get the following error: dynamic_links(3941,0x7fff749a2310) malloc: *** error for object 0x61636f6c65720054: pointer being freed was not allocated *** set a breakpoint in malloc_error_break to debug The error seems to be occurring upon the destruction of the instance of DSTrajectoryPoint that was initialised within the constructor of DSTrajectory. Can anyone explain why this could be happening?
A code fragment is shown below:
template<typename TTimeType, typename TStateSpaceType>
class DSTrajectoryPoint : public dataflow::Data
{
protected:
TTimeType m_PointTime;
std::unique_ptr<TStateSpaceType> m_up_StateValue;
std::string m_Name;
public:
DSTrajectoryPoint()
{
m_PointTime = 0;
m_up_StateValue = nullptr;
}
};
template<
typename TTimeType,
typename TStateSpaceType,
class TDSTrajectoryPointType
>
class DSTrajectory: public dataflow::Data
{
protected:
std::vector<std::unique_ptr<TDSTrajectoryPointType>>
m_Trajectory;
public:
DSTrajectory(void)
{
checkPointType();
}
virtual ~DSTrajectory(void){}
protected:
virtual void checkPointType(void) const
{
TDSTrajectoryPointType TypeCheckPoint;
if (
typeid(TTimeType) !=
TypeCheckPoint.returnTimeTypeID() ||
typeid(TStateSpaceType) !=
TypeCheckPoint.returnStateSpaceTypeID()
)
throw std::runtime_error("Error");
}
};
int main()
{
DSTrajectory<
Real,
Rn<>,
DSTrajectoryPointCTRn<>
> T;
std::cout << 222 << std::endl;
}
Aucun commentaire:
Enregistrer un commentaire