0

I have a problem that arised with another question I made. I am using a list of pointers of a custom class (class that I have implemented), and the program crashes when I call this method, however, if I do not use a pointer list, it works fine. This is the function where the program crashes:

void enemy_spawner()
{
    Point2D enemy_pos = Point2D(rand() % (SCREEN_WIDTH - 20) + 20, -20);
    Point2D enemy_vel = Point2D(0, rand() % 4 + 1);

    Sprite* enemy = new Sprite(enemy_pos, enemy_vel, enemy_image, enemy_info);
    //Kamikaze* enemy = new Kamikaze(enemy_pos, enemy_vel, 0, enemy_image, enemy_info);

    if (enemy_group.size() < MAX_ENEMIES)
    {
        // Program crashes here
        enemy_group.push_back(enemy);
    }

}

The enemy_group is a global variable defined as follows:

std::list<Sprite*> enemy_group;

My Sprite class is as follows (I have read that I need to have properly defined copy constructors, etc)

class Sprite
{
private:
    Point2D sp_pos;
    Point2D sp_vel;
    SDL_Surface* sp_img;
    Point2D sp_center;
    Point2D sp_size;
    double sp_radius;
    bool sp_animated;
    int sp_frames;
    int sp_cur_frame;

public:
    Sprite() {}
    Sprite(Point2D pos, Point2D vel, SDL_Surface *img, ImageInfo info, bool animated = false, int frames = 0);
    virtual void draw(SDL_Surface* screen);
    virtual void update();
    void setInfo (ImageInfo info);
    void setPos( Point2D pos ) { sp_pos = pos; }
    void setVel( Point2D vel ) { sp_vel = vel; }
    void setImg (SDL_Surface* img) { sp_img = img; }
    void setNextFrame() { sp_cur_frame++; }
    void setFrame( int frame ) { sp_cur_frame = frame; }
    void setAnimated(bool animated) { sp_animated = animated; }
    void changeVelX (int c) { sp_vel.setX(c);}
    void changeVelY (int c) { sp_vel.setY(c);}
    void changePosX (int c) { sp_pos.setX(c);}
    void changePosY (int c) { sp_pos.setY(c);}
    SDL_Surface* getImg() { return sp_img; }
    Point2D getPos() { return sp_pos; }
    Point2D getVel() { return sp_vel; }
    Point2D getCenter() { return sp_center; }
    Point2D getSize() { return sp_size; }
    double getRadius() { return sp_radius; }
    int getCurFrame() { return sp_cur_frame; }
    int getFrames() { return sp_frames; }
    bool collide(Sprite &another_sprite);
    virtual ~Sprite() {}

    Sprite& operator=(Sprite other) {
        std::swap(sp_pos, other.sp_pos);
        std::swap(sp_vel, other.sp_vel);
        std::swap(sp_img, other.sp_img);
        std::swap(sp_center, other.sp_center);
        std::swap(sp_size, other.sp_size);
        std::swap(sp_radius, other.sp_radius);
        std::swap(sp_animated, other.sp_animated);
        std::swap(sp_frames, other.sp_frames);
        std::swap(sp_cur_frame, other.sp_cur_frame);
        return *this;
    }

    Sprite(const Sprite &obj)
    {
        sp_pos = obj.sp_pos;
        sp_vel = obj.sp_vel;
        sp_img = new SDL_Surface;
        *sp_img = *obj.sp_img;
        sp_center = obj.sp_center;
        sp_size = obj.sp_size;
        sp_radius = obj.sp_radius;
        sp_animated = obj.sp_animated;
        sp_frames = obj.sp_frames;
        sp_cur_frame = obj.sp_cur_frame;
    }
};

If it matters, I am using gcc compiler (Actually my IDE is codeblocks). The question is, why does the program crash? How do I solve this issue?

Thanks in advance

dpalma
  • 500
  • 5
  • 20
  • `enemy_group` is global, could it be a static initialization order fiasco? – Columbo Jan 05 '15 at 13:45
  • I am sort of a noob in C++, could you explain that some more? Thanks for the response! – dpalma Jan 05 '15 at 13:48
  • Reduce your code to a [mcve](http://stackoverflow.com/help/mcve). You might solve the problem yourself while doing so. Use a debugger to figure out how `enemy_spawner` gets called. – eerorika Jan 05 '15 at 13:49
  • Actually, while I was starting to follow you suggestion user2079303, I inquired a little bit about what Columbo said. And his guess was right so far, I changed a little bit the code, passing by reference the pointer list to the enemy_spawner function and not using it as a global variable. The program does not crash, but I will test it a little bit more. Thank you for your responses :) – dpalma Jan 05 '15 at 14:04

0 Answers0