473,387 Members | 1,590 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

STL clear() functions

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<testpTest;
vector<pTestvec;
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
>From what I understand, map::clear() should call the destructor of each
vector object it contains, which in turn should call the destructor of
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

Sep 28 '06 #1
4 5037
On 28 Sep 2006 14:27:32 -0700, jo***********@gmail.com wrote:
>From what I understand, map::clear() should call the destructor of each
vector object it contains, which in turn should call the destructor of
each shared_ptr object.
No, your shared_ptrs are deleted in the destructor of vec. Your
'smart' pointer has outsmarted you.

Best wishes,
Roland Pibinger
Sep 28 '06 #2
jo***********@gmail.com writes:
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<testpTest;
vector<pTestvec;
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
From what I understand, map::clear() should call the destructor of each
vector object it contains, which in turn should call the destructor of
each shared_ptr object. After (2) there should be no more references
to the "new test" at (1),
Here's where you're mistaken. You

A) Make a test and stick it in a smart pointer. Ref count, 1
B) Push_back that smart pointer onto a std::vector 10 times. Ref
count 11.
C) Stick that vector into a std::map. That's a copy operation, so
I'd expect the ref count to be 21. (I think this is what you missed.)
D) End of the code block. pTest goes out of scope, dropping the
ref count to 20.
D) Clear the map. That destroys the contained vector (a copy of
the one you manipulated earlier), dropping the ref count to 10.
E) Print "Done".
E) End of main(). The vector goes out of scope, dropping the ref
count to 0.
so I am expecting to see the following output:

Clearing Map
~test
Done
Try clearing the vector before printing "Done" and see what happens.

----------------------------------------------------------------------
Dave Steffen, Ph.D.
Software Engineer IV Disobey this command!
Numerica Corporation - Douglas Hofstadter
dgsteffen at numerica dot us
Sep 29 '06 #3
On 28 Sep 2006 18:23:30 -0600 in comp.lang.c++, Dave Steffen
<dg*******@numerica.uswrote,
Try clearing the vector before printing "Done" and see what happens.
Or even better, let it be destroyed naturally.

int main()
{
{
vector<pTestvec;
// other stuff goes here
}
cout << "Done\n";
return EXIT_SUCCESS;
}

Sep 29 '06 #4
Dave Steffen wrote:
jo***********@gmail.com writes:
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<testpTest;
vector<pTestvec;
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
>From what I understand, map::clear() should call the destructor of each
vector object it contains, which in turn should call the destructor of
each shared_ptr object. After (2) there should be no more references
to the "new test" at (1),

Here's where you're mistaken. You

A) Make a test and stick it in a smart pointer. Ref count, 1
B) Push_back that smart pointer onto a std::vector 10 times. Ref
count 11.
C) Stick that vector into a std::map. That's a copy operation, so
I'd expect the ref count to be 21. (I think this is what you missed.)
You're exactly right, this is what I missed. Thanks!
D) End of the code block. pTest goes out of scope, dropping the
ref count to 20.
D) Clear the map. That destroys the contained vector (a copy of
the one you manipulated earlier), dropping the ref count to 10.
E) Print "Done".
E) End of main(). The vector goes out of scope, dropping the ref
count to 0.
so I am expecting to see the following output:

Clearing Map
~test
Done

Try clearing the vector before printing "Done" and see what happens.

----------------------------------------------------------------------
Dave Steffen, Ph.D.
Software Engineer IV Disobey this command!
Numerica Corporation - Douglas Hofstadter
dgsteffen at numerica dot us
Sep 29 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: TaiwanNoWhere | last post by:
// case 1 int Option = 0; for(;;) { cin >> Option; switch(Option) { case 1:
5
by: Chris Mantoulidis | last post by:
Let's say I have this: std::string s1; std::cin >> s1; This will read s1 from cin until it finds a space (or a newline, whichever comes first). Okay this works. But when I want to continue...
3
by: steflhermitte | last post by:
Dear cpp-ians, I have a class: class mImage { public: //constructors / destructor mImage(unsigned int nrLayers); ~mImage();
14
by: Alan Silver | last post by:
Hello, I have spent ages trawling through Google, looking for information about global functions in ASP.NET and I'm still not clear about the best way to go about this (or not). I am writing...
0
by: Cleako | last post by:
Here is my dilema. I have a validation summary that I use soley for my Required Field Validators. I have it setup to run from a Page.Validate call in the code-behind, this is because I need to...
22
by: mp | last post by:
i have a python program which attempts to call 'cls' but fails: sh: line 1: cls: command not found i tried creating an alias from cls to clear in .profile, .cshrc, and /etc/profile, but none...
77
by: Ville Vainio | last post by:
I tried to clear a list today (which I do rather rarely, considering that just doing l = works most of the time) and was shocked, SHOCKED to notice that there is no clear() method. Dicts have it,...
16
by: Brian D | last post by:
I have a multiple select list that is created dynamically based on a previous selection on an asp page. The first thing I do is to clear the curent option list by ...
19
by: Krishanu Debnath | last post by:
Hello, I have a call to hash_map::clear() function which takes long time. someClass::someFunction() { // typedef hash_map<name_id, uintMp; // Mp p; // assuming proper namespace, hash...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.