J
jois.de.vivre
I'm having some unexpected behavior regarding clear() in the map
container. I have the following code:
/// --- begin code
#include <map>
#include <vector>
#include <iostream>
#include <boost/shared_ptr.hpp>
using namespace std;
class test
{
public:
~test()
{
cout << __FUNCTION__ << endl;
}
};
int main(int argc, char *argv[])
{
typedef boost::shared_ptr<test> pTest;
vector<pTest> vec;
map<int, vector<pTest> > m;
{
pTest t(new test); /* (1) */
for (int i=0; i<10; i++)
vec.push_back(t);
m[0] = vec;
/* t is destroyed here, releasing one ref. to (1) */
}
cout << "Clearing Map" << endl;
/* all other refs. to (1) should be destroyed here */
m.clear(); /* (2) */
cout << "Done" << endl;
return EXIT_SUCCESS;
}
//--- end code
each shared_ptr object. After (2) there should be no more references
to the "new test" at (1), so I am expecting to see the following
output:
Clearing Map
~test
Done
but instead I see:
Clearing Map
Done
~test
Am I understanding something incorrectly about how this is supposed to
work?
Thanks
container. I have the following code:
/// --- begin code
#include <map>
#include <vector>
#include <iostream>
#include <boost/shared_ptr.hpp>
using namespace std;
class test
{
public:
~test()
{
cout << __FUNCTION__ << endl;
}
};
int main(int argc, char *argv[])
{
typedef boost::shared_ptr<test> pTest;
vector<pTest> vec;
map<int, vector<pTest> > m;
{
pTest t(new test); /* (1) */
for (int i=0; i<10; i++)
vec.push_back(t);
m[0] = vec;
/* t is destroyed here, releasing one ref. to (1) */
}
cout << "Clearing Map" << endl;
/* all other refs. to (1) should be destroyed here */
m.clear(); /* (2) */
cout << "Done" << endl;
return EXIT_SUCCESS;
}
//--- end code
vector object it contains, which in turn should call the destructor ofFrom what I understand, map::clear() should call the destructor of each
each shared_ptr object. After (2) there should be no more references
to the "new test" at (1), so I am expecting to see the following
output:
Clearing Map
~test
Done
but instead I see:
Clearing Map
Done
~test
Am I understanding something incorrectly about how this is supposed to
work?
Thanks