Jump to content
  • Advertisement
Sign in to follow this  
Dragon_Strike

abstract class and std::vector *solved

This topic is 3991 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

im using an abstract class to handle all OnLostDevice and OnResetDevice for DirectX object wrappers... and it works great... just not when i put these objects into a vector... i get some iterator error... any ideas what might be causing this? the abstract class the objects are derivred from looks like this...

#ifndef _DXRESOURCE
#define _DXRESOURCE

#include <list>

namespace gpu
{	
	class CResource
	{

	private:

		static std::list<CResource*> m_lResourceList;

		std::list<CResource*>::iterator m_pIt;		

	public:

		CResource()
		{				
			m_lResourceList.push_back(this);
			m_pIt = m_lResourceList.end();
			m_pIt--;				
		}

		virtual ~CResource()
		{
			if (m_lResourceList.size() > 0)
				m_lResourceList.erase(m_pIt);					
		}			

		static const std::list<CResource*>* GetResourceList()
		{
			return &m_lResourceList;
		}

		virtual void OnLostDevice() = 0;
		virtual void OnResetDevice() = 0;
	};

	void OnLostDevice();
	void OnResetDevice();

}

#endif



#include "DXUT.h"
#include "Resource.h"

using namespace gpu;

std::list<CResource*> CResource::m_lResourceList = std::list<CResource*>();

void gpu::OnLostDevice()
{		
	std::list<CResource*>::const_iterator it = CResource::GetResourceList()->begin();

	for (UINT n = 0; n < CResource::GetResourceList()->size(); n++)
	{
		(*it++)->OnLostDevice();
	}	
}

void gpu::OnResetDevice()
{	
	std::list<CResource*>::const_iterator it = CResource::GetResourceList()->begin();

	for (UINT n = 0; n < CResource::GetResourceList()->size(); n++)
	{
		(*it++)->OnResetDevice();
	}	
}




EDIT:: the error i get is "erase operator outside of range" [Edited by - Dragon_Strike on January 10, 2008 4:32:11 AM]

Share this post


Link to post
Share on other sites
Advertisement
First, I don't see std::vector appearing in your code. Did you mean std::list?

Second, storing m_pIt like that is asking for trouble, and you also don't need it. You are only using it in the destructor for CResource to remove the last element, but you can just do this:


virtual ~CResource() {
m_lResourceList.pop_back();
}

Share this post


Link to post
Share on other sites
Your method of iterating through the list is incorrect. The way you currently do it, the first element will never be accessed, and you'll walk off the end of the list.

Why not try this:

for(std::list<CResource*>::const_iterator it = CResource::GetResourceList()->begin(); it != CResource::GetResourceList()->end(); it++)
{
(*it)->OnLostDevice();
}




EDIT: Sorry, brainfart. Got my increment operators mixed up.

Share this post


Link to post
Share on other sites
Quote:
Original post by Gage64
First, I don't see std::vector appearing in your code. Did you mean std::list?

Second, storing m_pIt like that is asking for trouble, and you also don't need it. You are only using it in the destructor for CResource to remove the last element, but you can just do this:

*** Source Snippet Removed ***


pop_back wont work since items will not always be removed in the same order they were created..

EDIT:: i could do it like this... but it gives the the same results...

m_lResourceList.erase(find(m_lResourceList.begin(), m_lResourceList.end(), this));
EDIT2:: i think this solved it... still testing though

std::list<CResource*>::iterator it = find(m_lResourceList.begin(), m_lResourceList.end(), this);

if (it != m_lResourceList.end())
m_lResourceList.erase(it);

Share this post


Link to post
Share on other sites
Quote:
pop_back wont work since items will not always be removed in the same order they were created..


Of course, you're right. Not sure what I was thinking.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

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

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!