I don't really have an explanation and whoever thought that this new posting format was the way to go... idk what to say. I really don't like it. I can only post so much lines of code before something happens to the window and I can't scroll all the way down.

Anyway, its segfaulting when I assign a value to a reference. Not sure why that's happening.

repost from cplusplus.com. Just trying to get some answers don't mean to spam.

GDB:

Program received signal EXC_BAD_ACCESS, Could not access memory.
Reason: KERN_INVALID_ADDRESS at address: 0x0000000000000030
[Switching to process 27451]
0x0000000100007413 in explosionhandler::registerexplosion (this=0x0, ttype=@0x10000fa5c, b=@0x10000f960, seq=3, a=120, m=0, e=-18) at explosionhandler.cpp:222
222     ttype = num_types; //there was a lot of code commented out 

.H:

#include <allegro5/allegro.h>
#include <allegro5/allegro_image.h>
#include "collarea.h"
#include <vector>
#ifndef EXP_H
#define EXP_H
using std::vector;
class ALLEGRO_BITMAP;
class collarea;
class explosionhandler {
public:

    struct explosion {
        int type;
        int seq;
        float x,y;
        float radius;
    };
    vector<struct explosion> explosions;
    struct explosion_type {
        //int type;
        int num_seq;
        //equation: size(#) = a + m*# - e*#^2
        float a,e,m;
        ALLEGRO_BITMAP* exp;
        explosion_type *next;
    };// *type;
    vector<struct explosion_type> type;
    int num_types;

    explosionhandler();
    ~explosionhandler();

    void add(explosion_type* a);

    void registerexplosion(int& ttype,ALLEGRO_BITMAP*& b,int seq, float a, float m,float e);

    void createexplosion(int ttype,float x,float y);
    void drawexplosions(ALLEGRO_BITMAP* screen);

    float getfloat(float& a);
    void gettype(explosion_type& a,ALLEGRO_BITMAP*& b,int& nseq, float& aa, float& ee, float& mm);

    int checkkill(collarea a);

};
#endif 

.cpp:

#include "explosionhandler.h"
#include <math.h>
explosionhandler::explosionhandler()
{
    num_types=0;
}
void explosionhandler::registerexplosion(int& ttype,ALLEGRO_BITMAP*& b,int seq, float a, float m,float e)
{
    explosion_type n;
    n.num_seq=seq;
    n.exp=b;
    n.a=a;
    n.m=m;
    n.e=e;
    ttype = num_types; //right here is the issue
    num_types++;
    type.push_back(n);
}
void explosionhandler::createexplosion(int ttype,float x,float y)
{
    if((ttype < num_types)&&(ttype>-1))
    {
        explosion e;
        e.type=ttype;
        e.x=x;
        e.y=y;
        e.seq=1;
        e.radius = (type.at(ttype).a) + (type.at(ttype).m) - (type.at(ttype).e);
        explosions.push_back(e);
    }
}
float explosionhandler::getfloat(float& a)
{
    return a;
}
void explosionhandler::drawexplosions(ALLEGRO_BITMAP* screen)
{
    al_set_target_bitmap(screen);
    float a,m,e;
    int ttype,seq;
    ALLEGRO_BITMAP *b;
    int hh = explosions.size();
    for(int i=0;i<hh;i++)
    {
        explosion x = explosions[0];
        explosions.erase(explosions.begin());
        gettype(type.at(x.type),b,seq,a,e,m);
        al_draw_scaled_bitmap(b,0,0,al_get_bitmap_width(b),al_get_bitmap_height(b),getfloat(x.x),getfloat(x.y),getfloat(x.radius)*2,getfloat(x.radius)*2,0);
        x.seq++;
        if(x.seq<=seq)
        {
            float oldradius=x.radius;
            x.radius=(a+m*x.seq-e*x.seq*x.seq)/2;
            x.x-=(x.radius-oldradius);
            x.y-=(x.radius-oldradius);
            explosions.push_back(x);
        }
    }
}
void explosionhandler::gettype(explosion_type& a, ALLEGRO_BITMAP*& b, int& nseq, float& aa, float& ee, float& mm)
{
    b=a.exp;
    nseq=a.num_seq;

    aa=a.a;
    ee=a.e;
    mm=a.m;
}
int explosionhandler::checkkill(collarea a)
{
    int yes=0;
    int h=explosions.size();
    for(int i=0;i<h;i++)
    {
        float x = explosions.at(i).x;
        float y = explosions.at(i).y;
        float seq = explosions.at(i).seq;
        if(seq)
        {
            float xx,yy;
            a.toxy(&xx,&yy,a.getbl());
            float bx,by;
            a.toxy(&bx,&by,a.getbr());
            xx  = (abs(x-xx)<abs(x-bx)) ? xx : bx;
            yy = (abs(y-yy)<abs(y-by)) ? yy : by;
            a.toxy(&bx,&by,a.gettl());
            xx = (abs(x-xx)<abs(x-bx)) ? xx : bx;
            yy = (abs(y-yy)<abs(y-by)) ? yy : by;
            a.toxy(&bx,&by,a.gettr());
            xx = (abs(x-xx)<abs(x-bx)) ? xx : bx;
            yy = (abs(y-yy)<abs(y-by)) ? yy : by;
            float dd = sqrt((x-xx)*(x-xx) + (y-yy)*(y-yy));
            if(dd < explosions.at(i).radius)
            {
                yes++;
            }
        }

    }
    return yes;

}

