0

I am attempting to create a graphical representation of finite automata using xcode, and as such I have created classes for states and transitions. In order to make moving objects easy, I have included a collection of pointers of transitions going in and out of the state. Compiling is fines, but when I try to append to the vector, it produces the following error. EXC_BAD_ACCESS(code=1, address=0x3f35) Following the error takes me to the std library, and shows the error being in this line.

template <class _Tp, class _Allocator>
inline _LIBCPP_INLINE_VISIBILITY
void
vector<_Tp, _Allocator>::push_back(const_reference __x)
{
    if (this->__end_ != this->__end_cap())
    {
        __annotate_increase(1);
        __alloc_traits::construct(this->__alloc(),
                                  _VSTD::__to_raw_pointer(this->__end_), __x);
        ++this->__end_;
    }
    else
        __push_back_slow_path(__x);
}

Here is a simplified version of my State class, my Transition class is declared before and is then defined afterwards.

class State
{
    int id;
    std::vector<Transition *> links_in;
    std::vector<Transition *> links_out;
    float x;
    float y;
    int r = radius; //x, y are centre coordinates of the circle representing the state, while r is the radius
    bool is_active = false;
    bool is_end = false;
    bool is_shown = true;
    bool is_moving;
public:
    // Get Functions go here

    // Set Functions go here

    //Add functions
    void add_in_trans(Transition * t){
        links_in.push_back(t);
    }
    void add_out_trans(Transition * t){
        links_out.push_back(t);
    }
    //Delete Functions
    void remove_in_trans(){
        links_in.pop_back();
    }
    void remove_out_trans(){
        links_out.pop_back();
    }
    void draw_state();
    State(int ix, int iy);
    State(){}
}

If you have any suggestions for a better way of doing this, I am more then happy to hear them. I have spent all day trying to sort this out, to no avail.

Thanks in advance.

UPDATE:

I attempted to use integers and vectors as a temporary fix, but I came up with the same problem, so I assume that the problem isn't the pointers but the way I'm using vectors.

This is the code

#include <vector>

class Transition;

class State
{
    int id;
    std::vector<int> links_in;
    std::vector<int> links_out;
    float x;
    float y;
    int r = radius; //x, y are centre coordinates of the circle representing the state, while r is the radius
    bool is_active = false;
    bool is_end = false;
    bool is_shown = true;
    bool is_moving;
public:
    // Get Functions
    int get_x(){
        return x;
    }
    int get_y(){
        return y;
    }
    int get_id(){
        return id;
    }
    bool is_it_active(){
        return is_active;
    }
    bool is_it_moving(){
        return is_moving;
    }
    bool is_in(int ix, int iy){ //Function to tell if pair of coordinates are in the circle, used to select.
        std::cerr << ix-x << " " << iy-y << " " << r*r << std::endl;
        if ((ix-x)*(ix-x) + (iy-y)*(iy-y) < r*r)
            return true;
        else
            return false;
    }
    // Set Functions
    void set_active(bool s){
        is_active = s;
    }
    void set_moving(bool s){
        is_moving = s;
    }
    void end_switch(){
        is_end = !is_end;
    }
    void set_start(){
        g_start_state = id;
    }
    void set_x(int ix){
        x = ix;
    }
    void set_y(int iy){
        y = iy;
    }
    //Add functions
    void add_in_trans(int t){
        links_in.push_back(t);
    }
    void add_out_trans(int t){
        links_out.push_back(t);
    }
    //Delete Functions
    void remove_in_trans(){
        links_in.pop_back();
    }
    void remove_out_trans(){
        links_out.pop_back();
    }
    void draw_state();
    State(int ix, int iy);
    State(){}
};

State::State(int ix, int iy){
    id = g_state_num;
    if (g_start_state == 0)
        g_start_state = id;
    x = ix;
    y = iy;
}

