From 39005d30196c67e1b4e4e3cdbc9f43c8bcd24f91 Mon Sep 17 00:00:00 2001 From: Eric Fiselier Date: Sun, 26 Jun 2016 23:56:32 +0000 Subject: Fix PR27115 - enable_shared_from_this does not work as a virtual base class. See https://llvm.org/bugs/show_bug.cgi?id=27115 The problem was that the conversion from 'const enable_shared_from_this*' to 'const T*' didn't work if T inherited enable_shared_from_this as a virtual base class. The fix is to take the original pointer passed to shared_ptr's constructor in the __enable_weak_this method and perform an upcast to 'const T*' instead of performing a downcast from the enable_shared_from_this base. llvm-svn: 273835 --- .../util.smartptr.enab/enable_shared_from_this.pass.cpp | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'libcxx/test/std/utilities') diff --git a/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.enab/enable_shared_from_this.pass.cpp b/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.enab/enable_shared_from_this.pass.cpp index 6661e831ab8..baee66e123e 100644 --- a/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.enab/enable_shared_from_this.pass.cpp +++ b/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.enab/enable_shared_from_this.pass.cpp @@ -39,12 +39,28 @@ struct Z : Y {}; void nullDeleter(void*) {} +struct Foo : virtual public std::enable_shared_from_this +{ + virtual ~Foo() {} +}; + +struct Bar : public Foo { + Bar(int) {} +}; + + int main() { { // https://llvm.org/bugs/show_bug.cgi?id=18843 std::shared_ptr t1(new T); std::shared_ptr t2(std::make_shared()); } + { // https://llvm.org/bugs/show_bug.cgi?id=27115 + std::shared_ptr t1(new Bar(42)); + assert(t1->shared_from_this() == t1); + std::shared_ptr t2(std::make_shared(42)); + assert(t2->shared_from_this() == t2); + } { std::shared_ptr p(new Z); std::shared_ptr q = p->shared_from_this(); -- cgit v1.2.3