title: Double free or corruption when using a unique_ptr and inheritance
body: <h1>Problem:</h1>
  
  <p>Crashing with "double free or corruption" when overwriting the <code>unique_ptr</code></p>
  
  <h1>Setup:</h1>
  
  <p>I'm working on a calendar app. I am using SFML which has a class <code>sf::Drawable</code>.<br>
  I have a class <code>DayView : public sf::Drawable</code> and a class <code>WeekView : public sf::Drawable</code>. 
  These two classes are the concrete implementations to display the calendar content. One day-wise one week-wise.<br>
  In the class <code>CalendarScene</code> I'd like to have a pointer to the current implementation that I can simply replace with a click.<br>
  So I place them into an <code>unique_ptr&lt;sf::Drawable&gt; displayImpl</code> to automatically delete them on overwrite.</p>
  
  <p>Now I assign the <code>displayImpl = unique_ptr&lt;WeekView&gt;(new WeekView(...))</code><br>
  If I now press a button an reassign <code>displayImpl = unique_ptr&lt;DayView&gt;(new DayView(...))</code> the application crashes in the (<code>virtual</code>) destructor of <code>WeekView</code> with the message <code>*** Error in 
  '/home/XXX/workspaces/CDT/XXX/Debug/XXX': double free or corruption (out): 0x0000000000f9ed60 ***</code></p>
  
  <p>I also have a <code>shared_ptr&lt;vector&lt;shared_ptr&lt;Calendar::Entry&gt;&gt;&gt;</code> as member in either of the <code>*View</code> classes. Don't know if this plays a role.</p>
  
  <p>Where lies my problem?</p>
  
  <h1>Code</h1>
  
  <pre><code>/*
   * CalendarScene.h
   *
   *  Created on: 17. Nov. 2016
   *      Author: martin
   */
  
  #ifndef SCENES_CALENDARSCENE_H_
  #define SCENES_CALENDARSCENE_H_
  
  #include "../Scene.h"
  #include "../api/google/GoogleApi.h"
  #include "../api/Calendar.h"
  #include "../event/Event.h"
  #include &lt;vector&gt;
  #include &lt;memory&gt;
  
  class CalendarScene: public Scene {
      public:
          CalendarScene(GoogleApi * api);
          virtual ~CalendarScene();
  
          void prepare(const sf::Vector2u size) override;
          void event(Event &amp;evt) override;
  
      private:
          void draw(sf::RenderTarget &amp;, sf::RenderStates) const override;
          sf::String texttodisplay;
  
          std::vector&lt;std::shared_ptr&lt;Calendar&gt;&gt; calendars;
          Calendar::Entries ents;
          Json::Value calendarList;
          Json::Value eventList;
  
          enum { WEEK, DAY } displayMode;
          int wOffset;
  
          std::unique_ptr&lt;sf::Drawable&gt; displayImpl = nullptr;
  
  };
  
  #endif /* SCENES_CALENDARSCENE_H_ */
  </code></pre>
  
  <hr>
  
  <pre><code>/*
   * CalendarScene.cpp
   *
   *  Created on: 17. Nov. 2016
   *      Author: martin
   */
  
  #include "CalendarScene.h"
  #include &lt;SFML/Graphics.hpp&gt;
  #include &lt;algorithm&gt;
  #include "../ui/SmartText.h"
  #include "../ui/UIFactory.h"
  #include "../util/sfmladditions.h"
  
  using std::unique_ptr;
  using std::shared_ptr;
  using std::runtime_error;
  using std::time;
  using std::time_t;
  using std::string;
  
  using io::curl::parameter_map;
  
  using Json::Value;
  using Json::ValueIterator;
  
  namespace {
  
  typedef shared_ptr&lt;vector&lt;shared_ptr&lt;Calendar::Entry&gt;&gt;&gt; SharedEntrySharedVector;
  
  enum Format {
      PORTRAIT, LANDSCAPE
  };
  
  
  class WeekView : public sf::Drawable {
  public:
      WeekView(SharedEntrySharedVector weekEntries) : ents(weekEntries) {}
      virtual ~WeekView() {}
  protected:
      virtual void draw(sf::RenderTarget&amp; target, sf::RenderStates states) const;
  private:
      SharedEntrySharedVector ents;
  };
  
  class DayView : public sf::Drawable {
  public:
      int dayOffsetToNow = 0;
  
      DayView(SharedEntrySharedVector dayEnt) : ents(dayEnt) {}
  
  protected:
      virtual void draw(sf::RenderTarget&amp; target, sf::RenderStates) const;
  private:
      SharedEntrySharedVector ents;
  };
  
  void WeekView::draw(sf::RenderTarget&amp; target, sf::RenderStates) const {
      /* some drawing */
  }
  
  void DayView::draw(sf::RenderTarget&amp; target, sf::RenderStates) const {
      /* some other drawing */
  }
  
  } // namespace
  
  CalendarScene::CalendarScene(GoogleApi * api) :
          Scene(api), displayMode(WEEK), wOffset(0), displayImpl(new WeekView(SharedEntrySharedVector())) {
  }
  
  CalendarScene::~CalendarScene() {
      calendars.clear();
  }
  
  /*
   *
   * Obtain calendar list
   * Obtain event list
   *
   */
  #include &lt;iostream&gt;
  void CalendarScene::prepare(const sf::Vector2u target) {
  
      /* populating the ents.entries field which is of type vector&lt;shared_ptr&lt;Calendar::Entry&gt;&gt; */
  
      displayImpl = unique_ptr&lt;WeekView&gt;(new WeekView(SharedEntrySharedVector(&amp;(ents.entries))));
  }
  
  void CalendarScene::event(Event &amp;evt) {
      if (evt.type() == Event::Type::ScreenEvent) {
          /* nothing interesting */
      } else if (evt.type() == Event::Type::ButtonEvent) {
          ButtonEvent &amp;b = (ButtonEvent&amp;) evt;
          switch (b.eventChar()) {
          case sf::Keyboard::Key::W:
              displayMode = WEEK;
              displayImpl = unique_ptr&lt;WeekView&gt;(new WeekView(SharedEntrySharedVector(&amp;(ents.entries))));
              break;
          case sf::Keyboard::Key::T:
              displayMode = DAY;
  /********  Here lies the problem  ********/
              displayImpl = unique_ptr&lt;DayView&gt;(new DayView(SharedEntrySharedVector(&amp;(ents.entries))));
              break;
          case sf::Keyboard::Key::Right:
              wOffset++;
              break;
          case sf::Keyboard::Key::Left:
              wOffset--;
              break;
          default:
              return;
          }
          if (wOffset &lt; 0)
              wOffset = 0;
          else if (wOffset &gt; 6)
              wOffset = 6;
          if (displayMode == DAY) {
              ((DayView*)displayImpl.get())-&gt;dayOffsetToNow = wOffset;
          }
      }
  }
  
  void CalendarScene::draw(sf::RenderTarget &amp;target, sf::RenderStates) const {
      if (!displayImpl)
          return;
  
      target.draw(*displayImpl);
  }
  </code></pre>
  
  <hr>
  
  <p>Valgrind says:</p>
  
  <pre><code>==27630== Invalid free() / delete / delete[] / realloc()
  ==27630==    at 0x4C2A360: operator delete(void*)
  ==27630==    by 0x4B6D4D: std::_Sp_counted_ptr&lt;std::vector&lt;std::shared_ptr&lt;Calendar::Entry&gt;, std::allocator&lt;std::shared_ptr&lt;Calendar::Entry&gt; &gt; &gt;*, (__gnu_cxx::_Lock_policy)2&gt;::_M_dispose()
  ==27630==    by 0x411665: std::_Sp_counted_base&lt;(__gnu_cxx::_Lock_policy)2&gt;::_M_release()
  ==27630==    by 0x41054A: std::__shared_count&lt;(__gnu_cxx::_Lock_policy)2&gt;::~__shared_count() 
  ==27630==    by 0x4ABDC9: std::__shared_ptr&lt;std::vector&lt;std::shared_ptr&lt;Calendar::Entry&gt;, std::allocator&lt;std::shared_ptr&lt;Calendar::Entry&gt; &gt; &gt;, (__gnu_cxx::_Lock_policy)2&gt;::~__shared_ptr() 
  ==27630==    by 0x4ABE51: std::shared_ptr&lt;std::vector&lt;std::shared_ptr&lt;Calendar::Entry&gt;, std::allocator&lt;std::shared_ptr&lt;Calendar::Entry&gt; &gt; &gt; &gt;::~shared_ptr()
  ==27630==    by 0x4A461E: (anonymous namespace)::WeekView::~WeekView() 
  ==27630==    by 0x4A46B3: (anonymous namespace)::WeekView::~WeekView() 
  ==27630==    by 0x4AE161: std::default_delete&lt;sf::Drawable&gt;::operator()(sf::Drawable*) const 
  ==27630==    by 0x4AF7BD: std::unique_ptr&lt;sf::Drawable, std::default_delete&lt;sf::Drawable&gt; &gt;::reset(sf::Drawable*)
  ==27630==    by 0x4AB181: std::enable_if&lt;std::__and_&lt;std::is_convertible&lt;std::unique_ptr&lt;(anonymous namespace)::DayView, std::default_delete&lt;(anonymous namespace)::DayView&gt; &gt;::pointer, sf::Drawable*&gt;, std::__not_&lt;std::is_array&lt;(anonymous namespace)::DayView&gt; &gt; &gt;::value, std::unique_ptr&lt;sf::Drawable, std::default_delete&lt;sf::Drawable&gt; &gt;&amp;&gt;::type std::unique_ptr&lt;sf::Drawable, std::default_delete&lt;sf::Drawable&gt; &gt;::operator=&lt;(anonymous namespace)::DayView, std::default_delete&lt;(anonymous namespace)::DayView&gt; &gt;(std::unique_ptr&lt;(anonymous namespace)::DayView, std::default_delete&lt;(anonymous namespace)::DayView&gt; &gt;&amp;&amp;) 
  ==27630==    by 0x4AA445: CalendarScene::event(Event&amp;) 
  ==27630==    by 0x4DC982: ClientWatch::ClientWatchPrivate::runWindowed() 
  ==27630==    by 0x4DC6AE: ClientWatch::ClientWatchPrivate::run() 
  ==27630==    by 0x4DC758: ClientWatch::run()
  ==27630==    by 0x529CE7: main 
  ==27630==  Address 0xeb334b0 is 48 bytes inside a block of size 176 alloc'd
  ==27630==    at 0x4C29180: operator new(unsigned long) 
  ==27630==    by 0x4E0904: _ZN11ClientWatch18ClientWatchPrivate9makeSceneI13CalendarSceneIRP9GoogleApiEEESt10unique_ptrI5SceneSt14default_deleteIS7_EEDpOT0_ 
  ==27630==    by 0x4DF88E: _ZN11ClientWatch18ClientWatchPrivate8newSceneI13CalendarSceneIRP9GoogleApiEEEvDpOT0_ 
  ==27630==    by 0x4DD519: ClientWatch::ClientWatchPrivate::drawFrame() 
  ==27630==    by 0x4DCB36: ClientWatch::ClientWatchPrivate::runWindowed() 
  ==27630==    by 0x4DC6AE: ClientWatch::ClientWatchPrivate::run() 
  ==27630==    by 0x4DC758: ClientWatch::run() 
  ==27630==    by 0x529CE7: main 
  ==27630== 
  </code></pre>
  
  <p>Changing the <code>unique_ptr</code>s to <code>shared</code> ones does not help. Still a double free error.</p>