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();










share|improve this question

















  • 1




    -> public std::enable_shared_from_this<A>.
    – Jarod42
    Nov 9 at 21:14














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();










share|improve this question

















  • 1




    -> public std::enable_shared_from_this<A>.
    – Jarod42
    Nov 9 at 21:14












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();










share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 9 at 21:09









user2123288

432716




432716







  • 1




    -> public std::enable_shared_from_this<A>.
    – Jarod42
    Nov 9 at 21:14












  • 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












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.






share|improve this answer




















  • 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 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










  • @kevin Probably different version of clang; I used stacked crooked.
    – Yakk - Adam Nevraumont
    Nov 10 at 1:31

















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).






share|improve this answer




















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













     

    draft saved


    draft discarded


















    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

























    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.






    share|improve this answer




















    • 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 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










    • @kevin Probably different version of clang; I used stacked crooked.
      – Yakk - Adam Nevraumont
      Nov 10 at 1:31














    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.






    share|improve this answer




















    • 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 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










    • @kevin Probably different version of clang; I used stacked crooked.
      – Yakk - Adam Nevraumont
      Nov 10 at 1:31












    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.






    share|improve this answer












    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.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    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 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










    • @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










    • 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










    • @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












    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).






    share|improve this answer
























      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).






      share|improve this answer






















        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).






        share|improve this answer












        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).







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 9 at 21:17









        StoryTeller

        89.7k12180246




        89.7k12180246



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Use pre created SQLite database for Android project in kotlin

            Darth Vader #20

            Ondo