Jump to content
  • Advertisement
Sign in to follow this  
cptrnet

does shared_ptr release memory?

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

Hi I have a vector of strings declared as this
std::vector< boost::shared_ptr<std::string> > m_Values;

When I put items into the vector I do this
boost::shared_ptr<std::string> value(new std::string(strValue));
m_Values.push_back(value);

Now when I do this, does it clean up the memory for me?

std::vector< boost::shared_ptr<std::string> >::iterator it = m_Values.begin();
std::vector< boost::shared_ptr<std::string> >::iterator oneToErase;

for(; it != m_Values.end(); ++it)
{
	if((*it) == m_Values[index])
	{
		oneToErase = it;
		break;
	}
}

m_Values.erase(oneToErase);


Share this post


Link to post
Share on other sites
Advertisement
Yeah, but that's kind of a weird way to do things. Does the vector own the strings? If so, why not just have it as a vector of strings?

Share this post


Link to post
Share on other sites
You understand that you can get a pointer to an object regardless of whether it was dynamically allocated, right?

Share this post


Link to post
Share on other sites
no I didnt know that
then how would I do this? I need it to be a pointer so the ListBox can have a value.




void CListBox::Add(std::string strItem, std::string strValue)
{
SendMessage(m_hControl, LB_ADDSTRING, NULL, (LPARAM)strItem.c_str());

boost::shared_ptr<std::string> value(new std::string(strValue));
m_Values.push_back(value);
int iSize = (int)(m_Values.size() - 1);

SendMessage(m_hControl, LB_SETITEMDATA, (WPARAM)iSize, (LPARAM)value->c_str());
}


Share this post


Link to post
Share on other sites
To get a pointer from an instance or reference, do something like the following:


int n = 5;
int* pn = &n;



Someone correct me if I'm wrong.

Share this post


Link to post
Share on other sites
Well I can't figure it out unless I use new std::strings. I guess ill just use it the way I got it to work.

Share this post


Link to post
Share on other sites
Quote:
Original post by cptrnet
Well I can't figure it out unless I use new std::strings. I guess ill just use it the way I got it to work.


Assuming you changed: std::vector< boost::shared_ptr< std::string > > m_Values;
to: std::vector< std::string > m_Values;

Your new CListBox::Add function would look like this:

void CListBox::Add(std::string strItem, std::string strValue)
{
SendMessage(m_hControl, LB_ADDSTRING, NULL, (LPARAM)strItem.c_str());

boost::shared_ptr<std::string> value(new std::string(strValue));
m_Values.push_back(value);
m_Values.push_back( strValue );
int iSize = (int)(m_Values.size() - 1);

SendMessage(m_hControl, LB_SETITEMDATA, (WPARAM)iSize, (LPARAM)value->c_str());
SendMessage(m_hControl, LB_SETITEMDATA, (WPARAM)iSize, (LPARAM)strValue.c_str());

//Assuming you actually needed a pointer as you mentioned earlier:
std::string * pointer_to_string_in_vector = &(m_Values[0]);
std::string * pointer_to_string_from_iterator = &*(m_Values.begin());
std::string * pointer_to_value = & strValue;

/* Note: the last pointer will be difference from the others because
* push_back()ing the value creates a new copy which is "owned" by the
* container.
*/
}

Share this post


Link to post
Share on other sites
Quote:
Original post by cptrnet
Just a bunch of garbage comes up with that.


It shouldn't... I presume you mean the listbox is filled with errornus characters?

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.

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!