 <p>This line is not necessary and should be removed from your code:</p> <pre><code>gpuErrchk( cudaMalloc((void**)&amp;dev_inp, sizeof(unsigned char)*flipped.rows*flipped.cols) ); </code></pre> <p>This line creates a device allocation, and assigns the pointer for that allocation to <code>dev_inp</code>.</p> <p>The problem arises here:</p> <pre><code>gpuErrchk( cudaGraphicsResourceGetMappedPointer((void **)&amp;dev_inp, &amp;size, cuda_resource) ); </code></pre> <p>This line acquires a <em>new</em> pointer, derived from the <code>cuda_resource</code> object, to another, different allocation, and places that pointer into <code>dev_inp</code>, <strong>overwriting</strong> your previously allocated pointer (from <code>cudaMalloc</code>). The new pointer acquired in this line already has an underlying device allocation. You do not need to allocate for it separately/additionally at this point.</p> <p>At this point, if you try to free <code>dev_inp</code>:</p> <pre><code>gpuErrchk(cudaFree(dev_inp)); // &lt;--- Error here </code></pre> <p>You are attempting to free data that your program did not explicitly allocate (via <code>cudaMalloc</code>), and is furthermore a necessary component of the persistent (at this point) <code>cuda_resource</code> object. You don't want to do that. Unfortunately, the original pointer that was placed in <code>dev_inp</code> is now lost (overwritten), and so there is no way to "free" it in your program, and you will have a memory leak, as long as the program is executing.</p> <p>The solution is not to perform the extra, unneeded allocation:</p> <pre><code>gpuErrchk( cudaMalloc((void**)&amp;dev_inp, sizeof(unsigned char)*flipped.rows*flipped.cols) ); </code></pre> <p>This then means that the corresponding <code>cudaFree</code> operation should be eliminated as well:</p> <pre><code>gpuErrchk(cudaFree(dev_inp)); // &lt;--- Error here </code></pre> <p>I would not use <code>cudaDeviceReset</code> anywhere in a CUDA code, especially a CUDA/OpenGL code, until the program is actually exiting. There are a few other very specialized situations where you might want to use <code>cudaDeviceReset</code> before the point at which you actually intended to exit your program, but they don't apply here.</p>