Stack Overflow(Help)

Started by
9 comments, last by snk_kid 19 years, 6 months ago
Hi ya! I have a tree and every node in this tree has two adjanced nodes; a 'next' node and a 'successors' node,the tree is realy huge but it eats only 2MB of memory.The problem is that when trying to free this node like:
virtual ~CNode()
{
delete successors;
delete next;

}
 i exit unexpectedly from my infinite loop.
I traced my destructor and i get a Stack Overflow [looksaround]

The main game loop is like:
1.Build Tree
2.FindBestMove
3.Make move
4.Free tree

I used my task manager to figure out if it's really a memory leak and it isn't .If in the begining i have 191MB of used memory
i get 193 after building the tree ,then after the deletion i get those 191 of used memory back,but i exit the loop ???

I realy need to fix this bug,this should be my first game although is not big deal it means a lot to me,breaking the ice[lol]


Thanks for your support!



Advertisement
A stack overflow means you recursed too deep, and so your tree itself is too deep.

It would be possible to request a bigger stack from the compiler...but generally this is frowned upon.


Basically, your destructor is causing too much call depth. Is there any way you can make the tree smaller? Or alternatively, try to allocate less on the stack.
SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.
Well, it could just be having too much shit on the stack. Try allocating onto the heap - use an auto_ptr instead of a local (actually, you should be using auto_ptrs in general - its much cleaner than delete which can be missed if an exception is thrown).
-- Single player is masturbation.
The tree is indeed big ,3 levels of recursion and more then 22 thousand nodes,but it's a chess game it should be big ,and this is a small one[lol].I used recursion to find my best(at the same deapth) move why couldn't i use it to destroy it as well?

ThankX for your answers!
Just a thought, you do realize that calling delete on one item in your tree deletes all the others, and then crashes when it reaches the beginning or end of the tree, trying to delete a successor that doesn't exist.
so, as soon as you delete one node, the whole tree is deleted, and, of course, your loop exits.

I am not quite sure of the structure of your tree, but perhaps something like this would be more appropriate:
virtual ~CNode(){if (next) next->successors = successors;if (successors) successors->next = next;}

aka double-linked list.

but even if it is not a linked list, I don't think you should be deleting next in your destructor.


SwiftCoder

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

Is it possible for all my awake destructor instances on the stack crash into one another?(some kind of overwrite;thread taking the place of another thread).I even tried to declare a big array in my destructor to avoid the crash ,just in case but it doesn't work (as expected).
What does it mean if a variable needs a stack frame?This is not related with the folowing(or i think it doesn't)
I found out that it exits with code 128.This means:
There are no child processes to wait for???????????[looksaround]
Does this ring any bell,i have no clue.

Please help me out!



Thanks a lot!
Quote:Original post by swiftcoder
Just a thought, you do realize that calling delete on one item in your tree deletes all the others, and then crashes when it reaches the beginning or end of the tree, trying to delete a successor that doesn't exist.


It won't crash if the pointers are correctly initialized to null. You know deleting null pointers is valid, right?
SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.
Well on the stack there's only the hidden passed argument,the 'this' pointer right ,so the destructor doesn't put too many things on the stack, it's because the tree is to big
Well i fixed the stack overflow exception but now i get an access violation error

void CGameTree::DestroyTree(CNode* node){if(!node)return;CNode** pp=&node;CNode*aux=NULL;_try{	while(*pp)	{	   	       DestroyTree((*pp)->successors);      aux=*pp;		if((*pp)->next)	pp=&(*pp)->next;    	delete aux;	}}_except(EXCEPTION_EXECUTE_HANDLER){ printf("Exception handler:%lx\n",exception_code());}}



Can you spot any error???[looksaround]

I appreciate your help!

This topic is closed to new replies.

Advertisement