7 Replies - 2615 Views - Last Post: 03 June 2008 - 08:57 AM Rate Topic: -----

#1 skater_00  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 12
  • View blog
  • Posts: 256
  • Joined: 30-April 08

Function causing runtime stack overflow

Post icon  Posted 02 June 2008 - 02:39 PM

My function timeGetTime() gives me the following warning after compilation:

warning C4717: 'TimeGravity::timeGetTime' : recursive on all control paths, function will cause runtime stack overflow

When I run the application, it crashes with error:

Unhandled exception at 0x00430339 in TimeGravity.exe: 0xC00000FD: Stack overflow.

DWORD TimeGravity::timeGetTime()
{
	DWORD CurrentTime = timeGetTime();

	double dTime = (CurrentTime - m_OldTime) / 1000.0f;

	if (dTime > 0.5)
		dTime = 0.02;

	m_OldTime = CurrentTime;

	m_BalPtr->Tick(dTime);

	return m_OldTime;
}



No idea why it is giving me this warning or what might have gone wrong.

This post has been edited by skater_00: 02 June 2008 - 02:39 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Function causing runtime stack overflow

#2 bodom658  Icon User is offline

  • Villiage Idiom
  • member icon

Reputation: 113
  • View blog
  • Posts: 1,123
  • Joined: 22-February 08

Re: Function causing runtime stack overflow

Posted 02 June 2008 - 08:48 PM

I dont know a lot about C++ and Classes, but it seems that your function is calling itself every time it is utilized, and it is being called so much that you are running out of memory for the program to run on. If you were to post the whole code, or at least the class and your main function, I may be able to help further
Was This Post Helpful? 0
  • +
  • -

#3 skater_00  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 12
  • View blog
  • Posts: 256
  • Joined: 30-April 08

Re: Function causing runtime stack overflow

Posted 03 June 2008 - 04:54 AM

Bal.h

#pragma once

#include "GameEngine.h"

class Bal
{
public:
	Bal();
	virtual ~Bal();

	void Tick(double);
	void Paint();

private:
	double m_PosX, m_PosY, m_SpeedX, m_SpeedY;
	int m_Straal, m_Height, m_Width;

	Bal(const Bal& s);									
	Bal& operator=(const Bal& s);	
};



Bal.cpp

#include "Bal.h"

#define GAME_ENGINE (GameEngine::GetSingleton())

Bal::Bal():m_PosX(20), m_PosY(150), m_SpeedX(10), m_SpeedY(10), m_Straal(20), m_Height(GAME_ENGINE->GetHeight()), m_Width(GAME_ENGINE->GetWidth())
{
	
}

Bal::~Bal()
{
	
}

void Bal::Tick(double dTime)
{
	m_PosX += m_SpeedX;
	m_PosY += m_SpeedY;

	if (m_PosX + m_Straal >= m_Width) {
		double dDiepteBotsing = m_Straal - (m_Width - m_PosX);
		m_PosX -= 2 * dDiepteBotsing;
		m_SpeedX *= -1;
	}

	if (m_PosX - m_Straal <= (m_Width - m_Width)) {
		double dDiepteBotsing = m_Straal - ((m_Width - m_Width) + m_PosX);
		m_PosX -= 2 * dDiepteBotsing;
		m_SpeedX *= -1;
	}

	if (m_PosY + m_Straal >= m_Height) {
		double dDiepteBotsing = m_Straal - (m_Height - m_PosY);
		m_PosY -= 2 * dDiepteBotsing;
		m_SpeedY *= -1;
	}

	if (m_PosY - m_Straal <= (m_Height - m_Height)) {
		double dDiepteBotsing = m_Straal - ((m_Height - m_Height) + m_PosY);
		m_PosY -= 2 * dDiepteBotsing;
		m_SpeedY *= -1;
	}
}

void Bal::Paint()
{
	GAME_ENGINE->SetColor(RGB(255, 255, 0));
	GAME_ENGINE->FillOval((int) m_PosX - m_Straal, (int) m_PosY - m_Straal, m_Straal * 2, m_Straal * 2);
}



TimeGravity.h

#pragma once
	
