-1

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;
  }
};
straus012
  • 3
  • 1
  • 6
  • 4
    You have some kind of issues with moving of `Struct1` probably it doesn't satisfy the rule of 5. But there is no way to tell for sure since you don't share the code. Exception would occur primarily if there is something wrong with the behaviour of the `struct`. – ALX23z Mar 07 '21 at 19:34
  • @ALX23z As I said in post this exception occurs only sometimes, which means that sometimes it works fine, and that moving Struct1 object is okay. Because of random behavior I suspect mostly multithreading and non allocating memory. – straus012 Mar 07 '21 at 19:38
  • From the look of the exception it might be the case that you allocate data in one dll and delete it in another - which is an error because dlls don't share heap and delete operation must be called from the same dll. – ALX23z Mar 07 '21 at 19:45
  • 3
    You may have UB somewhere in your code which is changing into vector's internal pointers. Hard to say without [mcve]. You mentioned multithreading, none of vector's methods are thread-safe. Might be worth a shot running the project through sanitizers - thread, memory, leak, undefined, address. Or at least Valgrind if you cannot easily recompile the project with them. – Quimby Mar 07 '21 at 19:53
  • 1
    Please show `Struct1` and please read [ask] with a mcve] – Richard Critten Mar 07 '21 at 19:53
  • 1
    @Quimby you are probably breaking the rule of 3/5/0 and until we get a complete [mcve] everything will be guess work. Missing from the amended question `Struct1ObjectType` See also https://stackoverflow.com/questions/4172722/what-is-the-rule-of-three – Richard Critten Mar 07 '21 at 20:20

2 Answers2

3

Re:

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?

Regardless of objects being different, you can't access the same vector from multiple threads without synchronization.

One way of doing this is with mutex:

std::mutex m_lock;

then when you need to access that vector:

{   
    const std::lock_guard<std::mutex> l(m_lock);
    vector1.push_back(object); // this line throws exception
}
Vlad Feinstein
  • 10,960
  • 1
  • 12
  • 27
-2

The C++ standard library uses "copy semantics" for elements in its collections. The code line vector1.push_back(object); cause the copy-construction of a Struct1 object (the object that is stored inside the std::vector<>, which is a copy of the object that is in the parameter).

You haven't shown the definition of class Struct1 so I don't know how it's being copy-constructed, but I would check there to see if you have a programming bug in that code.

Cow Corporation
  • 399
  • 2
  • 7
  • As I said in post this exception occurs only sometimes, which means that sometimes it works fine, and that moving Struct1 object is okay. Because of random behavior I suspect mostly multithreading and non allocating memory. – straus012 Mar 07 '21 at 19:38
  • There's more to "copy-construction" than only just "allocation of memory". What additional actions occur during "copy-construction" will depend on how the copy-construction function has been implemented (which, again, you haven't shown for your `Struct1` example). Sure, it's possible that the non-deterministic behavior that you observe could be due to multithreading hazards -- I suggest that you take a look at your copy-constructor implementation and see if you have any incorrectly-written multithreading code there. – Cow Corporation Mar 07 '21 at 19:43