Jump to content
  • Advertisement
Sign in to follow this  
finalcut

Unhaldled exception Windows Message(aka PacMan dies)

This topic is 4333 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hello, i'm making a pac man game and was just fine until i reached the AI part .The buggy code is below.Program stops execution and the classic windows message appears.The thing is that this happens at total random times(at least this is how i see it :) ).So i started printfing in files output from pointers that would potentially lead to memory violation but the output seems to be ok.Another thing is that the program crashes immediatly when i run it on any other pc ,same platform.I'm quite frustated.Logically there must be a strike out error of mine,i just can't locate it.While not calling AI::progress works fine.Any help would be very helpful Thank you in advance // PacMan * pm is public. terrain in memeber of Class AI

struct node{
int f;
int g;
int h;
int parentx,parenty;
int x,y;
int GetF(){
    return f;
}
bool operator <(const node & b)const{
     return f<b.f;
     }
node(int _x,int _y):x(_x),y(_y){}
node(){}
};
struct coord {
       public:
       int x,y;
 bool operator < (const coord &b)const{
     
      return y*29+x<b.y*29+b.x;
      }      
      bool operator > (const coord &b)const{
      return x>b.x && y>b.y;
      }      
      bool operator == (const coord &b)const{
         
      return x==b.x && y==b.y;
      }      
     coord(int a,int b):x(a),y(b){
          
          }
};
typedef std::map <coord,node> list;

struct list_iter
{
	typedef list::value_type T;
	bool operator()(T const& a, T const & b)
	{
		return a.second < b.second;
	}
};

