confusing segmentation fault?

This is a supporting source file of a larger (but still pretty small) game.

Yeah this is kind of a repeat of my earlier thread but I got rid of the linked list and am using 2 vectors. Let me show you:

.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#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;
}


GDB:

1
2
3
4
5
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 
It looks like you are calling the registerexplosion function on a null pointer.
Let me show you where its being used for context:

.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#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 


.cpp:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include "rocket.h"
#include <math.h>
#include <stdio.h>
#define abs(x) (((x)>0) ? (x) : (-(x)))
#define growth(x) (1+.18*((x)*(x)))
rocket::rocket(float sp,float w,float h)
{
	speed = sp;
	launched=0;
	limit=0;
	internal_ptr=0;
	width=w;
	height=h;
	rockets.resize(0);
	exptype=-1;  //******** this is the variable initialized **********/
	fprintf(stdout,"constructor, # of rockets in vector: %i\n",(int)rockets.size());
}
void rocket::setexptype(int a)
{
	exptype=a;
}
void rocket::setlimit(int a)
{
	limit=a;
	launched=0;
	fprintf(stdout,"Set Limit called, # of rockets in vector: %i\n",(int)rockets.size());
}
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
}
void rocket::launch(float xx, float yy,float ttheta)
{
	fprintf(stdout,"Launched, before: %i\n",(int)rockets.size());
	if(launched<limit)
	{
		struct rk next;
		next.x=xx; next.y=yy; next.theta=ttheta;
		next.dx = speed * cos(ttheta);
		next.dy = speed * sin(ttheta);
		next.seq=0;
		launched++;
		rockets.push_back(next);
	}
	fprintf(stdout,"Launched, after: %i\n",(int)rockets.size());
}
void rocket::launch(collarea you, collarea other)
{
	if(launched<limit)
	{
		//fprintf(stdout,"In launch, in vector, Before: %i\n",(int)rockets.size());
		struct rk next;
		float xx,yy;
		you.getxy(&xx,&yy);
		next.x=xx; next.y=yy;
		other.getxy(&xx,&yy);
		float ratio = (next.y-yy)/(next.x-xx);
		next.theta = atan(ratio);
		next.dx = speed * cos(next.theta);
		next.dy = speed * sin(next.theta);
		next.seq=0;
		launched++;
		rockets.push_back(next);
		//fprintf(stdout,"In launch, in vector, After: %i\n",(int)rockets.size());
	}
}
int rocket::draw(float *xx, float *yy, float *ttheta)
{
	internal_ptr++;
	*xx = -1;
	*yy = -1;
	*ttheta = -1;
	int i=1;
	if(internal_ptr>=rockets.size())
	{
		i=0;
		internal_ptr=-1;
	}
	else if(!rockets.at(i).seq)
	{
		*xx=rockets.at(internal_ptr).x;
		*yy=rockets.at(internal_ptr).y;
		*ttheta=rockets.at(internal_ptr).theta;
		//fprintf(stdout,"In draw, parameters: ptr: %i, x: %f, y: %f, theta: %f\n",internal_ptr,*xx,*yy,*ttheta);
	}
	return i;
}
void rocket::checkexplode(collarea a)
{
	int g = rockets.size();
	fprintf(stdout,"In checkexplode(), rockets fired: %i, launched: %i\n",g,launched);
	for(int i=0;i<g;i++)
	{
		rk temp = rockets.at(0);
		if(!temp.seq)
		{
			float x=temp.x;
			float y=temp.y;
			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));
			//fprintf(stdout,"dd: %f, killdist: %f\n",dd,killdist);
			if(dd < killdist)
			{
				fprintf(stdout,"\n\nRocket should explode\n\n");
				temp.seq=1;
				handler->createexplosion(exptype,x,y);
			}
		}
		rockets.push_back(temp);
	}
}
void rocket::update()
{
	//fprintf(stdout,"In update, in vector: Before: %i\n",(int)rockets.size());
	int g=rockets.size();
	for(int i=0;i<g;i++)
	{
		rk temp = rockets.at(0);
		rockets.erase(rockets.begin());
		if(temp.seq)
		{
			temp.seq++;
		}
		else 
		{
			temp.x+=temp.dx;
			temp.y+=temp.dy;
		}
		rockets.push_back(temp);
	}
	//fprintf(stdout,"In update, in vector: Before: %i\n",(int)rockets.size());
}
void rocket::renew()
{
	//fprintf(stdout,"In renew, in vector: Before: %i\n",(int)rockets.size());
	int j=rockets.size();
	for(int i=0;i<j;i++)
	{
		rk temp = rockets.at(0);
		rockets.erase(rockets.begin());
		if(!((temp.x<0)||(temp.x>width)||(temp.y<0)||(temp.y>height)||(temp.seq>3)))
		{
			rockets.push_back(temp);
		}
	}
	//fprintf(stdout,"In renew, in vector: Before: %i\n",(int)rockets.size());
}
void rocket::move(collarea a)
{
	//fprintf(stdout,"In move, before sequence, in vector: Before: %i\n",(int)rockets.size());
	checkexplode(a);
	renew();
	update();
	//fprintf(stdout,"In move, after sequence, in vector: After: %i\n",(int)rockets.size());
}
rocket::~rocket()
{
	while(rockets.size())
	{
		rockets.erase(rockets.begin());
	}
	rockets.resize(0);
}



I guess I should've just post snippets lol whatever you get the idea.

explosionhandler is a pointer in main passed to rocket. rocket is a global object in main as is explosionhandler.
h->registerexplosion(exptype,b,3,(float)al_get_bitmap_width(b),(float)0,(float)-18);
Make sure h is not a null pointer.
it was. I initialized it. Problem Solved. Thanks.
Topic archived. No new replies allowed.