Stack Overflow

Started by
29 comments, last by vininim 17 years, 8 months ago
Quote:Original post by vininim
*** Source Snippet Removed ***

So you have recursion with a static array of 50000 itens, might be that. Might, I'm not bothering to check if it's called.
I'd say you're onto something there. That's nearly 200K for a single call of this function alone. It would be best to dynamically allocate that large array, or better yet, simply use a std::vector![cool]

To the OP, I'm not sure you understand that your int test[100000]; takes up space on the stack the moment that the function containing that definition is entered, NOT after the call to add3DResource. It does not matter where in the function you put that declaration as it always takes up space right from the beginning.
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms
Advertisement
Original post by starfleetrp
I am not sure what you mean by "deep recursion" but here is the singleton class
template<typename T>class Singleton{   static T* ms_singleton;   public:      Singleton()      {         assert(!ms_singleton);         //use a cunning trick to get the singleton pointing to the start of the whole, rather than         //the start of the Singleton part of the object         int offset = (int)(T*)1 - (int)(Singleton <T>*)(T*)1;         ms_singleton = (T*)((int)this + offset);      }      ~Singleton()      {         assert(ms_singleton);         ms_singleton=0;      }      static T& GetSingleton()      {         assert(ms_singleton);         return *ms_singleton;      }      static T* GetSingletonPtr()      {         assert(ms_singleton);         return ms_singleton;      }};template <typename T> T* Singleton <T>::ms_singleton = 0;


Your singleton class contains a static pointer to some object of type T. This pointer is initialised to 0. The functions GetSingleton() and GetSingletonPtr() return that pointer (or a reference to the object it points to).

In your application the constructor/desctructor of the singleton class is never called (i.e. no instance of the singleton class was created). This way the pointer will remain 0. (Did you really turn on assertions ?)

For more information about creating singletons read here:
http://www.nada.kth.se/cvap/abstracts/cvap246.html
http://www.codeproject.com/cpp/singletonrvs.asp
http://www.informit.com/guides/content.asp?g=cplusplus&seqNum=148&rl=1

I didnt really pay attention to the singleton from a tutorial. So I will look into inproving my singleton design by probably replacing it with a better/different one. Thanks for the link.

I will look into the the buffer[50000] or and see if that is causing the problem. These are the only 2 peices of code I used that was not mine (well I did change the 3dsLoader a lot, but the basics like buffer stayed the same) I will try to fix that. Well I guess I also used code from an image loader but hey.

I will post back with my results when I change the buffer arround!
Ok instead of re-writing all of my code for a quick check I changed the buffer from 50000 to 1000. It now works perfectly, well not exacly. I no longer would get an immedate buffer overflow. It will happen like after 5 frames, unless I comment out a line of code.

[source lang = "cpp"]void Entity::_updateBounds(){	if (!mNode)	{		Log::GetSingleton().addRecord("No Node Bound.  Tried to Entity::_updateBounds() on </b>" + mName + "</b>", LOG_ERROR);		return;	}	if (!mModel.isValid())		return;    //mOBB->UpdateBounds(mNode->getDerivedOrientation(), mNode->getDerivedScale(), mNode->getDerivedPosition());	mOBB->UpdateBounds();//(mNode->getDerivedPosition());}


Now as you can see mOBB->UpdateBounds() will work perfectly but as soon as I pass any parameter over to the function after about it being called a few times (I saw it overflowing after 4 frames). I am not sure why the heck it would be doing this.
mOBB is an OBB class which pretty much just contain the 8 corners of the bounds.
Currently UpdateBounds() does nothing. However as I said it will crash if I pass any parameters to it.

It is declaired like
void OBB::UpdateBounds() OR void OBB::UpdateBounds(Quaternion orientation, const Vector3& scale, const Vector3& position) which crashes
{
//Nothing in here
}

I have fixed the 3dsLoader from causing a buffer overflow, which I should have caught earlier! However this is a problem where I can see nothing wrong.
I know I am being a complete moron... I am sure of it as you haveal ready prooved with the buffer[50000].

I really appriciate all of the help that you are giving me!

[Edited by - starfleetrp on August 18, 2006 6:27:07 PM]
never allocate thousands of bytes of memory on the stack, that's just not what it is for.

The stack is for control variables, the heap is for data ... change:

int buffer[50000]

to use the heap and you will be fine.
Let's please take a closer look at how that 'buffer' is being used.

int buffer[50000] = {0};  // ...  switch (mCurrentChunk->mID) {    case DS_VERSION:							    mCurrentChunk->mBytesRead += fread(&buffer, 1, mCurrentChunk->mLength - mCurrentChunk->mBytesRead, mFile);


All the other usages seem to be like that as well.

0) Why are you using C I/O in C++?

1) Assuming you do need the data for some reason, don't use a static buffer like that. Also, why are you reading bytes (see parameter 2 value of 1 for fread; member function name 'mBytesRead') into an int array?

// Keeping the C I/O for nowint readChunk(FILE* f, vector<char>& result) {  return fread(&(result[0]), 1, result.size(), f);}


2) The frequent access of data members of mCurrentChunk suggests that we should be doing this in a Chunk member function instead:

void Chunk::read(FILE* f) {  vector<char> additional_data(mLength - mBytesRead);  mBytesRead += readChunk(f, additional_data);}


3) If you DON'T need the data (and as far as I can tell, you aren't using it), there are more elegant ways available to skip over it.
That's the Scott Bilas singleton from Game Programming Gems, right? That trickery in the Singleton constructor was written to overcome VC6's shortcomings, and IIRC it's no longer needed.

Usually I do my singletons like this:

template< class T >class Singleton{public:	Singleton()	{		assert(s_ptr == 0);		s_ptr = static_cast<T*>(this);	}	~Singleton()	{		assert(s_ptr != 0);		s_ptr = 0;	}	static T& Get()	{		return *s_ptr;	}	static const T& Get() const	{		return *s_ptr;	}private:	static T* s_ptr;};template< class T > T* Singleton<T>::s_ptr = 0;
daerid@gmail.com
Yes I am going to get rid of the buffer and already have im using one created on the heap until I can try to completely reprogram the class. PS, like I said before I got that code from a APRON tutorial, since it was the only one that would load my models.

However I have fixed that problem now the other problem is the one on just the 1st page 2nd last post. It is still causing a stack overflow however I am not sure why?
I have removed the buffer off of the stack for now as I have said many times and it is now on the heap however I am not getting an buffer overflow from a different source since I added a message box after the Create3dsResource just to see if it was completeing and it is. The problem im having I am currently tracking down since I now know what to look for. I will try to keep you informed!
I finally found the other probelem.
My first problem was the buffer was taking up too much space and I thank you all for helping me figure that out
My second problem was occuring because when I updated the derived information of the node it then tired to update the OBB. The OBB was requesting information from the Node getDerivedPosition etc. This information was then out of data because it was in the middle of the update so it tried to update again which called the update..... It was an infinite recursion that was causing my second problem!
I thank you all very much for you help!

This topic is closed to new replies.

Advertisement