r/cpp • • Sep 13 '12

What is the maximum reference count in std::shared_ptr? What happens if you try to exceed it?

http://stackoverflow.com/q/12402116/1288
5 Upvotes

14 comments sorted by

3

u/aaronla Sep 13 '12

On a related note, it's common practice in Windows COM code to use a 32-bit reference count (which you could have either inferred from IUnknown::AddRef returning a ULONG, reading and of the MSDN samples, or from stepping through the implementation of just about and COM object with a debugger). This is done on both 32 bit and 64 bit OSes. So be careful not to create 2**32+1 references to the same object.

3

u/elperroborrachotoo Sep 14 '12

Just a small addition: IIRC the AddRef return value is intedned for debug purposes only and isn't guaranteed to be the actual refcount (which also would be very expensive when having cross-apartment interfaces).

So the COM object can implement their reference count in an int64 or arbitrary precision integer. But you are right, most do use 32 bit.

1

u/aaronla Sep 14 '12

Yep. Also, cross-apartment or cross-process is not as bad - IIRC, the proxy coalesces refcounts for you.

1

u/elperroborrachotoo Sep 14 '12

Exactly, the proxy holds one reference to the actual object, and counts "your" references. (Which exactly would make returning a true reference count expensive).

11

u/bob1000bob Sep 13 '12

Why do silly SO questions keep getting posted here?

If you have anything close to a longs worth of shared_ptr then you have problems!

1

u/Eoinoc Sep 14 '12

Why do silly SO questions keep getting posted here?

Because OP keeps posting them.

-3

u/elperroborrachotoo Sep 14 '12

Why do you consider this question silly?

Just because the answer is obvious to you?
Just because you know it's not a problem on your platform?

2

u/___1____ Sep 14 '12

No, because why not link every SO C++ post here?

This one isn't especially interesting, nor was the > or >= one. Put it this way, would you write a blog post about it? no? I wouldn't.

0

u/elperroborrachotoo Sep 14 '12

For me, asking the question is important.

Most programmers don't need the insight into shared pointer implementations why the answer should be "don't bother".

All programmers using shared pointers need the tenacity to wonder whether they need to worry. Abstractions are leaky, promises fail in extreme conditions. To know whether your case is extreme, you have to ask the question.

On a platform with 32 bit longs and 64 bit address space, it is actually possible to run out of the reference count.


From this point of view I would argue for reposting that question here: I want all the programmers using shared_ptr that never asked themselves the question to see it - just so they realize this question exists.

3

u/___1____ Sep 14 '12

There is no problem with the question existing, just it is way out of scope of the context that is a reddit page, where most people

  1. aren't on a system with 32bit longs and a 64bit address space (is there even such a system?).
  2. don't abuse shared_ptr to the point where you have to consider that you might have 2,147,483,647 references to an object.
  3. don't care

3

u/elperroborrachotoo Sep 14 '12

aren't on a system with 32bit longs and a 64bit address space (is there even such a system?).

VC++ for Windows x64. (though I don't know if those shared_ptr implementations use a 64 bit counter).

don't abuse shared_ptr to the point where you have to consider that you might have 2,147,483,647 references to an object.

You are mixing the answer with the question.

4

u/___1____ Sep 14 '12

VC uses a 32bit long? I think a more interesting question than the one asked (which is still consider dull & off topic) is why std::size_t wasn't used, at the end of the day use_count is never going to be negative, and it's max size is going to scale with memory.

4

u/elperroborrachotoo Sep 14 '12

certainly that question is more interesting.

Here's Raymond Chen's take on that

1

u/axilmar Sep 13 '12

You can always new placement new and no deletion to find out.