Sign in to follow this  
Extrakun

How did I cause a stack corrouption?

Recommended Posts

Hi, This class is giving me some headache...after I create an instance of it, and then tried to quit the application, I always get the exception "Stack is corrputed" at the ending brace of the main() function. I'm using VS C++ .NET 2005. The class in question is CSampleMenu, which is derived from GUIGroup. I believe that it has something to do with the destructor...I set it as virtual which is what many OOP books suggested...is there anything else I have done wrong? GUIGroup.h
#pragma once
#include "CEGUI_Includes.h"

class GUIGroup
{
private:
	virtual void setupEventHandlers() = 0; // bind events to controls

protected:	
	CEGUI::Window * GroupWindow_;	 // parent window of the UI group
	
public:
	virtual ~GUIGroup(void);		 // release all resources

	virtual void onLoad(void) = 0;	 // initalise the GUI and its controls
	virtual void onHide(void) = 0;	 // called when GUI group is switched for another
	virtual void onShow(void) = 0;   // called when the GUI group is showed

	void addTo( CEGUI::Window*&  dest); // add window holding all the UI to given window
	void load(void);				   // load the gui group
	void show(void);				   // display the window
	void hide(void);				   // hide the window

	// return the window of the group
	CEGUI::Window* GetWindow(void)
	{
		return GroupWindow_;
	
	}
};

GUIGroup.cpp
#include "GUIGroup.h"

// Release all acquired resources
GUIGroup::~GUIGroup(void)
{
	if (GroupWindow_)
	{
		delete GroupWindow_;
		GroupWindow_ = 0;
	}
}


void GUIGroup::addTo(CEGUI::Window*& dest)
{
	dest->addChildWindow(this->GroupWindow_);
}

// Load the GUI group
void GUIGroup::load()
{
	this->onLoad();
}

// Show the GUI group
void GUIGroup::show()
{
	onShow();
	this->GroupWindow_->show();
}

// Hide the GUI group
void GUIGroup::hide()
{
	this->GroupWindow_->hide();
	onHide();
}

CSampleMenu.h
#ifndef SAMPLE_MENU
#define SAMPLE_MENU

#include "GUIGroup.h"

/*
A sample GUIGroup to play with
*/
class CSampleMenu : public GUIGroup
{
public:
	
	~CSampleMenu(void);

	void onLoad(void);	   // initalise the GUI and its controls
	void onHide(void) {};  // called when GUI group is switched for another
	void onShow(void) {};  // called when the GUI group is showed

private:
	void setupEventHandlers() {};	
};

#endif

CSampleMenu.cpp
#include "SampleMenu.h"

void CSampleMenu::onLoad()
{
	// Load in the imagesets
	CEGUI::ImagesetManager::getSingletonPtr()->createImageset ("MenuButtons.imageset", "General");
	CEGUI::ImagesetManager::getSingletonPtr()->createImageset ("MenuBackground.imageset", "General");

	// Load in the layout
	GUIUtil::loadLayout("Menu.layout", &GroupWindow_ );	
}

Thanks in advance!

Share this post


Link to post
Share on other sites
Typical causes for stack corruption are infinite recursions and large local variables, for example, screen buffers declared as local variables. The default stack size on Windows is 1 mb. I don't see any of that in the sample code.

Share this post


Link to post
Share on other sites
You don't appear to be initialising your GroupWindow_ pointer in your constructor.

They don't automatically get intialised to 0.

Also, most of your destructor is redundant - you call replace it all with just delete GroupWindow_

Share this post


Link to post
Share on other sites
Quote:
Original post by LessBread
Typical causes for stack corruption are infinite recursions and large local variables, for example, screen buffers declared as local variables. The default stack size on Windows is 1 mb. I don't see any of that in the sample code.


memcpy overflow onto a stack address can do a nice job of it as well.

Quote:

void GUIGroup::addTo(CEGUI::Window*& dest)
Make that just a pointer. *& is for when you want to move the pointer as a side effect. You don't need it here since you aren't affecting the pointer itself; merely the memory it points to.

Quote:
This class is giving me some headache...after I create an instance of it, and then tried to quit the application, I always get the exception "Stack is corrputed" at the ending brace of the main() function. I'm using VS C++ .NET 2005.
Nitage is right that you don't initialise GroupWindow_ to anything. If this code is accurate, then your destructor is deleting whatever the hell memory it likes. When is the destructor called? I am going to guess that you have allocated the CSampleMenu on the stack (not used 'new'), so it's cleaned up when your function ends. That's why the debugger is pointing to the end brace.

Share this post


Link to post
Share on other sites
Quote:
Original post by flangazor
When is the destructor called? I am going to guess that you have allocated the CSampleMenu on the stack (not used 'new'), so it's cleaned up when your function ends. That's why the debugger is pointing to the end brace.


Hi flangazor! I initialise GroupWindow_ to 0 and it works well.

However, I was allocating CSampleMenu on the heap. Could the problem be caused by something else?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this