Archived

This topic is now archived and is closed to further replies.

knealeaj

Unhandled exception !!!!!!!

Recommended Posts

I am putting a particle engine into my project. the particle engine compiles but when i create a particle object and try to render it, my program crashes. The debugger tells me i have a unhandled exception and an access violation. What does this mean?? Thanks Here is the revised(from the last post )code still messy but ill get there in the end!? //Particle Structure************************************************************************************************************* struct particles { bool active; //active? Yes/No float life; //life span of the particle float fade; //fade speed float r; float g; float b; //RGB values Vector Pos; Vector Speed; Vector Force; //coordinates to display the particle //describes particle direction vector //controls force on the particle e.g yg can be considered gravity }; //a class to organise the particles class Particle { private: particles particle[MAX_PARTICLES]; //particle objects public: Particle(Vector speed, Vector Position,Vector force,float r,float g,float b) { for(int loop = 0;loop < MAX_PARTICLES;loop++) { particle[loop].active = true; particle[loop].life = 1.0f; //give the fade a random value particle[loop].fade = float (rand()%500)/5000.0f+0.01f; //set the position of the particles particle[loop].Pos = Position; //give a positive or negative speed to the particle particle[loop].Speed.x = float((randf(0.0f,speed.x))-speed.x/2) ; particle[loop].Speed.y = float((randf(0.0f,speed.y))-speed.y/2) ; particle[loop].Speed.z = float((randf(0.0f,speed.z))-speed.z/2) ; //Set the force off each particle particle[loop].Force = force; //set the colour of the particles particle[loop].r = r; particle[loop].g = g; particle[loop].b = b; } }//end InitParticles void DrawParticles(); }; void Particle::DrawParticles() { glEnable(GL_TEXTURE_2D); glEnable(GL_BLEND); glDisable(GL_DEPTH_TEST); glBindTexture(GL_TEXTURE_2D,fireTex.texID); glPushMatrix(); //Use a loop to upadate each of the particles for(int loop = 0;loop < MAX_PARTICLES;loop++) { if(particle[loop].active) { //asign temporary variables to coordinates of particle Vector temp = particle[loop].Pos; //Draw the particle and fade it using the life property glColor4f(particle[loop].r,particle[loop].g,particle[loop].b,particle[loop].life); glBegin(GL_TRIANGLE_STRIP); glNormal3f(0.0,1.0,0.0); glTexCoord2d(1,1);glVertex3f(temp.x+0.05f,temp.y+0.05f,temp.z); glTexCoord2d(0,1);glVertex3f(temp.x-0.05f,temp.y+0.05f,temp.z); glTexCoord2d(1,0);glVertex3f(temp.x+0.05f,temp.y-0.05f,temp.z); glTexCoord2d(0,0);glVertex3f(temp.x-0.05f,temp.y-0.05f,temp.z); glEnd(); //Now Move the Particle particle[loop].Pos.x += particle[loop].Speed.x/(SLOWDOWN * 3000); particle[loop].Pos.y += particle[loop].Speed.y/(SLOWDOWN * 1200); particle[loop].Pos.z += particle[loop].Speed.z/(SLOWDOWN * 1000); //Apply force particle[loop].Speed += particle[loop].Force; particle[loop].life -= particle[loop].fade; //fade the particle if(particle[loop].life < 0.0f) { particle[loop].active = false; } }//end if }//end for glEnable(GL_DEPTH_TEST); glPopMatrix(); }//end DrawParticles I declare the particle object globally Particle *Fire; Then define it upon a collision if(OnCollision(playShot->x,playShot->y,playShot->width,playShot->height, borg1->x, borg1[i]->y, borg1[i]->width, borg1[i]->height)&&playShot->state ==1&&borg1[i]->state==1) { Fire = new Particle(speed,pos,force,1.0,0.0,0.0); } and Render outside the initialisation Fire->DrawParticles(); Thanks again!!!!!

Share this post


Link to post
Share on other sites
An exception was raised (something didn''t work), and you didn''t have a means for "handling" it. As you have no exception handling code here, this is not suprising. Access violation means pretty much Memory Access Violation, you attempted to access memory you didn''t own. In this case, its almost certainly an array bounds violation, but I haven''t really studied your code all that hard. I did not see that you initialized your particle array at any point. This is most likely the problem. You did declare it particle[max_particle], but you also need to loop through it and initialize it before trying to use it.

Share this post


Link to post
Share on other sites
Thanks for the reply. I have tried everything but still no joy. Could anyone give me the solution as Im pulling my hair out!!

Thanks

Share this post


Link to post
Share on other sites
run your program through your debugger. it will break at the exact line that is causing the problem. then you fix that line and then your program works.

-me

Share this post


Link to post
Share on other sites
quote:
Original post by knealeaj


if(OnCollision(playShot->x,playShot->y,playShot->width,playShot->height,
borg1->x, borg1->y, borg1[i]->width, borg1[i]->height)&&playShot->state ==1&&borg1[i]->state==1)
{
Fire = new Particle(speed,pos,force,1.0,0.0,0.0);
}

Fire->DrawParticles();



Looks to me like OnCollision() is returning false, and therefore the Fire object is no initialized. Consider doing this instead:


bool collided;

if( collided = OnCollision(playShot->x,playShot->y,playShot->width,playShot->height,
borg1->x, borg1[i]->y, borg1[i]->width, borg1[i]->height)&&playShot->state ==1&&borg1[i]->state==1)
{
Fire = new Particle(speed,pos,force,1.0,0.0,0.0);
}

//Then later on...

if( collided )
Fire->DrawParticles();

Share this post


Link to post
Share on other sites
BTW, don''t paste that code directly into the editor. This forum software is screwing with the \[\i\] tags. Before you do anything else, make sure that the parts of the code that use arrays with the i variable are coming through correctly.

Share this post


Link to post
Share on other sites
quote:
Original post by _the_phantom_
if you put your code in a [ source ][ /source ] block (without the spaces) it shouldnt muck up like that (and makes it easier for others to understand the code)



int main( int argc, char * argv[] ) {
printf("Thanks for the great tip, _the_phantom!\n");

return 0;
}


;-)

Share this post


Link to post
Share on other sites