Stack overflow help needed

Started by
5 comments, last by ankhd 10 years, 1 month ago

Hello all.

Im getting a stack over flow in this line of code or I think it is.

DWORD GetOwnerPlayerID(void)

{

(OwnerObj == NULL)

return -1;

return OwnerObj->GetOwnerPlayerID();

}//no owner

if ownerobj is the same object would this be the cause, I think it is

but the codes been running for month and tonight it crashed on that line

I cant believe I did that hehe. and its been running since 29.1.13 is when I created the class lol

Advertisement

More info would be helpful. If OwnerObj is a member of the same class which implements GetOwnerPlayerID, then try this (simple cycle detection):


DWORD GetOwnerPlayerID(void)

{

 if (OwnerObj == NULL)

                return -1;


       if (OwnerObj == this) {
   printf("ERROR: cycle detected for object %s !", OwnerObj->getName());
}


       return OwnerObj->GetOwnerPlayerID();

}//no owner


Thought there could be non trivial cycles: A->B->C->A...

Why is this function recursive? What does it do, how is it called? we need more information than than. Obliously, the code is right, most probably, just just got stuck in calling that function in an infinite loop until it crashed, but without the rest of the relevent code we can't say much.

Apparently DWORD is unsigned, which would mean your return -1; ends up as some huge integer value.

Not sure if that is the problem though, but could be.

o3o

How deep is this ownership hierarchy? Perhaps the function isn't optimized for tail recursion and you overflow the stack.

Not sure exactly what you're trying to do, but you could probably rewrite it without using recursion:


Object* root = this;

while(root->OwnerObj) root = root->OwnerObj;

return(root->ID);

More info would be helpful. If OwnerObj is a member of the same class which implements GetOwnerPlayerID, then try this (simple cycle detection):


DWORD GetOwnerPlayerID(void)

{

 if (OwnerObj == NULL)

                return -1;


       if (OwnerObj == this) {
   printf("ERROR: cycle detected for object %s !", OwnerObj->getName());
}


       return OwnerObj->GetOwnerPlayerID();

}//no owner


Thought there could be non trivial cycles: A->B->C->A...

You can also do non-trivial cycle check by having two pointers that you trace:

You have Pointer A and Pointer B. For each step, advance Pointer A once, advance Pointer B twice.

If the advances are under same conditions, they will meet sooner or later if there is a cycle. Of course a bit more involving than the method Ashaman73 proposed, but may be a desperate last resort.

Using Ashaman73's example:

A->B->C->A

Step 1: Pointer A = A, Pointer B = B

Step 2: Pointer A = B, Pointer B = A

Step 3: Pointer A = C, Pointer B = C = Yes, there is a cycle.

this object is my explosion object and it has no owner I set it to the explosion object it self by mistake.

Im going to put if (OwnerObj == this) { printf("ERROR: cycle detected for object %s !", OwnerObj->getName()); in. turns out to be the case

one of the projectiles must of polled all cell objects and at the time it picked the explosion object to see if it was a enermy object and called it self over and over.

Im going to remove the explosion objects ID I'll return -1;

DWORD = -1; = max amount a DWORD can hold I do a bound check on the returned value like this

DWORD error = -1;

if(ID == error)

do error code; is this not a good Idea the ID will never get to this amount I only need about 8000 IDs thats 8000 units running around 1000 per player

I can't believe that it only buged out now after all this time thats been in the code and I ran it many times shooting and all.

Im just lucky VC pointed to the code that overflowed.

the code is like this

cExplosion *explosion = GetFreeExplosionObj();// this is a pool of explosion object pre allocated

if(explosion == NULL)

return false;//error none free

//see if we can get some particles

//add the explotion

UINT error = -1;

UINT CurrentExplosionElement = explosion->GetFreeParticleDataElement();

if(CurrentExplosionElement == error)

return false;//no particles to use maybe next time

explosion->InUse = true;//so we dont reuse it

explosion->m_vPos = worldpos;

explosion->OwnerObj = explosion;//bad on my part don't know what I was thinking at the time

and when a projectile is fired it checks the cells it travels though for object IDs and if there not allies it explodes and adds a explosion object.

Thanks everyone for the help.

This topic is closed to new replies.

Advertisement