A seemingly simple thing that is driving me crazy. If I declare 3 vectors in the ofApp header file and then start adding to them (push_back) I get an EXC_BAD_ACCESS (code=2, address=0x20000) error with the ofVector overloaded = operator. When I query that size of the vectors before I do anything with them, the first one is always zero but the others can look enormous.
I am using Xcode 6.1.1 and oF8.4 and output would be:
size of particles1: 0
size of particles2: 4294961909
size of particles3: 110
and get EXC_BAD_ACCESS (code=2, address=0x20000) failure
Interestingly, if I run it on code blocks/windows/oF8.0 I get:
size of particles1: 0
size of particles2: 0
size of particles3: 0
and it runs great …
I am using the super simple oF vector example and I just added two additional vectors from what was in the example …
Trying to use vectors on a different project but having the same problem. Most examples of best practices with vectors declare the vector then start with a push_back to populate. I typically try to initialize all variables before using them but in this case I don’t know the size the vector will be before I start and was hoping to just follow the typical declare then start push-back paradigm to grow it. Can anyone tell me what I am doing wrong? Thanks…
ofApp.h
#pragma once
#include "ofMain.h"
// create the particle class in the header for brevity
class Particle{
public:
Particle(){
pos.set( 0, 0, 0 );
vel.set( ofRandom(-5,5), ofRandom(-4,-8), 0);
}
void update(){
pos += vel;
vel.y += .1;
}
void draw(){
ofEllipse(pos.x,pos.y,10,10);
}
ofPoint pos;
ofVec3f vel;
};
class ofApp : public ofBaseApp{
public:
void setup();
void update();
void draw();
void keyPressed(int key);
void keyReleased(int key);
void mouseMoved(int x, int y);
void mouseDragged(int x, int y, int button);
void mousePressed(int x, int y, int button);
void mouseReleased(int x, int y, int button);
void windowResized(int w, int h);
void dragEvent(ofDragInfo dragInfo);
void gotMessage(ofMessage msg);
// declare the vector of particles
vector<Particle> particles;
vector<Particle> particles2;
vector<Particle> particles3;
};
oFApp.cpp
#include "ofApp.h"
// write our boolean remove function
bool shouldRemove(Particle &p){
if(p.pos.y > ofGetHeight() )return true;
else return false;
}
//--------------------------------------------------------------
void ofApp::setup(){
ofSetFrameRate(60);
cout << " size of particle1: " << particles.size() << endl;
cout << " size of particle2: " << particles2.size() << endl;
cout << " size of particle3: " << particles3.size() << endl;
// particles.clear();
// particles2.clear();
// particles3.clear();
// particles.resize(1);
// particles2.resize(1);
//// particles3.resize(1);
// particles.reserve(5);
// particles2.reserve(5);
// particles3.reserve(5);
// cout << " POST CLEAR size of particle1: " << particles.size() << endl;
// cout << " POST CLEAR size of particle2: " << particles2.size() << endl;
// cout << " POST CLEAR size of particle3: " << particles3.size() << endl;
}
//--------------------------------------------------------------
void ofApp::update(){
for(vector<Particle>::iterator it = particles.begin(); it != particles.end(); it++)
{
(*it).update();
}
ofRemove(particles,shouldRemove);
for(vector<Particle>::iterator it = particles2.begin(); it != particles2.end(); it++)
{
(*it).update();
}
ofRemove(particles2,shouldRemove);
for(vector<Particle>::iterator it = particles3.begin(); it != particles3.end(); it++)
{
(*it).update();
}
ofRemove(particles3,shouldRemove);
cout << " RUNNING size of particle1: " << particles.size() << endl;
cout << " RUNNING size of particle2: " << particles2.size() << endl;
cout << " RUNNING size of particle3: " << particles3.size() << endl;
}
//--------------------------------------------------------------
void ofApp::draw(){
ofTranslate( ofGetWidth()/2, ofGetHeight()/2);
for(vector<Particle>::iterator it = particles.begin(); it != particles.end(); it++)
{
(*it).draw();
}
ofTranslate( ofGetWidth()/2, ofGetHeight()/2);
for(vector<Particle>::iterator it = particles2.begin(); it != particles2.end(); it++)
{
(*it).draw();
}
ofTranslate( ofGetWidth()/2, ofGetHeight()/2);
for(vector<Particle>::iterator it = particles3.begin(); it != particles3.end(); it++)
{
(*it).draw();
}
}
//--------------------------------------------------------------
void ofApp::keyPressed(int key){
particles.push_back( Particle() );
particles2.push_back( Particle() );
particles3.push_back( Particle() );
}
//--------------------------------------------------------------
void ofApp::keyReleased(int key){
}
//--------------------------------------------------------------
void ofApp::mouseMoved(int x, int y ){
}
//--------------------------------------------------------------
void ofApp::mouseDragged(int x, int y, int button){
}
//--------------------------------------------------------------
void ofApp::mousePressed(int x, int y, int button){
}
//--------------------------------------------------------------
void ofApp::mouseReleased(int x, int y, int button){
}
//--------------------------------------------------------------
void ofApp::windowResized(int w, int h){
}
//--------------------------------------------------------------
void ofApp::gotMessage(ofMessage msg){
}
//--------------------------------------------------------------
void ofApp::dragEvent(ofDragInfo dragInfo){
}
Originally from
http://www.openframeworks.cc/tutorials/c++%20concepts/001_stl_vectors_basic.html