mardi 21 août 2018

shared_ptr not returned correctly

I have the following code:

class MarchingEvent
{
...
};

class DerivedEvent : public MarchingEvent
{
...
};

std::shared_ptr<MarchingEvent> function1()
{
    return std::make_shared<DerivedEvent>();
}

std::shared_ptr<MarchingEvent> function2()
{
    std::shared_ptr<MarchingEvent> event = function1()
    if (event)
        return event; // This line crashes...

    return std::shared_ptr<MarchingEvent>();
}

When running function2() I get a crash at line return event;, the crash is a SIGABRT and I can trace back to std file shared_ptr_base.h in the context of:

  // Counted ptr with no deleter or allocator support
  template<typename _Ptr, _Lock_policy _Lp>
    class _Sp_counted_ptr final : public _Sp_counted_base<_Lp>
    {
    public:
      explicit
      _Sp_counted_ptr(_Ptr __p)
      : _M_ptr(__p) { }

      virtual void
      _M_dispose() noexcept
      { delete _M_ptr; }

Do you have any idea of what I am doing wrong?

Aucun commentaire:

Enregistrer un commentaire