Thread: Problem with Vector/Shared Pointer

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    673

    Question Problem with Vector/Shared Pointer

    I started using boost::shared_ptr to handle my pointer, then after applying it to my resource manager I realized I was losing a lot of performance in initialization, and shutdown was taking 10x as long to release my memory. So, I decided to make a test program to see what was happening, and I still can't figure it out. I am assuming it has something to do with the vector, but I am not sure.

    Here is the test program to test speed. On release build it allocates 79,000k in task manager in about 3 seconds, and it takes my computer about 2 minutes to free it.
    I am hoping someone can help me solve this issue. If not shared_ptr just isnt going to work for me.
    Code:
    template<class T>
    struct SPtr
    {
        typedef boost::shared_ptr<T> Type;
    };
    
    int main()
    {
        std::vector<SPtr<int>::Type> Test;
        Test.resize(1000000);
        std::vector<SPtr<int>::Type>::iterator it = Test.begin();
        int x = 0;
        for ( ; it != Test.end(); ++it )
        {
            (*it).reset(new int(x));
            ++x;
        }
        system("pause");
        return 0;
    }
    Thank you for any assistance.

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Don't know. It takes slightly less time to free it for me and both happens in less than a second.

    Did you also compare it against plain pointers?

    Also, testing it with the simplest built-in type seems not very realistic: in real life the things you'd allocate dynamically would be more complex.

    You might try profiling the code to see where it is slow.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    >>Also, testing it with the simplest built-in type seems not very realistic: in real life the things you'd allocate dynamically would be more complex.

    Maybe so, but if it takes forever allocating/de-allocating built in data types, then naturally it will take even longer to do so with larger datatypes.

    I am using VS C++ 9.0 sp1. Is there something with VS vector that makes it slow? ( such as debug stuff ) speed doesn't change from debug to release build.

    I tried it with raw pointers (no vector/shared_ptr) and it is instantaneous .023 seconds. So, it has to be the vector I am assuming. I get a heap corruption error when creating a dynamic array of shared_ptr's.

    I have been adding counters in my code that produce a message box with allocation/de-allocation times on different elements, and the speeds are ridiculous. 3 seconds to load 100 textures. approx. 10mb of data. Which with raw pointers is unnoticeable. ( This is not due to file streams. All data is contiguous in a single file. )
    Last edited by Raigne; 01-08-2009 at 03:11 AM. Reason: typo(s)

  4. #4
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    I believe there is bound checking for vectors in Visual C++. Don't know if this is not done in release mode. Don't know how to turn it off.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Raigne
    I tried it with raw pointers (no vector/shared_ptr) and it is instantaneous .023 seconds. So, it has to be the vector I am assuming. I get a heap corruption error when creating a dynamic array of shared_ptr's.
    The combination of shared_ptrs with vector might or might not be the problem, but boost::ptr_vector provides an alternative that might perform better.

    Quote Originally Posted by C_ntua
    I believe there is bound checking for vectors in Visual C++. Don't know if this is not done in release mode. Don't know how to turn it off.
    The bounds checking is enabled in the default release configuration as well, but can be disabled by setting _SECURE_SCL=0
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointer to pointer realloc problem
    By prakash0104 in forum C Programming
    Replies: 14
    Last Post: 04-06-2009, 08:53 PM
  2. Another pointer problem
    By mikahell in forum C++ Programming
    Replies: 21
    Last Post: 07-20-2006, 07:37 PM
  3. Pointer problem
    By mikahell in forum C++ Programming
    Replies: 5
    Last Post: 07-20-2006, 10:21 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. pointer problem
    By DMaxJ in forum C Programming
    Replies: 4
    Last Post: 06-11-2003, 12:14 PM