Why is std::tr1::shared_ptr<>.reset() so expensive?

Posted by Paul Oyster on Stack Overflow See other posts from Stack Overflow or by Paul Oyster
Published on 2010-06-15T13:11:15Z Indexed on 2010/06/15 13:12 UTC
Read the original article Hit count: 164

Profiling some code that heavily uses shared_ptrs, I discovered that reset() was surprisingly expensive.

For example:

struct Test {
    int i;
    Test() {
        this->i = 0;
    }
    Test(int i) {
        this->i = i;
    }
} ;
...
auto t = make_shared<Test>(1);
...
t.reset(somePointerToATestObject);

Tracing the reset() in the last line (under VC++ 2010), I discovered that it creates a new reference-counting object.

Is there a cheaper way, that reuses the existing ref-count and does not bother the heap?

© Stack Overflow or respective owner

Related posts about c++

Related posts about visual-studio-2010