title: _M_ construct null not valid error when trying to implement multi threaded queue
body: <p>I am trying to implement a priority queue using using a simple linear approach as explained in <a href="https://rads.stackoverflow.com/amzn/click/com/B008CYT5TS" rel="nofollow noreferrer" rel="nofollow noreferrer">Art of Multiprocessor programming</a>. I'm new to c++ and have difficulty troubleshooting.</p>
  <p>I've implemented two template classes and am testing them using a simple test method. As I'm not able to pin point the error, I'm pasting all the three classes below for reference.</p>
  <p>I know that <code>_M_ construct null not valid</code> comes when trying to construct a string using <code>nullptr</code>, but am not sure where I'm doing that.</p>
  <p>The three classes created are given below:</p>
  <h1>bin.h</h1>
  <pre><code>#include &lt;mutex&gt;
  #include &lt;deque&gt;
  #include &lt;memory&gt;
  #include &lt;iostream&gt;
  using namespace std;
  
  namespace priority
  {
      template&lt;typename T&gt;
      class Bin
      {
      private:
          std::deque&lt;T&gt; v;
          std::mutex m;
      public:
          Bin() {
          }
  
          Bin(const Bin &amp;o) {
              
          }
  
          const Bin &amp;operator=(const Bin &amp;other) {
              return *this;
          }
  
          void put(T item) {
              std::lock_guard&lt;std::mutex&gt; lock(m);
              v.push_back(item);
          }
  
          T *get() {
              std::lock_guard&lt;std::mutex&gt; lock(m);
              if (v.size() == 0) {
                  return nullptr;
              }
              else {
                  T val = v.front();
                  T *ptr_val = &amp;(val);
                  v.pop_front();
                  return ptr_val;
              }
          }
  
          bool isEmpty() {
              std::lock_guard&lt;std::mutex&gt; lock(m);
              return v.size() == 0;
          }
          
      };  
  }
  </code></pre>
  <h1>SimpleLinear.h</h1>
  <pre><code>#include &lt;mutex&gt;
  #include &lt;vector&gt;
  #include &lt;memory&gt;
  #include &quot;Bin.h&quot;
  
  namespace priority
  {   
      template&lt;typename T&gt;
      class SimpleLinear
      {
      private:
          int range;
          std::vector&lt;Bin&lt;T&gt;&gt; pqueue;
      public:
          SimpleLinear(int range){
              this-&gt;range = range;
  
              for (int i = 0; i &lt; range; i++)
              {
                  pqueue.push_back(Bin&lt;T&gt;());
              }
  
          }
  
          void add(T item, int key) {
              pqueue[key].put(item);
          }
  
          T removeMin() {
              for (int i = 0; i &lt; range; i++)
              {
                  T *item = pqueue[i].get();
                  if (item != nullptr) {
                      return *item;
                  }
              }
              return nullptr;
          }
          
      };
  }
  </code></pre>
  <h1>test.cpp</h1>
  <pre><code>#include &lt;iostream&gt;
  #include &lt;vector&gt;
  #include &lt;thread&gt;
  #include &lt;algorithm&gt;
  #include &quot;SimpleLinear.h&quot;
  using namespace std;
  using namespace priority;
  
  void te(SimpleLinear&lt;string&gt; s, int thread_id) {
  
      s.add(&quot;sundar&quot;+to_string(thread_id), thread_id);
      s.add(&quot;akshaya&quot;+to_string(thread_id), 3);
      s.add(&quot;anirudh&quot;+to_string(thread_id), 1);
      s.add(&quot;aaditya&quot;+to_string(thread_id), 5);
      cout &lt;&lt; s.removeMin() &lt;&lt; endl;
      cout &lt;&lt; s.removeMin() &lt;&lt; endl;
      cout &lt;&lt; s.removeMin() &lt;&lt; endl;
  
  }
  
  
  int main(int argc, char const *argv[])
  {   
  
      SimpleLinear&lt;string&gt; s(100);
      std::vector&lt;std::thread&gt; v;
  
      for (int i = 0; i &lt; 100; i++)
      {   
          // if (i % 2 == 0)
              v.push_back(thread(te, std::ref(s), i));
          // else
              // v.push_back(thread(t, std::ref(s), i));
  
          
      }
      for_each(v.begin(), v.end(), std::mem_fn(&amp;std::thread::join));
      return 0;
  }
  </code></pre>
  <p>I'm getting the error:</p>
  <pre><code>terminate called after throwing an instance of 'std::logic_error'
    what():  basic_string::_M_construct null not valid
  terminate called recursively
  terminate called recursively
  terminate called recursively
  Aborted (core dumped)
  </code></pre>