Jump to content
  • Advertisement
Sign in to follow this  
LordFallout

STL map problem with structure containing pointer

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

Ok, im not new to STL or maps i've just never had a problem with them, the main thing is i've got a class that manages things, it has a map of UID's to Resources. The problem is that my Resources are structures and one of the members is a pointer. so when im using my map, i allocate a variable and assign it to this pointer, now when the program leaves the creation function the resource i allocated gets destroyed, which causes the pointer in the map to become invalid, how do i get around this? Here is a bit of code as an explanation.
	// Maps UID's to Textures
	map<int, sQMResource<CTexture>>	m_TextureList;

	template <class QMResourceT>
	struct sQMResource
	{
		string		sFilename;
		int			iReferences;
		QMResourceT	*qmResource;
	};

	CTexture* tmpText = new CTexture();
	tmpText->CreateTexture(m_pd3ddev, sFilename);

	m_TextureList[m_iNextTexture].iReferences = 1;
	m_TextureList[m_iNextTexture].qmResource = tmpText;
	m_iNextTexture++;

Share this post


Link to post
Share on other sites
Advertisement
Edit: Oops, I was assuming that sQMResource had a destructor in which your were deleting the QMResourceT. Without this, jyk is correct.

Also,
Quote:

map<int, sQMResource<CTexture>> m_TextureList;


This is an error. Nested templates must have spaces between brackets, so as to not get confused with shifting operators:
map<int, sQMResource<CTexture> > m_TextureList;

Share this post


Link to post
Share on other sites
Quote:
Original post by LordFallout
Ok, im not new to STL or maps i've just never had a problem with them, the main thing is i've got a class that manages things, it has a map of UID's to Resources. The problem is that my Resources are structures and one of the members is a pointer. so when im using my map, i allocate a variable and assign it to this pointer, now when the program leaves the creation function the resource i allocated gets destroyed, which causes the pointer in the map to become invalid, how do i get around this?
If you're talking about the resource pointed to by tmpText, it's not destroyed when the 'create' function exits, so the pointer should still be valid.

Also, have you considered using a smart (shared/reference-counted) pointer instead of a raw pointer? This would most likely obviate the need for your iReferences variable, and might also address the ownership/lifetime issues you seem to be dealing with.

Share this post


Link to post
Share on other sites
Quote:
Original post by jyk
If you're talking about the resource pointed to by tmpText, it's not destroyed when the 'create' function exits, so the pointer should still be valid.

Also, have you considered using a smart (shared/reference-counted) pointer instead of a raw pointer? This would most likely obviate the need for your iReferences variable, and might also address the ownership/lifetime issues you seem to be dealing with.


Thats what i thought but for some reason the destructor is call, and so the data is invalidated, which is quite annoying because i thought it should work.

I have not considered Smart pointers but i might as, i am trying to deal with multiple objects referencing one pointer that could get deleted, is there much overhead with smart pointers? will they slow down work much or is it trivial.

One other question, i want the user to be able to access the resource with a UID, so my map uses ints against resources, however this means when a user tries to load a resource from file i have to serach through the entire map to see if the filename exists, is there a more efficient way to do this?

Quote:
Original post by Driv3MeFar
Quote:

map<int, sQMResource<CTexture>> m_TextureList;


This is an error. Nested templates must have spaces between brackets, so as to not get confused with shifting operators:
map<int, sQMResource<CTexture> > m_TextureList;


Thank you for pointing this out i had no idea.

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!