Why do I get an exception for this usage of enable_shared_from_this()?
up vote
2
down vote
favorite
On the following example below, something is escaping me. Why does this generate the usual weak_ptr exception when using the shared_from_this, if there is an outstanding shared_ptr reference to the object which calls it ?
class A : std::enable_shared_from_this<A>
public:
static std::shared_ptr<A> create()
return std::shared_ptr<A>(new A());
A()
void setParent(const std::shared_ptr<A>& other)
std::shared_ptr<A> keep()
auto o = A::create();
o->setParent(shared_from_this());
return o;
;
int main()
std::shared_ptr<A> a = A::create();
auto s = a->keep();
c++ c++11 std
add a comment |
up vote
2
down vote
favorite
On the following example below, something is escaping me. Why does this generate the usual weak_ptr exception when using the shared_from_this, if there is an outstanding shared_ptr reference to the object which calls it ?
class A : std::enable_shared_from_this<A>
public:
static std::shared_ptr<A> create()
return std::shared_ptr<A>(new A());
A()
void setParent(const std::shared_ptr<A>& other)
std::shared_ptr<A> keep()
auto o = A::create();
o->setParent(shared_from_this());
return o;
;
int main()
std::shared_ptr<A> a = A::create();
auto s = a->keep();
c++ c++11 std
1
->public std::enable_shared_from_this<A>
.
– Jarod42
Nov 9 at 21:14
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
On the following example below, something is escaping me. Why does this generate the usual weak_ptr exception when using the shared_from_this, if there is an outstanding shared_ptr reference to the object which calls it ?
class A : std::enable_shared_from_this<A>
public:
static std::shared_ptr<A> create()
return std::shared_ptr<A>(new A());
A()
void setParent(const std::shared_ptr<A>& other)
std::shared_ptr<A> keep()
auto o = A::create();
o->setParent(shared_from_this());
return o;
;
int main()
std::shared_ptr<A> a = A::create();
auto s = a->keep();
c++ c++11 std
On the following example below, something is escaping me. Why does this generate the usual weak_ptr exception when using the shared_from_this, if there is an outstanding shared_ptr reference to the object which calls it ?
class A : std::enable_shared_from_this<A>
public:
static std::shared_ptr<A> create()
return std::shared_ptr<A>(new A());
A()
void setParent(const std::shared_ptr<A>& other)
std::shared_ptr<A> keep()
auto o = A::create();
o->setParent(shared_from_this());
return o;
;
int main()
std::shared_ptr<A> a = A::create();
auto s = a->keep();
c++ c++11 std
c++ c++11 std
asked Nov 9 at 21:09
user2123288
432716
432716
1
->public std::enable_shared_from_this<A>
.
– Jarod42
Nov 9 at 21:14
add a comment |
1
->public std::enable_shared_from_this<A>
.
– Jarod42
Nov 9 at 21:14
1
1
->
public std::enable_shared_from_this<A>
.– Jarod42
Nov 9 at 21:14
->
public std::enable_shared_from_this<A>
.– Jarod42
Nov 9 at 21:14
add a comment |
2 Answers
2
active
oldest
votes
up vote
3
down vote
You need to inherit publicly from enable_shared_from_this
so the shared ptr ctor can see it.
As an aside, clang fails to compile your example due to this.
Thanks. Yes that was it, although the example compiled ok, and I even stepped debug it to confirm it was being raised by the shared_from_this(). Thanks!
– user2123288
Nov 9 at 21:20
What error does clang give?
– Kevin
Nov 9 at 21:45
@Kevin Thatshared_from_this
is inaccessible.
– Yakk - Adam Nevraumont
Nov 9 at 22:13
@Yakk-AdamNevraumont I don't see this. All I did was add#include <memory>
and it compiles fine.
– Kevin
Nov 10 at 0:40
@kevin Probably different version of clang; I used stacked crooked.
– Yakk - Adam Nevraumont
Nov 10 at 1:31
add a comment |
up vote
2
down vote
enable_shared_from_this
must be an unambiguous and accessible base of A
for the library to handle the internal weak pointer correctly.
Your inheritance is private (the default when using the class
keyword).
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
You need to inherit publicly from enable_shared_from_this
so the shared ptr ctor can see it.
As an aside, clang fails to compile your example due to this.
Thanks. Yes that was it, although the example compiled ok, and I even stepped debug it to confirm it was being raised by the shared_from_this(). Thanks!
– user2123288
Nov 9 at 21:20
What error does clang give?
– Kevin
Nov 9 at 21:45
@Kevin Thatshared_from_this
is inaccessible.
– Yakk - Adam Nevraumont
Nov 9 at 22:13
@Yakk-AdamNevraumont I don't see this. All I did was add#include <memory>
and it compiles fine.
– Kevin
Nov 10 at 0:40
@kevin Probably different version of clang; I used stacked crooked.
– Yakk - Adam Nevraumont
Nov 10 at 1:31
add a comment |
up vote
3
down vote
You need to inherit publicly from enable_shared_from_this
so the shared ptr ctor can see it.
As an aside, clang fails to compile your example due to this.
Thanks. Yes that was it, although the example compiled ok, and I even stepped debug it to confirm it was being raised by the shared_from_this(). Thanks!
– user2123288
Nov 9 at 21:20
What error does clang give?
– Kevin
Nov 9 at 21:45
@Kevin Thatshared_from_this
is inaccessible.
– Yakk - Adam Nevraumont
Nov 9 at 22:13
@Yakk-AdamNevraumont I don't see this. All I did was add#include <memory>
and it compiles fine.
– Kevin
Nov 10 at 0:40
@kevin Probably different version of clang; I used stacked crooked.
– Yakk - Adam Nevraumont
Nov 10 at 1:31
add a comment |
up vote
3
down vote
up vote
3
down vote
You need to inherit publicly from enable_shared_from_this
so the shared ptr ctor can see it.
As an aside, clang fails to compile your example due to this.
You need to inherit publicly from enable_shared_from_this
so the shared ptr ctor can see it.
As an aside, clang fails to compile your example due to this.
answered Nov 9 at 21:16
Yakk - Adam Nevraumont
178k19185363
178k19185363
Thanks. Yes that was it, although the example compiled ok, and I even stepped debug it to confirm it was being raised by the shared_from_this(). Thanks!
– user2123288
Nov 9 at 21:20
What error does clang give?
– Kevin
Nov 9 at 21:45
@Kevin Thatshared_from_this
is inaccessible.
– Yakk - Adam Nevraumont
Nov 9 at 22:13
@Yakk-AdamNevraumont I don't see this. All I did was add#include <memory>
and it compiles fine.
– Kevin
Nov 10 at 0:40
@kevin Probably different version of clang; I used stacked crooked.
– Yakk - Adam Nevraumont
Nov 10 at 1:31
add a comment |
Thanks. Yes that was it, although the example compiled ok, and I even stepped debug it to confirm it was being raised by the shared_from_this(). Thanks!
– user2123288
Nov 9 at 21:20
What error does clang give?
– Kevin
Nov 9 at 21:45
@Kevin Thatshared_from_this
is inaccessible.
– Yakk - Adam Nevraumont
Nov 9 at 22:13
@Yakk-AdamNevraumont I don't see this. All I did was add#include <memory>
and it compiles fine.
– Kevin
Nov 10 at 0:40
@kevin Probably different version of clang; I used stacked crooked.
– Yakk - Adam Nevraumont
Nov 10 at 1:31
Thanks. Yes that was it, although the example compiled ok, and I even stepped debug it to confirm it was being raised by the shared_from_this(). Thanks!
– user2123288
Nov 9 at 21:20
Thanks. Yes that was it, although the example compiled ok, and I even stepped debug it to confirm it was being raised by the shared_from_this(). Thanks!
– user2123288
Nov 9 at 21:20
What error does clang give?
– Kevin
Nov 9 at 21:45
What error does clang give?
– Kevin
Nov 9 at 21:45
@Kevin That
shared_from_this
is inaccessible.– Yakk - Adam Nevraumont
Nov 9 at 22:13
@Kevin That
shared_from_this
is inaccessible.– Yakk - Adam Nevraumont
Nov 9 at 22:13
@Yakk-AdamNevraumont I don't see this. All I did was add
#include <memory>
and it compiles fine.– Kevin
Nov 10 at 0:40
@Yakk-AdamNevraumont I don't see this. All I did was add
#include <memory>
and it compiles fine.– Kevin
Nov 10 at 0:40
@kevin Probably different version of clang; I used stacked crooked.
– Yakk - Adam Nevraumont
Nov 10 at 1:31
@kevin Probably different version of clang; I used stacked crooked.
– Yakk - Adam Nevraumont
Nov 10 at 1:31
add a comment |
up vote
2
down vote
enable_shared_from_this
must be an unambiguous and accessible base of A
for the library to handle the internal weak pointer correctly.
Your inheritance is private (the default when using the class
keyword).
add a comment |
up vote
2
down vote
enable_shared_from_this
must be an unambiguous and accessible base of A
for the library to handle the internal weak pointer correctly.
Your inheritance is private (the default when using the class
keyword).
add a comment |
up vote
2
down vote
up vote
2
down vote
enable_shared_from_this
must be an unambiguous and accessible base of A
for the library to handle the internal weak pointer correctly.
Your inheritance is private (the default when using the class
keyword).
enable_shared_from_this
must be an unambiguous and accessible base of A
for the library to handle the internal weak pointer correctly.
Your inheritance is private (the default when using the class
keyword).
answered Nov 9 at 21:17
StoryTeller
89.7k12180246
89.7k12180246
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53233296%2fwhy-do-i-get-an-exception-for-this-usage-of-enable-shared-from-this%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
->
public std::enable_shared_from_this<A>
.– Jarod42
Nov 9 at 21:14