title: Valgrind detects memory leak when dynamically allocated array of pointers
body: <p>I am currently working on a C++ application that uses a dynamic array of pointers. I am running valgrind on it and it does detect a memory leak in my constructor.</p>
  <p>This is what my class looks like:</p>
  <pre><code>class CustomerOrder
  {
  private:
      std::string m_name;
      std::string m_product;
      unsigned int m_cntItem;
      Item** m_lstItem;
      static size_t m_widthField;
  public:
      CustomerOrder();
      CustomerOrder(const std::string&amp; customerOrder);
      CustomerOrder(const CustomerOrder&amp; customerOrder);
      CustomerOrder&amp; operator=(const CustomerOrder&amp; customerOrder) = delete;
      CustomerOrder(CustomerOrder&amp;&amp; customerOrder) noexcept;
      CustomerOrder&amp; operator=(CustomerOrder&amp;&amp; customerOrder) noexcept;
      ~CustomerOrder();
  };
  </code></pre>
  <p>This is my constructor implementation:</p>
  <pre><code>CustomerOrder::CustomerOrder(const std::string&amp; customerOrder)
  {
      Utilities utilities;
      size_t next_pos = 0;
      size_t start_pos = 0;
      bool more = true;
      
  
      m_name = utilities.extractToken(customerOrder, next_pos, more);
      m_product = utilities.extractToken(customerOrder, next_pos, more);
      m_cntItem = 0;
  
      start_pos = next_pos;
  
      while (more) {
          utilities.extractToken(customerOrder, next_pos, more);
          m_cntItem++;
      }
  
      m_lstItem = new Item*[m_cntItem];
  
      more = true;
  
      for (size_t i = 0; i &lt; m_cntItem; i++) {
          m_lstItem[i] = new Item(utilities.extractToken(customerOrder, start_pos, more));
      }
  
      if (utilities.getFieldWidth() &gt; m_widthField) {
          m_widthField = utilities.getFieldWidth();
      }
  }
  </code></pre>
  <p>These are my move constructor and move assignment operator.</p>
  <pre><code>CustomerOrder::CustomerOrder(CustomerOrder&amp;&amp; customerOrder) noexcept : m_lstItem(nullptr)
  {
      *this = std::move(customerOrder);
  }
  
  CustomerOrder&amp; CustomerOrder::operator=(CustomerOrder&amp;&amp; customerOrder) noexcept
  {
      if (this != &amp;customerOrder) {
          if (m_lstItem) {
              for (unsigned int i = 0; i &lt; m_cntItem; i++) {
                  delete this-&gt;m_lstItem[i];
              }
          }
  
          delete [] m_lstItem;
  
          this-&gt;m_name = customerOrder.m_name;
          this-&gt;m_product = customerOrder.m_product;
          this-&gt;m_cntItem = customerOrder.m_cntItem;
          this-&gt;m_lstItem = customerOrder.m_lstItem;
  
          customerOrder.m_lstItem = nullptr;
      }
  
      return *this;
  }
  </code></pre>
  <p>Finally, this is my destructor.</p>
  <pre><code>CustomerOrder::~CustomerOrder()
  {
      if (m_lstItem) {
          for (unsigned int i = 0; i &lt; m_cntItem; i++) {
              delete this-&gt;m_lstItem[i];
          }
      }
  
      delete [] m_lstItem;
      m_lstItem = nullptr;
  }
  </code></pre>
  <p>Based on the above information, can anyone help me understand why the above errors occur?
  This is the output from Valgrind:</p>
  <pre><code>==9320== HEAP SUMMARY:
  ==9320==     in use at exit: 24 bytes in 1 blocks
  ==9320==   total heap usage: 70 allocs, 69 frees, 103,891 bytes allocated
  ==9320== 
  ==9320== 24 bytes in 1 blocks are definitely lost in loss record 1 of 1
  ==9320==    at 0x4C3289F: operator new[](unsigned long) (vg_replace_malloc.c:423)
  ==9320==    by 0x10D67E: CustomerOrder::CustomerOrder(std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; const&amp;) (CustomerOrder.cpp:45)
  ==9320==    by 0x10F15C: main (ms2.cpp:102)
  ==9320== 
  ==9320== LEAK SUMMARY:
  ==9320==    definitely lost: 24 bytes in 1 blocks
  ==9320==    indirectly lost: 0 bytes in 0 blocks
  ==9320==      possibly lost: 0 bytes in 0 blocks
  ==9320==    still reachable: 0 bytes in 0 blocks
  ==9320==         suppressed: 0 bytes in 0 blocks
  </code></pre>
  <p>I understand it happens in the constructor but I am not so sure how I can change the destructor accordingly.</p>