I have a method in class like this:
int Class1::addObject(Struct1 object)
{
object.objectID = objCounter;
objCounter++;
vector1.push_back(object); // this line throws exception
// unrelated line
return object.objectID;
}
Vector is defined and initialized as following:
std::vector<Struct1> vector1 = {};
The biggest problem is that this exception occurs sometimes and I am afraid that there is memory leakage. I am using C++14 and I don't think there is problem with that (because I read somewhere that before C++11 vector was not allowed). Also the Struct1 object is initialized before calling this method, so it isn't about that neither. Could it be because Visual Studio doesn't have Administrator privileges or it may be due to vector changing location in memory when adding more elements and process couldn't allocate more memory (I think this can be it)? The third possible suspect is multithreading: since I am accessing Class1 with 4 threads at the time, but every thread has it's own group of objects that it adds/removes, and that group is never the same for 2 or more threads?
Update 1: Definition of Struct1 (copy and default constructors are added when someone suggested in answers)
struct Struct1
{
int objectID;
Struct1ObjectType objectType;
GLfloat array1[2];
GLfloat array2[3];
GLfloat objectSize;
Struct1() {}
Struct1(const Struct1& cs1)
{
objectID = cs1.objectID;
objectType = cs1.objectType;
for (int i = 0; i < 2; i++)
array1[i] = cs1.array1[i];
for (int i = 0; i < 3; i++)
array2[i] = cs1.array2[i];
objectSize = cs1.objectSize;
}
};