c++ - Segfault when accessing non-null pointer? -


the code works fine in linux environment, in windows crashes 5-10 seconds after program starts. debugger points n->fired = true; problem?

void particlesystem::privprocessparticles(pnodeptr n, double frametime) {       while(n != null) {         n->fired = true;         if(!n->immortal)             n->life -= frametime; //decrement life         n->particle.applyaccel2d(frametime);          /* since oldest particles on            top of queue, if life zero, dequeue! */         if(n->life <= 0) {             if(head != null && !n->immortal) {                 pnodeptr curr;                 curr = head;                 head = head->next;                 delete curr;             }         }         n = n->next;     } } 

allocation:

void particlesystem::addparticle(double lifein, double x, double y, double angle,                                  double size, double force, bool immortalin) {     //increment particle count     count++;      //allocate     pnodeptr n = new particlenode;      //initialize     n->particle.createquad(size);     n->particle.settexture(texture);     n->particle.setpos2d(x, y);     n->particle.setrot2d(angle);     n->particle.settopspeed(topspeed);     n->particle.setvelocity(force);      n->life = lifein;     n->immortal=immortalin;     n->fired = false;     n->next = null;      //place     if (head == null)      {         head = n;         tail = n;         n->next = null;     } else {         tail->next = n;         tail = n;     } } 

node:

struct particlenode {         quad particle;         double life;         bool fired;         bool immortal;         particlenode* next; }; 

there's not enough information posted. however, here's 1 potential source of problem.

when privprocessparticles function performs iterations on n, can decide delete head element of list. possible @ moment when decides delete head, n same head? if so, deleting head turns n dangling pointer, leads disastrous consequences @ n = n->next.

add assert(curr != n) before delete curr , see whether assertion holds or fails.

anyway, starting value of n passed privprocessparticles caller? can chance happen same head?

p.s. also, out of curiosity, logic use decide whether perform deletion or not seems suggest decision made node n (you check n->life <= 0 , n->immortal). proceed delete head, not n... design?

p.p.s. nitpick: doing excessive n->next = null initializations in addparticle.


Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -