title: Debug Assertion Failed … _BLOCK_TYPE_IS_VALID(pHead->nBlockUse)
body: <p>I have got a really bad memory leak I am trying to fix, but somehow i am not able to delete Objects without triggering this assertation.</p>
  
  <p>I have searched for a solution via Google and have read the Questions on stackoverflow about this Error but I was still not able to find the answer!</p>
  
  <p>Possible reasons to get this Error according to my research:<br>
  1. deleting objects more then one<br>
  2. shadow copying<br>
  3. creating and deleting Objects that are loaded from an external dll<br>
  4. creating objects without storing the pointer</p>
  
  <p>BUT:<br>
  1. I checked the code and was not able to find double deletion<br>
  2. I use a copy constructor to copy Objects<br>
  3. The Error relatet classes are build (with MS Visual Studio) to a seperate lib but not to a dll. AND all the classes that are related to this error are located in the same lib.<br>
  4. I checked the code and it seems like that's not the problem</p>
  
  <p>It would be great if anybody is able to spot the mistake in the code below, and I appreciate every hint that points me to the solution of the problem.</p>
  
  <p>EDIT:<br>
  I forgot to mention the same deleting problem in sendThreadMain of MessageSystem (see code below). If i delete the Message there it causes unexpected errors somewhere else in the code. Might just be wrong data transmission... but i do not really know.<br>
  This code is run on Windows and Linux!</p>
  
  <p>Here are the error related parts of the code:</p>
  
  <p>Message</p>
  
  <pre><code>class Message 
  {
  public:
      Message (char type, unsigned char id, unsigned short size) 
      {
          mType = type;
          mId = id;
          mSize= size;
      }
  
      Message(const Message &amp;o)
      {
          mType = o.mType;
          mId = o.mId;
          mSize = o.mSize;
      }
  
      char getType() const {return mType;};
      unsigned char getId() const {return mId;};
      unsigned short getSize() const {return mSize;};
  
  protected:
      char mType;
      unsigned char mId;
      unsigned short mSize;
  };
  
  
  class JoinMessage : public Message
  {
  public:
      JoinMessage () : Message ('j', 0, sizeof (JoinMessage))
      {
          team = TEAM_SPECTATOR;
      }
      JoinMessage (unsigned char id) : Message ('j', id, sizeof (JoinMessage)){}
      JoinMessage (const JoinMessage &amp;o) : Message (o)
      {
          team = o.team;
          setName(o.getName());
      }
  
  
      void setName(std::string newName)
      {
          if (newName.length() &gt; MAX_PLAYER_NAME_LENGHT)
              newName = newName.substr(0, MAX_PLAYER_NAME_LENGHT);
  
          memset(name, 0, MAX_PLAYER_NAME_LENGHT);
          for(unsigned int i = 0; i &lt; newName.length(); i++)
              name[i] = newName[i];
      }
  
      std::string getName() const
      {
          std::string stringToReturn;
  
          for(unsigned int i = 0; i &lt; MAX_PLAYER_NAME_LENGHT; i++)
          {
              if (name[i])
                  stringToReturn.push_back(name[i]);
              else
                  break;
          }
  
          return stringToReturn;
      }
  
      TeamIdentifier team;
  
  private:
      unsigned char name[MAX_PLAYER_NAME_LENGHT];
  };
  
  // there are a lot other messages
  </code></pre>
  
  <p>MessageQueue</p>
  
  <pre><code>MessageQueue::~MessageQueue()
  {
      boost::mutex::scoped_lock lock (queueMutex);
  
      while(messageQueue.size() &gt; 0)
      {
          // the crash is non-reproducible
          // works 90% of the time
          delete messageQueue.front (); // &lt;- Debug Assertion Failed … _BLOCK_TYPE_IS_VALID
          messageQueue.pop_front();
      }
  
  }
  
  void MessageQueue::enqueMessage (Message* message)
  {
      {
          boost::mutex::scoped_lock lock (queueMutex);
          messageQueue.push_back(message);
      }
  }
  
  Message* MessageQueue::dequeMessage ()
  {
      boost::mutex::scoped_lock lock (queueMutex);
      if (messageQueue.size() == 0) 
          return nullptr;
  
      Message* message = messageQueue.front ();
      messageQueue.pop_front();
  
      return message;
  }
  </code></pre>
  
  <p>MessageSystem</p>
  
  <pre><code>template &lt;class MessageType&gt;
  void broadcast (MessageType &amp;message)
  {
      MessageType *internMessage = new MessageType(message);
  
      boost::mutex::scoped_lock lock (mRecipientMapMutex);
      std::map &lt;boost::asio::ip::udp::endpoint, MessageQueue *&gt;::iterator it;
  
      for (it = mRecipientMap.begin ();
          it != mRecipientMap.end ();
          it++)
      {
          it-&gt;second-&gt;enqueMessage(internMessage);
  
      }
  }
  
  
  template &lt;class MessageType&gt;
  void post (MessageType &amp;message, boost::asio::ip::udp::endpoint &amp;recipient)
  {
      MessageType *internMessage = new MessageType(message);
  
      std::map &lt;boost::asio::ip::udp::endpoint, MessageQueue* &gt;::iterator it;
      MessageQueue *messageQueue = NULL;
      {
          boost::mutex::scoped_lock lock (mRecipientMapMutex);
          it = mRecipientMap.find (recipient);
          if (it != mRecipientMap.end())
              messageQueue = it-&gt;second;
  
          if(messageQueue)
              messageQueue-&gt;enqueMessage (internMessage);
      }
  
  }
  
  
  void MessageSystem::sendThreadMain ()
  {
      // copy endpoints to vecotr so it can be
      // deleted from map while iterating
      std::vector&lt;udp::endpoint&gt; endpoints;
      {
          boost::mutex::scoped_lock lock (mRecipientMapMutex);
          std::map &lt;udp::endpoint, MessageQueue *&gt;::iterator mapIt = mRecipientMap.begin ();
          while (mapIt != mRecipientMap.end())
          {
              endpoints.push_back(mapIt-&gt;first);
              mapIt++;
          }
      }
  
      std::vector&lt;udp::endpoint&gt;::iterator endpointIt = endpoints.begin();
          while (endpointIt != endpoints.end())
          {
              char sendBuffer[PACKET_SIZE];
              int sendBufferPosition = 0;
              {
                  boost::mutex::scoped_lock lock (mRecipientMapMutex);
  
                  MessageQueue *messageQueue = mRecipientMap[*endpointIt];
                  if (messageQueue == nullptr)
                  {
                      mRecipientMap.erase(*endpointIt);
                      endpointIt++;
                      continue;
                  }
  
                  while (Message *message = messageQueue-&gt;dequeMessage ())
                  {
                      if (sendBufferPosition + message-&gt;getSize() &gt; PACKET_SIZE) 
                      {
                          // put message back and send it later
                          messageQueue-&gt;enqueMessage (message);
                          break;
                      }
  
                      // copy message into buffer
                      std::memcpy (
                          &amp;sendBuffer [sendBufferPosition], message, message-&gt;getSize());
  
                      sendBufferPosition += message-&gt;getSize();
                      // deleting this message causes a crash if 2 or more
                      // recipients are registered within MessageSystem
                      //delete message; &lt;- RANDOM CRASH elsewhere in the program
                  }
              }
      .... // more code down here that seems not related to the error
  </code></pre>