Jump to content
  • Advertisement

Archived

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

std::map newbie questions

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

Do I need to clean up a map that contains owning pointers? And how do I clean up a map? I havent really used assoc containers before plus i try and avoid hand coded "clean up" code example
  
std::map<std::string, affix_actor *> mod_list;
std::mod_list.insert(pair<std::string, affix_actor*>("add_st",new add_st));
//

//

//

  

"I turned into a driveway I thought was mine and crashed into a tree I don''t have."- a real insurance claim

Share this post


Link to post
Share on other sites
Advertisement
You need to call delete on every pointer.

The easy way:

  
typedef std::map<std::string, affix_actor *> mod_list_type;
mod_list_type mod_list;

...

mod_list_type::iterator itor, end = mod_list.end()
for( itor = mod_list.begin(); itor != end; ++itor )
{
delete itor->second;
itor->second = 0;
}


The elegant way:

  
#include <functional>


template<class MAP> struct PointerMapDeleter
: public std::unary_function<MAP::value_type&, bool>
{
result_type operator()( argument_type arg )
{
delete arg.second;
// VC6 barfs on void-returning functors so we return bool

return true;
}
};

template<class MAP> struct ArrayMapDeleter
: public std::unary_function<MAP::reference, bool>
{
result_type operator()( argument_type arg )
{
delete[] arg.second;
// VC6 barfs on void-returning functors so we return bool

return true;
}
};


typedef std::map<std::string, affix_actor *> mod_list_type;
mod_list_type mod_list;

...

std::for_each( mod_list.begin(), mod_list.end(),
PointerMapDeleter<mod_list_type>() );



The ideal way:

  
#include <boost/shared_ptr.hpp>


typedef boost::shared_ptr<affix_actor> affix_actor_sptr;
typedef std::map<std::string, affix_actor_sptr> mod_list_type;
mod_list_type mod_list;

std::mod_list.insert( pair<std::string, affix_actor_sptr>("add_st",affix_actor_sptr(new add_st)));

...

// Nothing needs to be done for cleanup.



Using the reference-counted shared pointer class in the Boost library (see signature).


[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]

Share this post


Link to post
Share on other sites
Thanks Fruny, that was fast and great advice.

I almost tried the "the easy way:" first on my own but my Intelliesense broke down and didn’t report begin and end as a part of map (should have tried it anyway since I suspected it was wrong); so I probably posted this question preemptively after only glancing through a STL book of mine and not going to some online docs.

But luckily, you had two better ways anyway.

Thanks.

edit: spelling/grammar


"I turned into a driveway I thought was mine and crashed into a tree I don't have."- A real insurance claim


[edited by - Amnesty on January 31, 2003 2:14:10 AM]

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.

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

Sign me up!