#include "GameEngine.h"
#include "AbstractGame.h"
#include "Bal.h"

class TimeGravity : public AbstractGame
{
public:				
	TimeGravity();
	virtual ~TimeGravity();

	void GameInitialize(HINSTANCE hInstance);
	void GameCycle(RECT rect);

	DWORD timeGetTime();

private:
	Bal * m_BalPtr;
	DWORD m_OldTime;

	TimeGravity(const TimeGravity& t);
	TimeGravity& operator=(const TimeGravity& t);
};



TimeGravity.cpp

#include "TimeGravity.h"																				

#define GAME_ENGINE (GameEngine::GetSingleton())

TimeGravity::TimeGravity():m_BalPtr(0), m_OldTime(0)																					
{
	m_BalPtr = new Bal();
}

TimeGravity::~TimeGravity()																						
{
	delete m_BalPtr;	
}

void TimeGravity::GameInitialize(HINSTANCE hInstance)			
{
	AbstractGame::GameInitialize(hInstance);
	GAME_ENGINE->SetTitle("TimeGravity");
	GAME_ENGINE->RunGameLoop(true);
	GAME_ENGINE->SetFrameRate(50);
}

void TimeGravity::GameCycle(RECT rect)
{
	timeGetTime();
	m_BalPtr->Paint();
}

DWORD TimeGravity::timeGetTime()
{
	DWORD CurrentTime = timeGetTime();

	double dTime = (CurrentTime - m_OldTime) / 1000.0f;

	if (dTime > 0.5)
		dTime = 0.02;

	m_OldTime = CurrentTime;

	m_BalPtr->Tick(dTime);

	return m_OldTime;
}



These are the source files.
Was This Post Helpful? 0
  • +
  • -

#4 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5874
  • View blog
  • Posts: 12,754
  • Joined: 16-October 07

Re: Function causing runtime stack overflow

Posted 03 June 2008 - 05:47 AM

First response is correct. Looks like an infinite loop

DWORD TimeGravity::timeGetTime()  {  
   // now, call self
   DWORD CurrentTime = timeGetTime();
   // we will never get here
   // and keep calling self until we run out of resources.


Was This Post Helpful? 0
  • +
  • -

#5 skater_00  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 12
  • View blog
  • Posts: 256
  • Joined: 30-April 08

Re: Function causing runtime stack overflow

Posted 03 June 2008 - 07:16 AM

Weird, because the timeGetTime() function is the only function that was written by the tutor. And I can't tell him he needs to learn writing functions...
Was This Post Helpful? 0
  • +
  • -

#6 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3106
  • View blog
  • Posts: 19,145
  • Joined: 14-September 07

Re: Function causing runtime stack overflow

Posted 03 June 2008 - 07:41 AM

Yeah you can ;)
Was This Post Helpful? 0
  • +
  • -

#7 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5874
  • View blog
  • Posts: 12,754
  • Joined: 16-October 07

Re: Function causing runtime stack overflow

Posted 03 June 2008 - 07:52 AM

View Postskater_00, on 3 Jun, 2008 - 10:16 AM, said:

And I can't tell him he needs to learn writing functions...


You certainly can. I'd bet there's another getTime type method lurking around, perhaps belonging to a parent object, that needs an explicit reference. I'd just ask him what method in particular you're supposed to be calling there, because it seems to be calling itself and isn't that bad? Let him figure it out. ;)
Was This Post Helpful? 0
  • +
  • -

#8 skater_00  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 12
  • View blog
  • Posts: 256
  • Joined: 30-April 08

Re: Function causing runtime stack overflow

Posted 03 June 2008 - 08:57 AM

Ok, my bad. :P I guess this makes more sense:

void TimeGravity::GameCycle(RECT rect)
{
	DWORD CurrentTime = timeGetTime();

	double dTime = (CurrentTime - m_OldTime) / 1000.0f;

	if (dTime > 0.5)
		dTime = 0.02;

	m_OldTime = CurrentTime;

	m_BalPtr->Tick(dTime);
	m_BalPtr->Paint();
}

DWORD TimeGravity::timeGetTime(void)
{
	return m_OldTime;
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1