void State::draw_state(){
    if (is_shown){
        if (is_moving)
            glTranslatef(g_cursor_x, g_cursor_y, 0.0);
        else
            glTranslatef(x, y, 0.0);
        fill_colour();
        if (is_active)
            active_fill_colour();
        glBegin(GL_POLYGON);
        for (size_t i=0; i<24; i++){
            float n[2] = {static_cast<float>(r * cos(i*6)), static_cast<float>(r * sin(i*6))};
            glVertex2fv(n);
        }
        glEnd();
        line_colour();
        glBegin(GL_LINES);
        for (size_t i=0; i<24; i++){
            float n[2] = {static_cast<float>(r * cos(i*6)), static_cast<float>(r * sin(i*6))};
            glVertex2fv(n);
        }
        glEnd();
        if(is_end){
            glPushMatrix();
            glScalef(0.9, 0.9, 0.9);
            for (size_t i=0; i<24; i++){
                float n[2] = {static_cast<float>(r * cos(i*6)), static_cast<float>(r * sin(i*6))};
                glVertex2fv(n);
            }
            glPopMatrix();
        }
        text_colour();
        std::string s = std::to_string(id);
        for (int i=0; i<s.length(); i++){
            glPushMatrix();
            glTranslatef(-radius/2 + i*kerning, -radius/2, 0.0);
            glScalef(0.3, 0.3, 1.0);
            glutStrokeCharacter(GLUT_STROKE_ROMAN, s[i]);
            glPopMatrix();
        }
    }
}

class Character{
    int id;
    char c;
public:
    int get_id(){
        return id;
    }
    char get_char(){
        return c;
    }
    void set_char(char ic){
        c = ic;
    }
    Character(char ic);
    Character(){};
};

Character::Character(char ic){
    id = g_character_num;
    g_character_num++;
    c = ic;
}

class Transition{
    int ident;
    State * from_state;
    State * to_state;
    float from[2];
    float to[2];
    Character c;
public:
    void set_from(float x, float y){
        from[0] = x;
        from[1] = y;
    }
    void set_to(float x, float y){
        to[0] = x;
        to[1] = y;
    }
    void set_char(Character ic){
        c = ic;
    }
    int get_id(){
        return ident;
    }
    void draw_trans();
    void set_trans(State * ifrom, State * ito, Character ic){
        from_state = ifrom;
        to_state = ito;
        from[0] = ifrom->get_x();
        from[1] = ifrom->get_y();
        to[0] = ito->get_x();
        to[1] = ito->get_y();
        c = ic;
    }
    Transition(){};
    Transition(State ifrom, State ito, Character ic){
        from_state = &ifrom;
        to_state = &ito;
        from[0] = ifrom.get_x();
        from[1] = ifrom.get_y();
        to[0] = ito.get_x();
        to[1] = ito.get_y();
        c = ic;
    }
};

void Transition::draw_trans(){
    line_colour();
    glBegin(GL_LINES);
    glVertex2fv(from);
    glVertex2fv(to);
    glEnd();
    float grad = (from[0] - to[0]) /(from[1] - to[1]); //(By finding the gradient of the slope, we can fin good place to show it's information, it's character.
    if (grad < -1 || grad > 1){
        glPushMatrix();
        glTranslatef(from[0] - to[0] - 20, from[1] - to[1], 1.0);
    }
else{
    glPushMatrix();
    glTranslatef(from[0] - to[0], from[1] - to[1] + 20, 1.0);
}
    glutStrokeCharacter(GLUT_STROKE_ROMAN, (c.get_char()));
    glPopMatrix();
}
  • 1
    The posted code looks OK to me. Please post a [Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve). – R Sahu Jan 05 '16 at 01:59
  • You probably would be better using smart pointers rather than dumb ones; [here's an answer that covers these issues](http://stackoverflow.com/a/1361227/1270789). – Ken Y-N Jan 05 '16 at 02:03
  • Your problems might actually be unrelated to your std::vector usage, but rather a result from trashing memory elsewhere. For example, the Transition ctor `Transition(State ifrom, State ito, Character ic){ from_state = &ifrom; to_state = &ito; [...]` does not look good. You take the address of a State parameter which is going out of scope immediately. – ThorngardSO Jan 07 '16 at 11:55
  • Ah yes, I see what you mean. I have started replacing all my constructions that involve other created classes to take their smart pointers instead, and having sorted out some other vector problems happening elsewhere, my programme seems to be working. Thank you. – GreenClover Jan 07 '16 at 13:43

0 Answers0