Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

sirSolarius

Memory access error

This topic is 5282 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I''m getting an error when I call MemoryManager::checkMemory (see below) my program: "Unhandled exception in (project name): 0xC00000005: access violation." The code that the debugger breaks to is for stl list, and is the following:
iterator begin()
		{return (iterator(_Acc::_Next(_Head))); }
The starred region below indicates where the above code is called. I''m not sure what''s going on... but here''s the problem source:
class MemObj;

#include <list>

class MemoryManager  
{
protected:
	MemoryManager();
	~MemoryManager();
	std::list<MemObj*> m_deadObjects;
	static MemoryManager *instance;

public:
	static MemoryManager *getInstance();
	void checkMemory();

	void markForDeletion(MemObj *m);

	void Destroy();
};
#include "MemoryManager.h"

MemoryManager *MemoryManager::instance;
//////////////////////////////////////////////////////////////////////

// Construction/Destruction

//////////////////////////////////////////////////////////////////////


MemoryManager::MemoryManager()
{
}

MemoryManager::~MemoryManager()
{
}

//////////////////////////////////////////////////////////////////////////////////////////

// Name: getInstance

// Desc: Gets an instance of the singleton

// Notes:

// ToFix:

//////////////////////////////////////////////////////////////////////////////////////////

MemoryManager *MemoryManager::getInstance()
{
	if(!instance)
		instance=new MemoryManager();
	return instance;
}

//////////////////////////////////////////////////////////////////////////////////////////

// Name: CheckMemory

// Desc: Iterates through the list of dead objects and deletes them

// Notes:

// ToFix:

//////////////////////////////////////////////////////////////////////////////////////////

void MemoryManager::checkMemory()
{
	while(!m_deadObjects.empty())	// while there are dead objects

	{
		delete m_deadObjects.front();	// ****ERROR IS HERE********

		m_deadObjects.pop_front();	// and remove it from the list

	}
}

//////////////////////////////////////////////////////////////////////////////////////////

// Name: markForDeletion

// Desc: Adds the MemObj to the deletion queue when nothing points to it

// Notes:

// ToFix:

//////////////////////////////////////////////////////////////////////////////////////////

void MemoryManager::markForDeletion(MemObj *m)
{
	m_deadObjects.push_back(m);
}

//////////////////////////////////////////////////////////////////////////////////////////

// Name: Destroy

// Desc: Cleans up the memory created by, ironically, the memory manager

// Notes:

// ToFix:

//////////////////////////////////////////////////////////////////////////////////////////

void MemoryManager::Destroy()
{
	checkMemory();
	delete instance;
	instance=0;
}
class MemObj
{
protected:
	int m_refs;

public:
	MemObj()
	:m_refs(0)
	{
	}

	void attach()
	{
		++m_refs;
	}
	void release()
	{
		--m_refs;
		if(m_refs==0)		// nothing points to the object

		{
			MemoryManager::getInstance()->markForDeletion(this);
		}
	}
};
Thanks!

Share this post


Link to post
Share on other sites
Advertisement
delete m_deadObjects.front();// ****ERROR IS HERE********
m_deadObjects.pop_front();// and remove it from the list

Hmmm... To my mind error could be in the second line either.
Check if you try to access NULL somehow or smth like that.

Share this post


Link to post
Share on other sites
Ahh, I found the problem... the contents were getting deleted by something buried in my code that I just completely forgot about!

Sorry about that!

Share this post


Link to post
Share on other sites
You''re never initializing the value of the static MemoryManager::instance variable. Since you''re probably compiling a debug build, the compiler initializes it to NULL for you, but if you compile a release build, your program will crash the first time you try to do anything with it. Add the following line to the top of MemoryManager.cpp:

MemoryManager* MemoryManager::instance = NULL;

Also, to get rid of the if statement in the getInstance() function and speed things up a bit, you should make a separate initInstance() method:

void MemoryManager::initInstance(void) // public method
{
instance = new MemoryManager;
}


The reason to have a separate function instead of simply doing:

MemoryManager* MemoryManager::instance = new MemoryManager;

is because the order of construction of global objects is undefined and can vary. By having an initInstance() method, you can guarantee that all global objects will be created in the same order during your program''s initialization.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!