1

嗨,我正在使用模板开发一个多容器,但是我从子类析构函数中得到了分段错误,这是代码:

#include <algorithm>
#include <map>
#include <iostream>
class BaseType{
public:
    virtual ~BaseType(){}
    virtual BaseType * clone() const =0;
};

template<typename T>
class DataType : public BaseType
{
public:
    DataType(const T & aValueData = T()):mValue(aValueData) {
       // new DataType<T>(*this)
    }
    ~DataType(){

    }
    BaseType * clone() const
    {
        return new DataType<T>(*this);
    }

    T mValue;
};

    class MValueData
    {
    public:
        template<typename T>
        MValueData(T const  & aAnyValue = T()):mTypeData(0),isDelete(false)
        {
            std::cout<<"Object Address before create object: "<<mTypeData<<std::endl;
            mTypeData=new DataType<T>(aAnyValue);
            std::cout<<"Object Address after create object"<<mTypeData<<std::endl;
        }
        ~MValueData(){
            std::cout<<"Object Address "<<mTypeData<<std::endl;

            delete mTypeData;
            mTypeData=0;


        }
        MValueData()
        {
           mTypeData=0;
        }
        template<typename T>
        MValueData(const MValueData & aCopy)
        {

            mTypeData= new  DataType<T>();
           *mTypeData=aCopy.mTypeData;
        }
        template<typename T>
        const MValueData & operator=(const MValueData & aCopy)
        {
             mTypeData= new  DataType<T>();
             *mTypeData=aCopy.mTypeData;
            //MValueData(aCopia).swap(*this);
        }
         void swap(MValueData& other) {
             std::swap(this->mTypeData, other.mTypeData);
         }

         template <typename T>
         T& get()
         {
                return dynamic_cast<DataType<T>&>(*this->mTypeData).mValue;
         }

         bool operator <(const MValueData &rhs) const {
             return (mTypeData<rhs.mTypeData);
         }
         template<typename T>
         void setValue(T const & anyValue=T())
         {
             mTypeData= new DataType<T>(anyValue);
         }
         BaseType *mTypeData;
    private:

         bool isDelete;
    };

    int main()
    {
        MValueData aAnyType_1(0.22);
        aAnyType_1.get<double>();
        MValueData aAnyType_2(false);
        std::map<MValueData , MValueData&> mMapa;
        mMapa.insert(std::pair<MValueData  , MValueData&>(aAnyType_1,aAnyType_2));
//        mMapa.find(aAnyType_1);
        return 0;
    }

我正在使用 GDB 来确定错误,但我看不到正确的修复方法,当我评论此行时,segmentacion 停止:

 ~MValueData(){
         //   if(mTypeData)    delete mTypeData;
        }

只有这样它才能正常运行,但似乎我正在创建内存泄漏。更新:std::map 创建我插入的对象的副本,对象被销毁两次,一​​次在退出主函数时,另一次在 std::map 自行销毁时,有什么提示吗?提前谢谢!

4

3 回答 3

5

This segmentation fault might appear to be in the destructor, but it is a problem in your copy constructor. Lets take a simple example, I have a class which stores a pointer. Then I copy this pointer value like you are doing: I will have two pointers to the same memory location. Now I delete one of these objects, thus deleting the value at the pointer. The second object will have a pointer to invalid memory, and when this tries to delete the memory you get a segmentation fault.

How to fix this:

Well there are a few ways actually. Firstly, you need to decide whether you want deep copying of the pointers or not. If you do, write a deep copy of the memory the pointer points too. If not I reocmmend using shared_ptr to avoid these sorts of problems.

于 2013-09-23T17:40:37.023 回答
2

Your copy constructor is broken. You are not cloning the object, but only the pointer. Once an object is copied, the two copies will try to delete the same real object in memory in the destructor and that will cause a crash. You need to figure out who should own the object and whether you want it cloned or shared among the different instances of the MValueData object. Then act accordingly to fix the issue.

于 2013-09-23T17:40:09.343 回答
1

您的复制构造函数不正确。

它只复制指针而不是对象。

使用复制构造函数复制的两个对象将尝试在其析构函数中删除同一个对象。

于 2013-09-23T17:41:55.120 回答