Error in cudaFreeHost

The whole calculation of our CUDA code is all fine, except that the cudaFreeHost reported code=11(cudaErrorInvalidValue) in the final clearance step. It seems not making trouble now, but we were wondering if some bug inside triggered this error. In that case it would be serious problem. What could cause this problem?

The allocation has for sure been done by
checkCudaErrors( cudaMallocHost((void**)&global->host_lam, max_nNumerical*sizeof(double)) );

And the value of this pointer is not changed after allocation and before free. It is always 0x7ffeb1121128.

The version of CUDA is 7.5.
nvcc: NVIDIA ® Cuda compiler driver
Copyright © 2005-2015 NVIDIA Corporation
Built on Tue_Aug_11_14:27:32_CDT_2015
Cuda compilation tools, release 7.5, V7.5.17

Have you double-checked that proper error checking is done for each preceding CUDA API call, and all preceding kernel launches? If there are any missing status checks, the error reported by cudaFreeHost() might in fact originate with an earlier API call or kernel launch.

As a quick test of what @njuffa said, just place another cuda operation (e.g. cudaDeviceSynchronize()) immediately before the offending cudaFree operation, and see if it reports an error. If it does, you are chasing an asynchronous error that “occurred” prior to the cudaFree operation.

Also , if you freed that pointer once already, I think you would see this error. Note that the pointer value does not change as a result of the free operation.

What is the actual line of code that you are using to free the pointer?

It’s likely that if you invest the effort to create a simplified reproducer code, you would probably discover the actual problem yourself. If not, if you can post a short code here that reproduces the issue, someone may have further ideas.

I struggled with this for awhile. My problem turned out to be calling cudaDeviceReset():