Read the error report:

... in explosionhandler::registerexplosion (this=0x0, ...

Obviously, the "this" pointer is NULL, which means the error is in the code from which this function was called. You must post that code if you want us to help you find the error.

As for the bug with posting long code segments, I have noticed that too, I will file a bug report about it.

Its being called in an object in main:

rocket.h:

#include <allegro5/allegro.h>
#include <allegro5/allegro_image.h>
#include "collarea.h"
#include "explosionhandler.h"
#include <vector>
#ifndef R1_H
#define R1_H
#define PI 3.14159265
class ALLEGRO_BITMAP;
class collarea;
class explosionhandler;
using std::vector;
class rocket
{
public:
    rocket(float sp,float w,float h);
    ~rocket();

    collarea area;

    struct rk {
        float x,y,dx,dy,theta;
        int seq;
    };
    vector<struct rk> rockets;

    explosionhandler *handler;

    int limit;
    int launched;
    int internal_ptr;


    void setlimit(int a);


    void renew();
    void update();

    void setrocket(ALLEGRO_BITMAP*& a,ALLEGRO_BITMAP*& b, explosionhandler*& h);
    void launch(float xx, float yy,float ttheta);
    void launch(collarea you, collarea other);
    int draw(float *xx, float *yy, float *ttheta);
    void checkexplode(collarea a);
    void move(collarea a);

    void setexptype(int a);

    int exptype; //this is the variable.

private:
    float width, height;
    float speed;
    float killdist;
    //float x,y,dx,dy;
    float dist;
    float destx,desty;
    float ox,oy;
    //int seq;
    int vdist; //for explosion sequence

};
#endif 

The relevant (I think) code snippet from rocket.cpp:

rocket::rocket()
{
...
exptype=-1;
}
void rocket::setrocket(ALLEGRO_BITMAP*& a,ALLEGRO_BITMAP*& b, explosionhandler*& h)
{
handler = h;
area.sethitboundaries(a);
fprintf(stdout,"setrocket, # of rockets in vector: %i\n",(int)rockets.size());
h->registerexplosion(exptype,b,3,(float)al_get_bitmap_width(b),(float)0,(float)-18); //called function
}

And for the hell of it an extremely abbreviated version of main.cpp:

#include "tank.h"
#include "collarea.h"
#include "rocket.h"
#include "explosionhandler.h"
#include <allegro5/allegro.h>
#include <allegro5/allegro_image.h>
#include <stdio.h>
#include <cstdlib>
#define PI 3.14159265
#define degtorad(x) ((x/180) * PI)
...
rocket rock(bullet_speed+2,width,height);
explosionhandler *handler;
...
int setup()
{
    ...
    rock.setrocket(rk,exp,handler);
    rock.setlimit(5);
    al_set_target_bitmap(al_get_backbuffer(display));
    ...
}
...

omg its because *handler is a bad pointer not pointing to anything! YAY see I knew I could count on you guys.

All I did was read the error message that you got. See, it helps to pay attention to the error messages!

Lesson learned, I hope.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.