Beginner c++ sanity check (Code breaks with multiple instances of class)

I have a simple c++ class that basically just holds a couple of objects

Group.h

class Group {
public:
    Mover anchor;
    Spring spring;
    ofColor color;
    
    void init(ofVec3f location);
};

Group.cpp

void Group::init(ofVec3f location) {
    color = mRandomColor();
    
    anchor.location = location;
    
    spring.setTarget(&anchor);
    spring.location = location;
}

I then access the anchor and spring properties directly calling methods on them. This works fine if I have just once instance of Group, but if I add multiple, then things stop working.

Here is how I instantiate the instances and store in a vector:

int GRID_SIZE = 20;
for(int y = 0; y < (bounds.height / GRID_SIZE) - 1; y++) {
    for(int x = 0; x < (bounds.width / GRID_SIZE) - 1; x++) {
    
        ofVec3f l;
        l.x = (x +  1) * GRID_SIZE;
        l.y =  (y + 1) * GRID_SIZE;
        
        Group g;
        g.init(l);
        
        groups.push_back(g);
    }
}

Here is an example how I access the properties in my code:

float dist = (mouseMover.location - g.spring.location).length();
if(dist < 20) {
    ofVec3f force = mouseMover.repel(g.spring);

    g.spring.applyForce(force);
}

I am still learning C++, and wanted to see if someone could give me a sanity check on my class to see if I am doing anything obviously wrong.

it would help to know what’s the problem you are having and what’s spring since that’s the class you seem to be accessing.

Thank you. I didn’t expect anyone to debug all of my code, but want to see if I was doing something obviously wrong with my Group class. But you can view all of the code and project here:

with some of the utility classes here:

You gave me an idea to check some of my older code though (Follower and Mover, which Spring inherits from. Maybe I am exposing a bug in my older code I hadnt hit yet).

i have tracked this down a bit. Basically, one of the ofVec3f instances is being reset (Spring.target.location).

The odd thing is it only happens when I put the Group instances in a Vector. It happens sometime between the first and second ofApp::update calls.

If I dont put them in a vector and just manually manage a bunch everything works fine.

ok. I was able to get this to work, by storing my Group instances in a vector of shared pointers, instead of a vector of Groups.

vector<shared_ptr<Group>> groups;

int GRID_SIZE = 20;
for(int y = 0; y < (bounds.height / GRID_SIZE) - 1; y++) {
    for(int x = 0; x < (bounds.width / GRID_SIZE) - 1; x++) {
    
        ofVec3f l;
        l.x = (x +  1) * GRID_SIZE;
        l.y =  (y + 1) * GRID_SIZE;
        
        shared_ptr<Group> g(new Group());

        g->init(l);
        
        groups.push_back(g);
    }
}

If anyone has any thoughts on why this works, and if there is anything I can do in my class definitions to prevent it, I would be interesting in learning more.