gardian06
gardian06

Reputation: 1546

c++ vector problems

I am working with a std::vector to hold some objects that have dynamically allocated members, and when I go to put the Things into the vector a few things happen that I am not understanding.

  1. I call push_back() and use the constructor of the objects as the argument, but for some reason it goes to the destructor of the object. why is this; it should be adding not deleting?

  2. I call push_back() a second time doing the same as before, but this time it throws an illegal memory access at dbgdel.cpp operator delete (line 52). but delete should never be called in a constructor, or push_back().

I am uncertain what sections of code are pertinent to this question as the lines in question are pretty entrenched in a method.

Edit: Code added

class Thing{
    int** Array;
    int size;   // of square array
    Point current; // location
    Thing(int _n){
        // allocates, and gives values to the array, and members
        // only constructor
    }
};
class ThingMgr{
    Thing * Control;
    Thing * Current;
    Thing * Previous;
    int size;  // size of all. same use as in Thing
    ThingMgr(int _n){
        size = _n;
        Control = new Thing(size);
        Current = new Thing(size);
        Previous = new Thing(size);
    }
    void rearrange(int _num){
        std::vector<Thing> possibles;

        // performs deterministic work on members

        // [0] first
        possibles.push_back(Thing(size)); // this succeeds
        // [1] second
        possibles.push_back(Thing(size)); // this fails

        // more operations to be performed never reached.
    }
};

Upvotes: 0

Views: 488

Answers (2)

Herms
Herms

Reputation: 38788

Are you storing pointers in your vector, or is it copying into it? For example, is it:

class MyClass;
vector<MyClass> my_vector;
//or
vector<MyClass*> my_vector;

If it's the first then when you call push_back() you're creating a local copy of a new object, the vector makes a copy to store in the vector, and then your temporary one leaves scope and is destructed.

As for the second time, I'm guessing your class isn't written to handle copies properly, so the destructor from your temporary instance deletes something, but the copy still references the same pointer. Then when it goes to be deleted for some reason (or your new one references the same thing for some reason) it breaks. Hard to tell without seeing the code for your classes.

Upvotes: 1

K-ballo
K-ballo

Reputation: 81349

first: I call push_back() and use the constructor of the objects as the argument, but for some reason it goes to the destructor of the object. why is this; it should be adding not deleting?

You are pushing a copy of that element into the vector. You construct a temporal element, its copy-constructor is called to create a copy within the vector, then the destructor of the temporal element is called.

second: I call push_back() a second time doing the same as before, but this time it throws an illegal memory access at dbgdel.cpp operator delete (line 52). but delete should never be called in a constructor, or push_back().

It's strange that this happen on a second call, but eventually when the vector needs to regrow it copies elements again.

You are probably failing to provide a proper copy-constructor for the element in question.

Upvotes: 3

Related Questions