Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old September 28th, 2006, 10:15 PM
jois.de.vivre@gmail.com
Guest
 
Posts: n/a
Default 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
Quote:
>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

  #2  
Old September 28th, 2006, 10:25 PM
Roland Pibinger
Guest
 
Posts: n/a
Default Re: STL clear() functions

On 28 Sep 2006 14:27:32 -0700, jois.de.vivre@gmail.com wrote:
Quote:
>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
  #3  
Old September 29th, 2006, 01:05 AM
Dave Steffen
Guest
 
Posts: n/a
Default Re: STL clear() functions

jois.de.vivre@gmail.com writes:
Quote:
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
>
Quote:
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.
Quote:
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
  #4  
Old September 29th, 2006, 04:15 AM
David Harmon
Guest
 
Posts: n/a
Default Re: STL clear() functions

On 28 Sep 2006 18:23:30 -0600 in comp.lang.c++, Dave Steffen
<dgsteffen@numerica.uswrote,
Quote:
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;
}

  #5  
Old September 29th, 2006, 04:25 PM
jois.de.vivre@gmail.com
Guest
 
Posts: n/a
Default Re: STL clear() functions

Dave Steffen wrote:
Quote:
jois.de.vivre@gmail.com writes:
>
Quote:
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
Quote:
>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!
Quote:
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.
>
Quote:
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
 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles