Sign in to follow this  

problem with static std::list pointer

This topic is 3636 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 probably have a pretty bad design but anyways... the problem is that i have a static std::list<Resource*>* which i cant delete when the program finishes... i get a memory error pointing on some iterator code...
#ifndef _DXRESOURCE
#define _DXRESOURCE

#include <list>
#include <algorithm>


namespace DX
{	
	class Resource
	{
		public:

			Resource()
			{				
				g_ResourceList->push_back(this);
			}

			~Resource()
			{
				g_ResourceList->erase(find(g_ResourceList->begin(), g_ResourceList->end(), this));
			}


			static std::list<Resource*>* g_ResourceList;

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

		void OnCreateDevice();
		void OnLostDevice();
		void OnResetDevice();
		void OnDestroyDevice();



}

#endif



#include "DXUT.h"
#include "DXResource.h"

using namespace DX;

std::list<Resource*>* Resource::g_ResourceList = NULL;

void DX::OnCreateDevice()
{
	if (Resource::g_ResourceList)
	{
		Resource::g_ResourceList->clear();
		
	}
	Resource::g_ResourceList = new std::list<Resource*>;
}

void DX::OnLostDevice()
{
	if (Resource::g_ResourceList && !Resource::g_ResourceList->empty())
	{
		std::list<Resource*>::iterator it = Resource::g_ResourceList->begin();

		for (int n = 0; n < Resource::g_ResourceList->size(); n++)
		{
			(*it)->OnLostDevice();
			it++;
		}
	}
}

void DX::OnResetDevice()
{	
	if (Resource::g_ResourceList && !Resource::g_ResourceList->empty())
	{
		std::list<Resource*>::iterator it = Resource::g_ResourceList->begin();

		for (int n = 0; n < Resource::g_ResourceList->size(); n++)
		{
			(*it)->OnResetDevice();
			it++;
		}
	}
}


void DX::OnDestroyDevice()
{
	if (Resource::g_ResourceList && !Resource::g_ResourceList->empty())
	{
		std::list<Resource*>::iterator it = Resource::g_ResourceList->begin();

		for (int n = 0; n < Resource::g_ResourceList->size(); n++)
		{
			(*it)->OnDestroyDevice();
			it++;
		}
	}	

         // After clearing the list... delete the list pointer

         delete Resource::g_ResourceList;
}



as it is i get memory leaks... how do i delete Resource::g_ResourceList? [Edited by - Dragon_Strike on January 4, 2008 4:37:18 PM]

Share this post


Link to post
Share on other sites
It doesn't look like you are deleting the static list before you are exiting the program. You will have to find a way to do that before the program ends. I am wondering if there is any reason why you have a static pointer to a list rather than just having a static list? It would have the same functionality that you would want. It would clean itself up at program end. And you wouldn't have to have if-statements everywhere to see if it has been allocated yet.

Share this post


Link to post
Share on other sites
ive thought of that... however i get linker errors when not using pointer...


1>Model.obj : error LNK2001: unresolved external symbol "public: static class std::list<class DX::Resource *,class std::allocator<class DX::Resource *> > DX::Resource::g_ResourceList" (?g_ResourceList@Resource@DX@@2V?$list@PAVResource@DX@@V?$allocator@PAVResource@DX@@@std@@@std@@A)
1>DXResource.obj : error LNK2001: unresolved external symbol "public: static class std::list<class DX::Resource *,class std::allocator<class DX::Resource *> > DX::Resource::g_ResourceList" (?g_ResourceList@Resource@DX@@2V?$list@PAVResource@DX@@V?$allocator@PAVResource@DX@@@std@@@std@@A)
1>Debug\DXEngine.exe : fatal error LNK1120: 1 unresolved externals

EDIT:: I edited the code above soo that u can see where the delete should be happening...

btw should the destructor for Resource be virtual? Im using it as an base class

when i try to delete the pointer to the list get a memory error and it breaks here in <list>

iterator begin()
{ // return iterator for beginning of mutable sequence
return (iterator(_Nextnode(_Myhead), this));
}

Share this post


Link to post
Share on other sites
nop... but now i a and it solved that problem..


however now i get a memory error again...

it occurs at g_ResourceList->begin() here:

~Resource()
{
g_ResourceList->erase(find(g_ResourceList->begin(), g_ResourceList->end(), this));
}

it seems like when the last Resource is about to be removed, g_ResourceList is removed first and then the destructor is called... which causes the error.. any solution?

EDIT:: Solved... i added it to check if the ResourceList.size() > 0 before trying to remove anything from it

[Edited by - Dragon_Strike on January 4, 2008 5:29:21 PM]

Share this post


Link to post
Share on other sites

This topic is 3636 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.

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