1

I noticed something here in this code . even if when push back the elements in vector (see function passtoit). Destructor of struct test is being called . can anyone please explain this to me .. why this behavior is there ...

when i declare the std::vector of static object and let it run it gives me that heap corruption problem and when i declare the test as std::vector of test pointer(*) and delete that pointer as shown in the commented code , it works fine. please explain . it will help me alot . i don't know what else to write here in order to make the stackworkflow understand that it is valid question

    #include "stdafx.h"
    #include <vector>
    #include <iostream>
    #include <vld.h>
    using namespace std;

    class declaration
    class testHelper
    {
    public:
        testHelper(int passed):m(passed){}

        int m;

        ~testHelper(){cout<<"deleting as thought";}
    };
     declaration of structure 
    struct test
    {
       // constructor
        test(string name,testHelper* help):name(name),dummy(help){}

        string name;

        testHelper *dummy;
    // destructor
        ~test()
        {
            cout<<"deleting dummy";

            if(dummy!=NULL)

            {
                delete dummy;

                dummy =NULL;
            }
        }
    };

    function to pass 
    int passtoit()
    {
        std::vector<test> x;
        // push back on the vector
        x.push_back(test("prakash",(new testHelper(10))));

        //for(std::vector<test>::iterator i =x.begin();i!=x.end();++i)

        //{

        //    delete *i;

        //}

        return 0;
    }
      main function
    int _tmain()
    {
        // calling the function
        passtoit();

        return 0;

    }
quantdev
  • 23,517
  • 5
  • 55
  • 88

3 Answers3

1

When using std::vector<test>, elements are copied into the vector.

x.push_back(test("prakash",(new testHelper(10))));

You are creating an instance which is copied then immediately destroyed.

mstrthealias
  • 2,781
  • 2
  • 22
  • 18
0

When we insert elements to vector using push_back, copy constructor is called, then a temporary object is created for the arguement object and is then inserted to vector. Then the temporary object is destroyed/deleted.

To check how it actually works, try overriding the copy constructor in private scope of the class. push_back function will not be called and you will get compiler error.

Quest
  • 2,764
  • 1
  • 22
  • 44
Santosh Sahu
  • 2,134
  • 6
  • 27
  • 51
0

First,when vector is desctructed,it destroys every elemment.

template<class _Ty> inline
    void _Destroy(_Ty _FARQ *_Ptr)
    {   // destroy object at _Ptr
    _DESTRUCTOR(_Ty, _Ptr);
    }
#define _DESTRUCTOR(ty, ptr)    (ptr)->~ty()

Second,You should known when temporary objects are destroyed.

"All temporaries created as a result of expression evaluation are destroyed at the end of the expression statement (that is, at the semicolon), or at the end of the controlling expressions for for, if, while, do, and switch statements."

See this: Temporary Objects

So,the reason for this is the memory pointed by dummy is deleted twice. To avoid this,You should override operator=

test& operator=(const test& t)
    {
        if(this == &t)
            return *this;
        name = t.name;
        dummy = new testHelper(*t.dummy);
        return *this;
    }
wuqiang
  • 634
  • 3
  • 8