title: Invalid address specified to RtlValidateHeap error with templated class
body: <p>I have this class: </p>
  
  <pre><code>class HIDValue{
  private:
  
      void* mValue;
      UINT mSize;
      HIDElement mElement;
  
  public:
      HIDValue() {
          mValue = 0;
          mSize = 0;
      }
  
      template &lt;class T&gt;
      HIDValue(T pValue, HIDElement pElement) {
          mElement = pElement;
          mValue = 0;
          setValue(pValue);
      }
  
      HIDValue(const HIDValue&amp; pValue) {
          mSize = pValue.mSize;
          mElement = pValue.mElement;
          mValue = 0;
          if(mSize) {
              mValue = new char[mSize];
              memcpy(mValue, pValue.mValue, mSize);
          }
      }
  
      template &lt;class T&gt;
      void setValue(T pValue) {
          if(mValue)
              delete mValue;
  
          mValue = new T;
          *((T*)mValue) = *((T*)&amp;pValue);
          mSize = sizeof(T);
      }
  
      ~HIDValue() {
                  //THE MENTIONED ERROR IS HERE
          if(mValue)
              delete mValue;
      }
  
      void setElement(HIDElement pElement) {
          mElement = pElement;
      }
  
      const HIDElement&amp; getElement() const {
          return mElement;
      }
  
      template &lt;class T&gt;
      bool getValue(T* pValue) const {
          if(mValue &amp;&amp; mSize &lt;= sizeof(T)) {  
              *pValue = *((T*)mValue);
              return true;
          }
  
          return false;
      }
  };
  </code></pre>
  
  <p>With some changes it's working fine:</p>
  
  <pre><code>class HIDValue{
  private:
  
      //void* mValue;
      char mValue[16];
      UINT mSize;
      HIDElement mElement;
  
  public:
      HIDValue() {
          //mValue = 0;
          mSize = 0;
      }
  
      template &lt;class T&gt;
      HIDValue(T pValue, HIDElement pElement) {
          mElement = pElement;
          //mValue = 0;
          setValue(pValue);
      }
  
      HIDValue(const HIDValue&amp; pValue) {
          mSize = pValue.mSize;
          mElement = pValue.mElement;
          //mValue = 0;
          if(mSize) {
              //mValue = new char[mSize];
              memcpy(mValue, pValue.mValue, mSize);
          }
      }
  
      template &lt;class T&gt;
      void setValue(T pValue) {
          //if(mValue)
          //  delete mValue;
  
          //mValue = new T;
          *((T*)mValue) = *((T*)&amp;pValue);
          mSize = sizeof(T);
      }
  
      ~HIDValue() {
          //itten egy hiba vala
          //if(mValue)
          //  delete mValue;
      }
  
      void setElement(HIDElement pElement) {
          mElement = pElement;
      }
  
      const HIDElement&amp; getElement() const {
          return mElement;
      }
  
      template &lt;class T&gt;
      bool getValue(T* pValue) const {
          if(mValue &amp;&amp; mSize &lt;= sizeof(T)) {  
              *pValue = *((T*)mValue);
              return true;
          }
  
          return false;
      }
  };
  </code></pre>
  
  <p>I am curious what is the reason of the error.</p>
  
  <p>Thanks ahead and sorry for my english!</p>