void AI::Progress(){
     int tx,ty,r,x=0,y=0,n1,ts;
     node * ntmp,*curr;
  coord * ctmp;
  FILE *fp;
     tx=ghost->tmp1;
     ty=ghost->tmp;
  list open;
  list close;
  list::iterator ii ,ii1;
 int n2=0;
  
  bool search=true;

    if(terrain[ty][tx]==-2 && ghost->counter ==0 ){
  
     tx=ghost->tmp1;
     ty=ghost->tmp;
    ntmp =new node(tx,ty);
    ntmp->parentx=-1;
    ntmp->parenty=-1;
    ntmp->f=0;
    ntmp->g=0;
 ctmp=new coord(tx,ty); 
    open[*ctmp]=*ntmp;
  
    while(search){
  
        ii=min_element(open.begin(),open.end(),list_iter());
       
         assert(ii!=open.end());
    
        
       
        assert(ii->first.x<26 &&ii->first.y<29);
        ctmp = new coord(ii->first.x,ii->first.y);    
        ntmp=new node(ii->second.x,ii->second.y);
        ntmp->f = ii->second.f;
        ntmp->g = ii->second.g;
        ntmp->parentx=ii->second.parentx;
        ntmp->parenty=ii->second.parenty;
        curr=ntmp;
        close[*ctmp]=*ntmp;
        if(ii->first.x==pm->mapX &&ii->first.y==pm->mapY){
                                 
                           
        search=false;
        break;
        }
        delete &ii->first;
        delete &ii->second;
         open.erase(ii);
   //Debug code check both lists not empty and valid content    
  fp=fopen("debug.txt","w");
  fprintf(fp,"OPEN \n \n");
  for(ii1=open.begin();ii1!=open.end();++ii1)
  fprintf(fp,"X1 %d Y1%d X2 %d Y2 %d PX %d PY %d F %d \n",ii1->first.x,ii1->first.y,ii1->second.x,ii1->second.y,ii1->second.parentx,ii1->second.parenty,ii1->second.f);
  fprintf(fp,"CLOSE\n\n");
  for(ii1=close.begin();ii1!=close.end();++ii1)
  fprintf(fp,"X1 %d Y1%d X2 %d Y2 %d PX %d PY %d F %d \n",ii1->first.x,ii1->first.y,ii1->second.x,ii1->second.y,ii1->second.parentx,ii1->second.parenty,ii1->second.f);
  fprintf(fp,"\n\n PM %d %d",pm->mapX,pm->mapY);
  fclose(fp);
    
    
  
 if(curr->x<25 && (n1=terrain[curr->y][curr->x+1])!=-1)
 check2(&open,&close,curr,n1,0,PacRight);
 if(curr->x>0 && (n1=terrain[curr->y][curr->x-1])!=-1)
  check2(&open,&close,curr,-n1,0,PacLeft);
  if(curr->y<28 && (n1=terrain[curr->y+1][curr->x])!=-1)
  check2(&open,&close,curr,0,n1,PacDown);
  if(curr->y>0 && (n1=terrain[curr->y-1][curr->x])!=-1)
  check2(&open,&close,curr,0,-n1,PacUp);

          
}

  n1=100;
 
   if(curr!=NULL){
              
     while(n1-- >0){
           ii=   close.find(coord(curr->parentx,curr->parenty));
           assert(ii!=close.end());
              if(ii->second.x==tx && ii->second.y ==ty){
                                 x=curr->x-tx;
                                 y=curr->y-ty;
                                 if(x >0){
                                 r=PacRight;
                                 x=1;
                                 }
                                 else if(x <0){
                                  r=PacLeft;
                                  x=-1;
                                  }
                                 else  if(y >0) {
                                 r=PacDown;
                                 y=1;
                                 }
                                 else if(y <0){
                                 r=PacUp;
                                 y=-1;
                                 }
                                 break;
              }
              curr=&ii->second;
           }
     }
//Debug code it reaches here succesfully and correct data
      fp=fopen("debug3.txt","w");
  fprintf(fp,"%d %d %d %d\n \n",curr->x,curr->y,tx,ty);
  fclose(fp);
              if(n1==0)allegro_message("Need More");
              for(ii=open.begin();ii!=open.end();++ii){
                                                       delete &ii->first;
                                                       delete &ii->second;
                                                    open.erase(ii);
                                                    }
              for(ii=close.begin();ii!=close.end();++ii){
                      delete &ii->first;
                      delete &ii->second;
                        close.erase(ii);
                  }
 
     
      ghost->mapY=ghost->tmp;
       ghost->mapX=ghost->tmp1;
  
     if(r==PacDown){if(ty<28){x=0;y=1;}}
     if(r==PacUp){if(ty>0){x=0;y=-1;}}
     if(r==PacLeft){if(tx>0){x=-1;y=0;}}
     if(r==PacRight){if(tx<25){x=1;y=0;}}
  
     
      ghost->counter=(terrain[ty+y][tx+x])*20;
      if(r==PacDown){frB->Start(ghost,frD1,i-1);}
     if(r==PacUp){frB->Start(ghost,frU1,i-1);}
     if(r==PacLeft){frB->Start(ghost,frL1,i-1);}
     if(r==PacRight){frB->Start(ghost,frR1,i-1);}
      if(y<0)
      ghost->tmp=ty-terrain[ty+y][tx];
      else if(y>0)
      ghost->tmp=ty+terrain[ty+y][tx];
      
      else if(x>0)
            ghost->tmp1=tx+terrain[ty][tx+x];
       else if(x<0)
            ghost->tmp1=tx-terrain[ty][tx+x];
//Debug code reaches here too
  fp=fopen("debug1.txt","w");
  fprintf(fp,"YES \n \n");
  fclose(fp);
}




[Edited by - finalcut on January 30, 2007 11:27:19 AM]

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by finalcut

delete &ii->first;
delete &ii->second;


This is an error. A map<coord,node> stores its own copies of the coords and nodes, not your pointers. You should create your coords and nodes that you insert on the stack instead of the heap and avoid the explicit memory management altogether. When you insert things into a map, the map allocates its own structures on the heap and handles the memory management itself.

You'd only need to handle deleting like that if you were using a map<coord*,node*>, but I'd recommend against doing that.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!