title: cudaFree and cudaFreeHost fails to free heap allocated memory
body: <p>I have written a class in which constructor memory in a heap is allocated with cudaMallocHost() and cudaMalloc().</p>
  
  <p>If I try to free memory cudaFree() or cudaFreeHost(), GPUassert complains:</p>
  
  <blockquote>
    <p>GPUassert: invalid device pointer ../src/main.cu 97</p>
  </blockquote>
  
  <p>or</p>
  
  <blockquote>
    <p>GPUassert: invalid argument ../src/main.cu 95</p>
  </blockquote>
  
  <p>I am using CUDA TK 7.0 on a device with compute capability 2.1.</p>
  
  <p>I guess I am missing something fundamental. 
  Can I create objects which allocate memory on device? </p>
  
  <pre><code>class FreeMe {
  
  public:
      FreeMe(int size);
      ~FreeMe(void);
  
  private:
      float *A, *dA;
      int size;
  };
  
  FreeMe::FreeMe(int size) :
          size(size) {
      gpuErrchk(cudaMallocHost((void** ) &amp;A, sizeof(float) * size));
      gpuErrchk(cudaMalloc((void** ) &amp;dA, sizeof(float) * size));
  }
  FreeMe::~FreeMe(void) {
      std::cout &lt;&lt; "FreeMe obj deleted: Free ..." &lt;&lt; std::endl;
      gpuErrchk(cudaFreeHost(A));
      gpuErrchk(cudaFree(dA));
  }
  
  int main(int argc, char **argv) {
      int size = 3;
  
      FreeMe free1(size);
  
      cudaDeviceReset();
      std::cout &lt;&lt; "Program terminated successfully." &lt;&lt; std::endl;
      return EXIT_SUCCESS;
  }
  </code></pre>