
On Sat, 2008-04-26 at 18:34 +0300, Peter Dimov wrote:
Daniel Frey:
When looking at the test case that fails with .use_count() != 0, I don't really see why the call to shared_from_this() in line 130 should throw.
The test looks correct to me. After early_px.reset() and px.reset(), no shared_ptr owns &x. Their initial use_count() is 2, so after the two resets a weak_ptr to &x should expire (it might be a good idea to add it to the test). We'll indeed need weak_count::empty to make this and test5 both pass, though.
The current test protects against misuse of shared_from_this, the exception it expects is just a diagnostic for it. My new version even allows one more use case: When the refactored implementation reaches line 130, the weak_count.use_count() == 0 is true, thus init_weak_once() initializes a fresh _shared_count/_weak_count pair and expects someone else to take over ownership later as if the object was just created. Unless I'm missing something... Regards, Daniel