   free() issue on a particle system
  Hey there guys, i'm trying to build an opengl game for a school project, but i've come across an heap-related issue while building the particle system for it.when i try to remove a particle emmiter, which contains an array of particles and one of keyframes, it usually works, but will sometimes get an undefined error, the emmiter struct is like so:typedef struct StructEmmiter {//Directionbool Directional;cg::Vector3d MainDirection, RandomDirectionValue;double Power;//Gravitycg::Vector3d Gravity;bool UseGravity;//Propertiescg::Vector3d LastPosition;cg::Vector3d Position;bool Looping, Active, DestroyAfterUse;bool visible, Lighted;long int speed, speedm, Lspeedm;//Shapecg::Vector3d BoxSize;double SphereRadius;EMMITER_SHAPE Shape;//Alpha-BlendingALPHA_BLENDING Blending;//ParticleGLfloat color[4];double Size;long int LifeTime, ParticleLifeTime;GLuint texture;//internallong int id;StructEmmiter * next, * previous;long int MaxParticles, ActiveParticles,InactiveParticleID;Particle * particles;int nKeyframes;ParticleKeyframe * keyframes;long int time;Quad * quad;} Emmiter;and here is the removeEmmiter function:void ParticleManager::RemoveEmmiter(Emmiter * m) {Emmiter * aux;if (aux=m->previous) {//se B não for o primeiro corpoaux->next=m->next;//o corpo antes de B aponta para o seguinte} else {//se B for o primeiro corpofirstEmmiter=m->next;//torna o corpo a seguir a B o primeiro} if (aux=m->next) {//se B não for o ultimo corpoaux->previous=m->previous;//o corpo a seguir a B//aponta para o anterior} else {//se B for o ultimo corpolastEmmiter=m->previous;//o ultimo corpo passa a ser o anterior a B}nParticles-=m->ActiveParticles;free(m->keyframes);free(m->particles);free(m);nEmmiters--;}if i remove the frees it works fine. Please help.Oh the game's is a asteroids clone with a hovercraft istead of a spaceship, can you recommend me a way to simulate the hovercraft's trail on the water? my water is a plane with with alternating textures, which repeat.   
  Is m->keyframes allocated with malloc?   ;  How do you allocate the emitter?;  yeah m->keyframes and m->particles both. :S ty for ur quick reply   ;Emmiter * ParticleManager::AddEmmiter(long int maxParticles, int nKeyframes, GLuint texture,double size) {Emmiter * aux;aux=(Emmiter*)malloc(sizeof(Emmiter));//aloca aux//directionaux->Directional=false;aux->RandomDirectionValue=aux->MainDirection=cg::Vector3d(0,0,0);aux->Power=0;//Gravityaux->Gravity=cg::Vector3d(0,-10,0);aux->UseGravity=false;//Propertiesaux->LastPosition=aux->Position=cg::Vector3d(0,0,0);aux->Looping=true;aux->visible=true;aux->Active=false;aux->DestroyAfterUse=false;aux->speed=10;aux->Lighted=false;aux->speedm=aux->speed;aux->Lspeedm=0;aux->ParticleLifeTime=200;//Shapeaux->BoxSize=cg::Vector3d(3,3,3);aux->SphereRadius=6;aux->Shape=EMMITER_SHAPE_POINT;//Alpha-Blendingaux->Blending=ALPHA_BLENDING_ADDALPHA;//Particleaux->color[0]=aux->color[1]=aux->color[2]=aux->color[3]=1;aux->LifeTime=1000;aux->texture=texture;aux->Size=size;//internalaux->quad=InitQuad(texture,true,size);aux->quad->blending=aux->Blending;aux->MaxParticles=0;aux->ActiveParticles=0;aux->InactiveParticleID=0;SetEmmiterMaxParticles(aux,maxParticles);aux->nKeyframes=0;SetEmmiterKeyframeNumber(aux,nKeyframes);aux->time=0;aux->id=nEmmiters;aux->previous=lastEmmiter;//o corpo antes de aux é o último corpoaux->next=NULL;//aux não tem um corpo seguinteif (nEmmiters>0) {//se houverem mais corposlastEmmiter->next=aux;//o corpo a seguir ao ultimo é aux} else {//se não houverem mais corposfirstEmmiter=aux;//o primeiro corpo é aux}lastEmmiter=aux;//o ultimo corpo passa a ser auxnEmmiters++;return aux;} //DONEwith malloc... :PHEAP[template.exe]: HEAP: Free Heap block 60ecf60 modified at 60ecfe8 after it was freed[Edited by - quirinpa on June 8, 2010 1:13:47 AM];  Judging by the use of the scope resolution operator, this must be C++, except that you're not really using C++ properly.Basically in C++ you have almost no excuse not to use the 'new' and 'delete' keywords instead of malloc/free. I can't tell whether cg::Vector3d has a constructor that you're casuing not to be called or not, but this could be related to the problem and you should change it.There is also probably a problem in where you call RemoveEmmiter, and you will need to show the code for that.   ;  *phresnel re-iterates iMalc*Quote:Original post by iMalcJudging by the use of the scope resolution operator, this must be C++, except that you're not really using C++ properly.Basically in C++ you have almost no excuse not to use the 'new' and 'delete' keywords instead of malloc/free. I can't tell whether cg::Vector3d has a constructor that you're casuing not to be called or not, but this could be related to the problem and you should change it.There is also probably a problem in where you call RemoveEmmiter, and you will need to show the code for that.*and adds ...*that without knowing all the types in your struct, we can't decide whether you have a POD (plain old data) there or not, and hence must assume that malloc() yields undefined behaviour in your case.Further, in C++, using typedef struct SomeArbitraryName {} RealName; is not the proper way to declare a class-type. Just use struct RealName {};.   ;  Thank you iMarc, i am using the constructors for Vector3d, but im not doing anything about freeing it, should i? about the delete and new issues, i changed that now, same error. about realloc, i must copy all the data from an array right?Here goes the function where i call RemoveEmmitervoid ParticleManager::update(unsigned long elapsed_millis) {Emmiter * aux,*aux2;PRFparticles->start();nActiveEmmiters=0;int i=0;for (aux=firstEmmiter; aux!=NULL; aux=aux->next) {updateEmmiter(aux,elapsed_millis*TimeFactor);if (aux->DestroyAfterUse && (!aux->Active && !aux->ActiveParticles) ) {aux2=aux;aux=aux->previous;RemoveEmmiter(aux2);}else if (aux->Active) nActiveEmmiters++;i++;}PRFparticles->end();};  but it's not a class-type its a struct, i just dont wanna refer to it by struct realname. should i be doing it differently? i really gotta catch up to c++.. :P   ;Quote:Original post by quirinpabut it's not a class-type its a struct, structs and classes are identical in C++, except that struct-members are public by default. Further, you don't have to typedef them in C++, just use "class Foo {};" or "struct Bar {};".Quote:i just dont wanna refer to it by struct realname. Why not? How does not talking to your classes directly by their name but instead by (one of their) their nickname improve your code?   