该代码在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