該代碼在Linux環境下工作正常,但在Windows中,程序啓動後它會崩潰5-10秒。調試器指向n->fired = true;
作爲問題?訪問非空指針時發生Segfault?
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 the oldest particles will always be on
top of the queue, if life is zero, dequeue! */
if(n->life <= 0) {
if(head != NULL && !n->immortal) {
pNodePtr curr;
curr = head;
head = head->next;
delete curr;
}
}
n = n->next;
}
}
分配:
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;
}
}
節點:
struct particleNode {
Quad particle;
double life;
bool fired;
bool immortal;
particleNode* next;
};
僅僅因爲它不爲空,並不意味着它指向一個有效的對象。 – 2013-05-09 20:38:42
如何驗證對象是否是問題? – Graztok 2013-05-09 20:46:39
看看指針,看它是否像0xDEADBABE之類的東西。您要麼使用單位化,要麼使用懸空指針釋放,要麼可能損壞指針的內存。 – 2013-05-09 20:46:43