Note that there are some explanatory texts on larger screens.

plurals
  1. POc++ Segmentation fault in destructor child class
    primarykey
    data
    text
    <p>hi i am delevop a multicontainner using templates, but i am getting a segmentation fault from the child class destructor, here is the code:</p> <pre><code>#include &lt;algorithm&gt; #include &lt;map&gt; #include &lt;iostream&gt; class BaseType{ public: virtual ~BaseType(){} virtual BaseType * clone() const =0; }; template&lt;typename T&gt; class DataType : public BaseType { public: DataType(const T &amp; aValueData = T()):mValue(aValueData) { // new DataType&lt;T&gt;(*this) } ~DataType(){ } BaseType * clone() const { return new DataType&lt;T&gt;(*this); } T mValue; }; class MValueData { public: template&lt;typename T&gt; MValueData(T const &amp; aAnyValue = T()):mTypeData(0),isDelete(false) { std::cout&lt;&lt;"Object Address before create object: "&lt;&lt;mTypeData&lt;&lt;std::endl; mTypeData=new DataType&lt;T&gt;(aAnyValue); std::cout&lt;&lt;"Object Address after create object"&lt;&lt;mTypeData&lt;&lt;std::endl; } ~MValueData(){ std::cout&lt;&lt;"Object Address "&lt;&lt;mTypeData&lt;&lt;std::endl; delete mTypeData; mTypeData=0; } MValueData() { mTypeData=0; } template&lt;typename T&gt; MValueData(const MValueData &amp; aCopy) { mTypeData= new DataType&lt;T&gt;(); *mTypeData=aCopy.mTypeData; } template&lt;typename T&gt; const MValueData &amp; operator=(const MValueData &amp; aCopy) { mTypeData= new DataType&lt;T&gt;(); *mTypeData=aCopy.mTypeData; //MValueData(aCopia).swap(*this); } void swap(MValueData&amp; other) { std::swap(this-&gt;mTypeData, other.mTypeData); } template &lt;typename T&gt; T&amp; get() { return dynamic_cast&lt;DataType&lt;T&gt;&amp;&gt;(*this-&gt;mTypeData).mValue; } bool operator &lt;(const MValueData &amp;rhs) const { return (mTypeData&lt;rhs.mTypeData); } template&lt;typename T&gt; void setValue(T const &amp; anyValue=T()) { mTypeData= new DataType&lt;T&gt;(anyValue); } BaseType *mTypeData; private: bool isDelete; }; int main() { MValueData aAnyType_1(0.22); aAnyType_1.get&lt;double&gt;(); MValueData aAnyType_2(false); std::map&lt;MValueData , MValueData&amp;&gt; mMapa; mMapa.insert(std::pair&lt;MValueData , MValueData&amp;&gt;(aAnyType_1,aAnyType_2)); // mMapa.find(aAnyType_1); return 0; } </code></pre> <p>I am using GDB to determinate the bug but i cannot see proper way to fix, the segmentacion stop when i comment this line:</p> <pre><code> ~MValueData(){ // if(mTypeData) delete mTypeData; } </code></pre> <p>Only then it run propperly, but it seems that i am creating a memory leak. Updated:std::map create copys of the object that i insert into, the object is destroyed twice, one when exit the main function and another when std::map is destroying it self, any hint? thx in advance!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload