0

I have a class like this:

class OBJ{...};

class A
{
   public:
   vector<OBJ> v;
   A(int SZ){v.clear(); v.reserve(SZ);}
};

A *a = new A(123);
OBJ something;
a->v.push_back(something);

This is a simplified version of my code. The problem is in debug mode it works perfect. But in release mode it crashes at "push_back" line. (with all optimization flags OFF) I debugged it in release mode and the problem is in the constructor of A. the size of the vector is something really big with dummy values and when I clear it, it doesn't change...

Do you know why?

Thanks,

skaffman
  • 398,947
  • 96
  • 818
  • 769
Nima
  • 854
  • 3
  • 11
  • 18
  • Since the vector stores OBJ instances directly (instead of pointers to OBJ), the push_back will copy-construct OBJ instances. Is the copy-constructor for OBJ correctly implemented? – Patrick May 27 '10 at 19:27

1 Answers1

0

I can guess - I would say that OBJ probably does not have a correctly implemented copy constructor and/or assignment operator and destructor.

  • OBJ has constructor, destructor, copy constructor, default constructor. My question is "Why the capacity of the vector is always something very big?" I used clear() and resize(0). didn't work.. – Nima May 27 '10 at 20:17
  • @Nima The fact that it has these makes it even more likely that this is the problem. Post some real code. –  May 27 '10 at 21:27